Thread: Finding Character In String

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    Finding Character In String

    I have to allow a user to input a character. The user then inputs a string and the function is to count the amount of time the character appears in the string. I have produced a user-defined function which points to the main function. However, it's not complier is giving coding error in the 'if' function part. Can someone help with the correction. Thanks in advance.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int strFind(char ch, char *str)
    {     
          
       int i, n=0;
    
       for(i=0; i<strlen(str); i++){
                  if(str == ch){
                         n++;} 
                  else{
                         }
                }
                
     }
    
    
    
    int main()
    {
        char ch, str[100];
        
        printf("Please enter a single character: ");
        scanf("%c", &ch);
        
        printf("Please enter a string: ");
        scanf("%s", &str);
        
        strFind(ch, *str);
        printf("The number of '%c' in '%s' is %d", ch, str, n);
        
        scanf("%d");
        return 0;
        
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't compare a pointer to a character with a character. Presumably you want to compare the character pointed to by str with the character ch.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Quote Originally Posted by tabstop View Post
    You can't compare a pointer to a character with a character. Presumably you want to compare the character pointed to by str with the character ch.

    Yes, so it should be: str* == ch ?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    *str == ch, more like.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    It should be *str to deference the character itself.

    Also, I don't believe you need to use this syntax: *str or &str in the main function. The array name itself IS a pointer, so you can just say scanf("%s", str).

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Thanks.

    Theres another error which says that 'n' is undefined in the 'int main()'. How can I bring it from the user defined function?

  7. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Looks like you need to return n from your function.

    You could do it at least two ways:

    declare n in main, then pass n as a reference parameter (int *) to your function.

    or

    declare n in main, then assign n the value of your function when it returns a value. In this case, inside your function you need to declare n before using it, like you are doing right now (in both main and your function)
    Last edited by MacNilly; 03-02-2009 at 06:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  4. Replies: 3
    Last Post: 11-03-2002, 02:14 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM