Reputation: 1
char * piglatin(const char s[], int len) {
char * result[len+3] = s[];
char * current[len+3] = s[];
if(s[0]=="o"||"u"||"e"||"a"||"i"){
result[len-1] = "y";
result[len-2] = "a";
result[len-3] = "-";
}
else{
for(int i = 0; i<len-1; i++){
result[i] = current[i+1];
result[len-1] = "-";
result[len] = current[0];
result[len+1] = "a";
result[len+2] = "y";
}
}
}
I met a problem when I was doing program homework for my computer science class. the professor want us to append "-ay" after the string s if the first letter of s is vowel, otherwise remove the first letter of s and append "-?ay". My error appears at the "if(s[o]=="o"||"u"||"e"||"a"||"i")" and it said "comparison between pointer and integer ('int' and 'const char *')". I feel confused since s is not a pointer and the right hand side is not integer either.
Upvotes: 0
Views: 12789
Reputation: 21
cmon brother ... use 'o' not "o" for all others to and in if statement you have to compare them all to s[0] like this s[0]=='o'||s[0]=='u'||s[0]=='e'||s[0]=='a' and so on . but you will still get errors so dont forget to return a value of pointer :) and (const char s[]) s is constant how will you change it !!!!!! remove const
Upvotes: 2
Reputation: 129364
In the below, s[0]
is a char - so a form of integer, where "o"
is a string - const char *
- so you are comparing a letter 'a'(or such) to the address of the string `"o".
if(s[0]=="o"||"u"||"e"||"a"||"i"){
You should do:
if(s[0]=='o' ... )
However, the || 'u' doesn't mean what I think you think it means. Since none of the characters (or strings in your code) are zero/NULL, they become true
, and your if-statement will always be true.
You need to have a comparison statement:
if(s[0] == 'o'|| s[0] == 'u' ... )
Upvotes: 0
Reputation: 2044
There are two issues here. The compiler is complaining because s[0]
is a char
and "o"
(and others) are pointers to char arrays (basically). To fix this, replace "o"
with 'o'
.
There's a bigger issue though: you are only comparing s[0] to 'o'. The other things in your test will all evaluate to true:
if(s[0]=='o'||s[0]=='u'||s[0]=='e'||s[0]=='a'||s[0]=='i'){
Upvotes: 0