Thread: Comparing characters

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    Comparing characters

    Hello,

    I am currently new to both this board and to C programming. What I am attempting to do, is to compare a character to another one.

    This is done by:

    Code:
    char vowels[] = "aeiouyæøå"; 
    
    int isVowel(char c)
    {
      int i; 
      for(i = 0; i < strlen(vowels); i++) if(strcmp(tolower(c), vowels[i]) == 0) return 1;
      return 0; 
    }
    
    // Several other irrelevant lines of code
    // I have some lines of a song saved in char *lines[NO_LINES]
    // where NO_LINES is defined to 4. 
    // The call of isVowel from the function rem:
    
    int rem(void)
    {
      int i;
      for(i = 0; i < NO_LINES; i++)
      {
        int j; 
        for(j = 0; j < strlen(lines[i]); j++)
        {
          if(isVowel((char)lines[i][j]) == 0) printf("%c", lines[i][j]); 
        }
      }
      return 1;
    }
    I try merrily to compile this, and I get some warnings:

    Code:
    $ cc o1.c
    o1.c: In function 'isVowel':
    o1.c:17: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast
    o1.c:17: warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast
    And when I try to run the program, I get:

    Code:
    $ ./a.out remove tresmaa.txt 
    Bus error
    remove is an argument to start rem(), and lines[] is filled with 4 lines from before rem()'s call. I have tried to debug this, and it is the strcmp(tolower(c), vowels[i]) call that causes the problem. I have ensured that I have included all necessary headers, but I cannot see why I get this bus error. Thus I would be more than happy if I could get some help from you.

    Thanks in advance and best regards,

    Thuz

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    strcmp is used to compare strings; to compare characters, just compare characters.
    Code:
    char vowels[] = "aeiouyæøå"; 
    
    int isVowel(char c)
    {
       int i; 
       for ( i = 0; vowels[i] != '\0'; i++ )
       {
          if ( tolower(c) == vowels[i] )
          {
             return 1;
          }
       }
       return 0; 
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    Talking

    That worked wonders. Silly of me not to be thinking of that easy and correct solution.

    Thank you very much for your help Dave.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Comparing characters
    By Banana Man in forum C++ Programming
    Replies: 28
    Last Post: 01-13-2008, 11:11 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Comparing Characters
    By luckygold6 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2003, 08:19 PM