Counting characters

This is a discussion on Counting characters within the C Programming forums, part of the General Programming Boards category; Hi I want to know how to count the number of lower-case letters that ocurres only once in a string. ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    29

    Counting characters

    Hi

    I want to know how to count the number of lower-case letters that ocurres only once in a string.

    the string is created by the user, i don't want to count the letters from a file.

    example:
    program output: insert a string: abscdecde356YG
    the number os lower-case letters that ocurres only once is: 3(a, b and s)

    thanks, Calavera

  2. #2
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    One way to do this would be to create an array that represents all the characters in the alphabet and to initialize the contents to 0. You can then increment the contents by 1 the first time a lowercase letter is found. That way when you're done examining the string, just print out the letters that have 1 as a value. For the string itself, you could go through it in a loop and use islower() to check if the character is lowercase.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Does anyone around here ever think for themselves? Come on! Give it some thought. How do you think you'd do it? Break it into steps. I'm tired of seeing everyone just show up and say "do this for me", or "how can i...". Give up programming if you aren't going to think for yourself, because if you don't, you have no chance of ever becoming a programmer.

    Read the announcements while you're at it, so I don't have to tell you all the rest of the rules around here as a reply to your second post.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    It seems this is homework... Anyway:

    Code:
    unsigned int
    unique_lower(const char *const string)
    {
            const char      *pointer;
            unsigned int     count = 0U;
    
    
            for (pointer = string; *pointer != '\0'; pointer++)
                    if (islower(*pointer) && strchr(string, *pointer) == strrchr(string, *pointer))
                            count++;
    
            return count;
    }

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ha ha@previous post!!...the teacher will know for sure that you cheated

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    since it already has been spoiled , here is a similar code to rmps's but in function 'main()' :
    Code:
    #include <stdio.h>
    #include <ctype.h> /* for function 'islower()' */
    #include <string.h> /* for 'strchr()' and 'strrchr()' */
    
    int main()
    {
     char string[100];
     char *p;
     int count = 0;
    
     fgets(string, sizeof(string), stdin);
    
          for(p = string ; *p != '\0' ; p++)
                if(islower(*p) && (strchr(string, *p) == strrchr(string , *p)))
                    count++;
    
    
     printf("unique lower-case letters occured %d times in the string\n\n\n", count);
     system("PAUSE");
    
     return 0;
    }
    ill explain it this time hopefully you'll learn something instead of just copying\pasting the code...

    as you see... first we declare the string and the pointer 'p' we'd be using to point to the elements of the strings.

    'count' is just a counter to see how many low-case character , that didn't occur twice , are there. Its declared INT because the string wouldn't hold more than 99 chars (including the '\0') , and the alphabet letters are 26 anyway so there is now way its gonna hold more than an INT would allow. (AFAIK)

    Then it'll ask the user to input a something into that string with 'fgets()'.

    Code:
     for(p = string ; *p != '\0' ; p++)
                if(islower(*p) && (strchr(string, *p) == strrchr(string , *p)))
                    count++;
    this part would be a bit tough if you're not familiar with pointers.

    the for loop has 3 expressions. The first says that the pointer 'p' would be pointing to the first element of 'string' (string equals string[0]) , third will let the pointer 'p' move on to the next element in each loop , second will ensure that it'll keep pointing to the next element untill it reaches the end of the string wich is the string terminator '\0'.

    now for the IF statement...

    the IF statement here is divided into two expressions... first checks if the character pointed to by 'p' is lower case or not. Second uses strchr() and strrchr() functions that scans our 'string' for the first occurance of a given character. The differences between strchr() and strrchr() that the first scans the string forward and the second scans it backward.

    The reason we are comparing the result of these opposite functions is lets say we entered 'aaab' as an input, and *p was pointing to the first 'a' in the string , strchr() would quickly detect the first 'a' and return a pointer to its location , whereas strrchr() would scan the string backward and detect the third 'a' in the string wich doesn't equal the location of the first 'a' thus breaking IF and looping again.

    When it loops few times and our pointer points to 'b' in the string , strchr() will find its first occurance in string[3] and strrchr() would , too . That makes our IF statement true because 'b' is a lower-case character AND (&&) both scanning functions point to the same location in the string therefore the counter goes up by one.

    and finally the result would be printed with printf and the program terminates successfly returning 0.


    hope this helps...
    Last edited by Brain Cell; 10-01-2004 at 11:26 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 06:56 PM
  2. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  3. Help with hw problem: counting characters in a string
    By pinkfloyd4ever in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2007, 11:18 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Counting characters
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-29-2003, 12:54 PM

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