I am trying to compile a simple string but it does not work... why?

334 views Asked by At

My compiler is Code::Blocks. I am trying to eliminate vocals from a character sequence.

#include <iostream>
#include <cstring>
using namespace std;
char sir[256];
int i=0;

int main (){
cout<<"sir=";cin.getline(sir,256);
for (i=strlen(sir);i>0;i--){
    if (strstr(sir[i],'aeiou')==0){
        strcpy(sir+i,sir+i+1);
        break;}}
cout<<"sir="<<sir<<"\n";
return 0;
}

I receive the following error:

error: call of overloaded 'strstr(char&, int)' is ambiguous
note: candidates are:
note: char* strstr(char*, cost char*) near match

But I think the problem is on strstr command...

4

There are 4 answers

0
Dangling_pointer On

'aeiou' is not a string literal in c/c++ use "aeiou". In c/c++ string literal are represented inside " "(double quotes)

Read more here

0
Pete Becker On

So, apparently, the idea is to remove vowels. As others have said, use "aeiou" and not 'aeiou'. But you also need to use the right function to check whether you have a vowel. That's strchr(const char* s, int c), not strstr. strchr looks for an occurrence of c in the string that s points to, and returns a pointer to that occurrence, or, if it's not found, a pointer to the terminating nil character. So the test in the original code should be:

if (*strchr("aeiou", sir[i] != '\0')

Personally, I'd write this a bit more succinctly:

if (*strchr("aeiou", sir[i]))
0
Bernd Elkemann On

As I wrote in the first comment, the expression

strstr(sir[i],'aeiou')

is wrong for two reasons: ' is for single characters, " is for strings, but the main reason is, that strstr finds the occurance of the whole thing, not of the characters separately.

Try this:

#include <iostream>
#include <cstring>
using namespace std;
char sir[256];
char sir2[256];
int i=0;

int main (){
    cout<<"sir=";cin.getline(sir,256);
    char* reader = sir;
    char* writer = sir2;
    while(*reader) {
        if(*reader!='a' && *reader!='e' && *reader!='i' && *reader!='o' && *reader!='u') {
            *writer = *reader;
            ++writer;
        }
        ++reader;
    }
    *writer = '\0';
    cout<<"sir="<<sir2<<"\n";
    return 0;
}
0
Tim Child On

ststr is defined by two function prototypes

const char* strstr( const char* str, const char* target );

      char* strstr(       char* str, const char* target );

your call is calling as

strstr(sir[i],'aeiou')

the first arg is a char type, not a char * type, so the compiler does know how to map that to const char * or char *

Also check your loop index as

 i=strlen(sir) 

will over index the char array and

i > 0 

will NOT access the last character.