a question that i can't solve

This is a discussion on a question that i can't solve within the C Programming forums, part of the General Programming Boards category; Write a program that can count the number of the alphabet from the string. Example string " Complicated Question" I ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    a question that i can't solve

    Write a program that can count the number of the alphabet from the string.
    Example string " Complicated Question"

    I need a index.

  2. #2
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Code:
    char string[50] = " Complicated Question";
    string = tolower((unsigned char)string);
    int alpha[26];
    int i;
    for (i=0; i<50; i++) {
        alpha[string[i]-97]++;
    }
    Took < 5mins. Something like that anyway. Now you have to modify that to use pointers, to improve. BTW, the number of times each character occurs is stored in alpha (u can figure out how to access each element.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    P4R4N01D: What is 32 - 97, and where in your 26 element array is that? Perhaps "isalpha" would be useful inside your loop?

    Code:
    for (i=0; i<50; i++) {
    Perhaps 50 should be replaced with the length of the string?


    --
    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.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    string = tolower((unsigned char)string);
    Also, tolower() takes a single character (as an int), not a whole string.

    Took < 5 secs to find some bugs.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    EOP
    EOP is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    Quote Originally Posted by cpjust View Post
    Code:
    string = tolower((unsigned char)string);
    Also, tolower() takes a single character (as an int), not a whole string.

    Took < 5 secs to find some bugs.
    std::transform(s.begin(), s.end(), s.begin(), tolower);

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by EOP View Post
    std::transform(s.begin(), s.end(), s.begin(), tolower);
    True, but this is the C forum

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    P4R4N01D has the right idea, it's all about ascii values. But you guys are getting bogged down with unnecessary bonus functions:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
            short int cv, i=0, total=0;
            while (argv[1][i] != 0) {	// change to lowercase
    		if ((argv[1][i] > 64) && (argv[1][i] < 91)) argv[1][i]+=32;
    		if ((argv[1][i] < 97) || (argv[1][i] > 122)) {
    			i++;		// skip non-letters
    			continue;
    		}
                    cv = argv[1][i]-96;
                    total += cv;
                    i++;
            }
            printf("%s = %d\n", argv[1],total);
    }

    gcc -o kabblah kabblah.c
    ./kabblah "Complicated Question"
    complicated question = 221
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    The OP needed this converted to use pointers not array indexing. Also this issue is very similar to strlen() and there are numerous examples on the web on how to do just that. Is there any benefit to setting tolower when you only need to check the count of letters within the string?

    Also working with the ACII table is very good when checking if it us upper case or not and doing the appropriate math to convert if necessary.

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by slingerland3g View Post
    The OP needed this converted to use pointers not array indexing. Also this issue is very similar to strlen() and there are numerous examples on the web on how to do just that. Is there any benefit to setting tolower when you only need to check the count of letters within the string?
    You should probably re-read what the OP is asking for. To count the number of times each letter of the alphabet appears in a given string.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Was the below requirment changed? As I did not catch that....

    "Write a program that can count the number of the alphabet from the string."

  11. #11
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Ah, I see the problem. Lol, if the OP has the "C Programming" book, by any chance, this is covered pretty well, if I remember correctly, in order to check if a letter of the alphabet is found and thus increments the array element.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Actually, considering that I have met quite a few people who confuse "letter" and "alphabet", it could be the case that the OP is asking how to count the number of letters in a string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by laserlight View Post
    Actually, considering that I have met quite a few people who confuse "letter" and "alphabet", it could be the case that the OP is asking how to count the number of letters in a string.
    Hmm, true, I guess it will be a moot point if the OP doesn't come back to clarify what he meant...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MK27 View Post
    P4R4N01D has the right idea, it's all about ascii values. But you guys are getting bogged down with unnecessary bonus functions:

    gcc -o kabblah kabblah.c
    ./kabblah "Complicated Question"
    complicated question = 221
    Quote Originally Posted by slingerland3g View Post
    The OP needed this converted to use pointers not array indexing. Also this issue is very similar to strlen() and there are numerous examples on the web on how to do just that. Is there any benefit to setting tolower when you only need to check the count of letters within the string?

    Also working with the ACII table is very good when checking if it us upper case or not and doing the appropriate math to convert if necessary.
    Sure, as long as you don't mind throwing portability out the window.
    If the whole world used ASCII & spoke English, there would be no more problems right?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  15. #15
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    This may not be exactly what the OP needed but though I would try to remember the solution from the book, just do not have it on hand but it went something like this:

    Usage:

    ./countLetters "count my letters"


    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv) {
    int i;
    int letters[26] = {0};
    char ch = 'a';
    
    while (*(argv[1]))
    {
     letters[(*(argv[1])++) - 'a']++;
    }
    
    printf ("Letters found are: \n");
    
    for (i = 0 ; i < 26 ; i++)
    {
      printf ("%c = %d \n", ch++, letters[i]);
    }
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help to solve My easy C programing questions!
    By hotwebs in forum C Programming
    Replies: 13
    Last Post: 12-25-2009, 02:55 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 06:53 AM

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