Thread: Count lowercase letter

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    58

    Count lowercase letter

    Hello. I am making program which count lowercase letters in a string. So far i have this but scanf stop after first word if string have spaces. Also I want to make that program count only letters which are not count i mean how many different lowercase letters in a string. Can somebody help to do this?
    Code:
    #include<stdio.h>
    #include<string.h>
    #include <stdlib.h>
    
    int main(){
    char s[80];
    int count=0;int i=0;
    
    printf("enter string:\n");
    scanf("%s",s);
    for( ;i<strlen(s); ++i)
    if ((int)s[i]>=97 && (int)s[i]<=122){
    count+=1;
    }
    
    printf("%d ", count, strlen(s));
    system ("pause");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    i know where you did mistake.
    the mistake is, the strlen displays the number as an ascii code.
    you cant do for loop with the strlen value.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Can you explain me more because I only starting to learn. I mean give some examples.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rac1 View Post
    i know where you did mistake.
    the mistake is, the strlen displays the number as an ascii code.
    you cant do for loop with the strlen value.
    Ummmm.... sorry, not correct.

    strlen() returns the length of a string (number of characters) as an integer.
    (Go ahead, look it up in your Library Documentation, I can wait.)

    Code:
    x = strlen("Hello");       // x = 5
    x =  strlen("old friend");   // x =10

    Of course you can use the length of a string as a condition in the for() loop.

    The reason we don't commonly do that is one of efficiency...
    Code:
    for (i = 0; i < strlen(s); i++)  // has to calculate strlen() for every iteration of the loop
      {// code }
    
    
    j = strlen(s);
    for (i = 0; i < j; i++)   // only calculates strlen once.
      {//code }
    When you get long strings... the difference becomes significant enough to affect program performance.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    Hımm, thank you, now i know true.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by krakatao View Post
    Hello. I am making program which count lowercase letters in a string. So far i have this but scanf stop after first word if string have spaces. Also I want to make that program count only letters which are not count i mean how many different lowercase letters in a string. Can somebody help to do this?
    Referring to message 1...

    First improve your indenting... seriusly, it will help you avoid a great many mistakes. ( Indent style - Wikipedia, the free encyclopedia )

    Do not use "short cuts" like you did on line 11 ... if future revisions to a program remove or alter i then your loop will misbehave.

    Do not use "magic numbers"... they will trip you up every single time. You should use the character representations instead...
    Code:
    if (s[i] >= 'A' && s[i] <= 'Z')
    (note the apostrophic quotations)

    You can use the ++ operator on line 13 ... count++;

    On line 16 you have one value defined in the printf() format string but are feeding it two variables.

    On line 17 you are calling a system function to pause your code... usually this turns out to be slow and ugly... use getchar() instead.



    Fix these problems then repost your code...

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Thanks for the tips. Here is my code now i don't know how to use getchar instead of getchar instead of system ("pause"). But still i don't know how to make program look all chars not until space and don't count same letter twice.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include <stdlib.h>
    
    int main(){
    char s[80];
    int count=0;int i=0;
    
       printf("enter string:\n");
       scanf("%s",s);
         for( i=0; i<strlen(s); ++i)
            if (s[i] >= 'a' && s[i] <= 'z'){
            count++;
    }
    
    printf("%d ", count);
    system ("pause");
    return 0;
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    As it is now, your count++ will count all lowercase letters. Which is half of your goal...

    Now you need to sit down and think about the next stage of your program (we do that a lot, btw...)

    So you can count the lower case characters...
    Now you need some means to see how many of which character is used...
    Then you need to devise a way to report only those actually used...

    Hmmmmm....

    Thing is, I'm not going to write code for you (and don't even think about asking)... this is your assignment, it's got to come from your head.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    But atleast tell how to make count all chars because when there is space it stops counting.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by krakatao View Post
    But atleast tell how to make count all chars because when there is space it stops counting.
    That is because scanf(" %s"... will only read to the first space.
    If you want to copy the entire line typed in you need to use fgets() with stdin.

    Replace your line line 10 (referring to message 7) with ...
    Code:
    fgets(s,80,stdin);
    and see how that goes.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Thank you now i will try to make count each letter only once if things will go wrong i tell.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    can you give some hints for me?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by krakatao View Post
    can you give some hints for me?
    Sure... it will involve replacing the int count variable with an array.

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    I mean give some frames or very similar program because tomorrow in the morning i have to show all my programs which i did and I have 1 program almost finished and on this one I stuck I want to sleep at night ;D

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    anyone?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 08:16 AM
  2. Lowercase letter frequency
    By pauzza in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 12:32 PM
  3. Help for letter count generating program
    By $ingh in forum C Programming
    Replies: 6
    Last Post: 06-17-2010, 02:42 PM
  4. Problems with my c letter count program
    By cram55 in forum C Programming
    Replies: 10
    Last Post: 05-30-2007, 04:10 AM
  5. Letter count
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2001, 01:23 PM