Comparing characters

This is a discussion on Comparing characters within the C Programming forums, part of the General Programming Boards category; Hello, I am currently new to both this board and to C programming. What I am attempting to do, is ...

  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,006
    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, 11:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 04: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, 05:54 PM
  5. Comparing Characters
    By luckygold6 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2003, 08:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21