سؤال

I'm trying to create a program that removes vowels from a string, add them into a vector and then give the user the possibility of having the original code again.

In my code i have this:

char s[20];

And soon after i have this comparison:

for(j=0;j<tam;j++)
{
    if(strcmpi(s[j],"a")==1 ||
       (strcmpi(s[j],"e")==1 ||
       (strcmpi(s[j],"i") ==1||
       (strcmpi(s[j],"o") ==1||
       (strcmpi(s[j],"u")==1))
    {

So if the array is char and the vowels are char(""), why the compiler give me this error?:

[Warning] passing arg 1 of `strcmpi' makes pointer from integer without a cast

EDIT
As someone said the correct is s[j] == 'a', but that result in wrong way. If a put car the result is still car. Don't know why.

 if(s[j] == 'a' || 
    s[j] == 'A' || 
    s[j] == 'e' || 
    s[j] == 'E' || 
    s[j] == 'i' || 
    s[j] == 'I' || 
    s[j] == 'o' || 
    s[j] == 'O' || 
    s[j] == 'u' || 
    s[j] == 'U') 
 {
    s[j] = s[j++]; }
هل كانت مفيدة؟

المحلول

Strcmpi is for comparing strings. The first argument to strcmpi is of type char, when it expects a char*.

In order to compare single chars, s[j]=='e' is enough, or tolower(s[j])=='e' if you need it to be case insensitive. You'll need to include ctype.h. for tolower.

نصائح أخرى

The arguments to strcmpi must be strings, but s[j] is just a single character, not a string. You can use == to compare characters directly. To get case-insensitive comparisons, get the lowercase version of the character first and compare it.

for (j = 0; j < tam; j++) {
    char lower = tolower(s[j]);
    if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') {
        ...
    }
}

You don't want to use strcmp or any of its variants. Because you want to know whether the string contains vowels or not, you may want to use a substring search using strstr.

You use function strcmpi incorrectly. It first parameter has type const char * while you pass an argument of type char. That is the function expects a string while you pass only one character.

Moreover this function is not a standard C/C++ function. So it should not be used.

You could achieve the same result using the following approach

char vowels[] = "aeiou";
//...
if ( strchr( vowels, tolower( s[j] ) )
{
   std::cout << s[j] << " is a vowel" << std::endl;
}

You have already been told that strcmpi is not the right way to check single characters. This is an answer to the edit to your question, where you ask about actually stripping the vowels.

If you want to retain the original string, you need extra memory for the string without consonants. You also need two indices, because once you have skipped a vowel in the original string, the indices are out of sync. Here's an example implementation:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
    char orig[] = "Jackdaws love my big sphinx of quartz.";
    char cons[sizeof(orig)];        // Make new char buffer as big
                                    // as the original

    int i, j;

    j = 0;
    for (i = 0; orig[i]; i++) {
        if (strchr("AEIOUaeiou", orig[i]) == NULL) {
            cons[j++] = orig[i];
        }
    }
    cons[j] = '\0';                 // Terminate consonant string

    printf("was: '%s'\n", orig);
    printf("is:  '%s'\n", cons);

    return 0;
}

The expression strchr checks whether a character is in a string. You can use it as a shortcut to spelling out all vowels in explicit comparisons.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top