Like Tree1Likes
  • 1 Post By tabstop

Please help me using strchr

This is a discussion on Please help me using strchr within the C++ Programming forums, part of the General Programming Boards category; Here;'s the problem: write a program that inputs several lines of text and uses function strchr to determine the total ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    Please help me using strchr

    Here;'s the problem:

    write a program that inputs several lines of text and uses function strchr to determine the total number of occurrences of each letter of the alphabet in the text. Uppercase and lowercase letters should be counted together. Store the totals for each letter in an array, and print the values in tabular format after the totals have been determined.

    Example Output:

    enter strings:

    in the world
    Mother Nature
    (etc,,)

    A= 1
    D=1
    E=3
    M=1
    O=2
    and so on..

    please help me guys and thank you

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    Here's my code for that.please correct my codes:
    Code:
    #include<iostream>
    #include<ctype.h>
    
    using namespace std;
    
    int main()
    {
    
    char word[500];
    int cnt=0;
    char *search;
    char find='q';
    
        cout<<"Enter strings (ctrl+z to stop)"<<endl<<endl;
        
        while(gets(word)!=NULL){
    
    if(search=strchr(word,find))
    {
     cnt++;
     while(search=strchr(search+1,find))
     cnt++;
    }
    
    }
    cout<<cnt<<endl;
    
        
    system("pause");
    
    }

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. You're using a char array instead of a string.
    2. You misspelled "getline" as "gets".
    3. You need to count every letter that comes in, rather than just the letter q.
    4. That means you'll need to go through every character typed in, and check whether it is a letter or a non-letter.
    5. If it is a letter, you'll have to add to a counter for that letter.
    AndrewHunter likes this.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Also, note that 'find' can potentially have conflicts when used in C++ code; being a standard algorithm and also a member of almost everything in the std library.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by lovy poe View Post
    please help me guys and thank you
    Past tabstop's great advice, you should sit down and read CBoard's C++ Tutorials. Start at lesson 1 and work your way through. All the answers you need are there.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Same homework methinks -> Help with strchr function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    I have finish it in other way . . .
    but can u help me how to stop looping using ctrl + z

    Here's my code:


    Code:
    char text[5][80];
        char *searchPtr;
        int character[26]={'\0'};
        int count =0;
        int i,j;
        
        cout<<"Enter strings (ctrl + z) to stop: \n\n";
        
    
       for(i=0;i<=5;i++){
                           gets(&text[i][0]);
                        }
        for(i=0;i<=5;i++){
                           for(j=0;text[i][j]!='\0';j++){
                           text[i][j]=tolower(text[i][j]);
                           }
                           }
                           for(i=0;i<=25;i++){
                           for(j=0,count=0;j<=5;j++){
                           searchPtr=&text[j][0];
                           while ( searchPtr = strchr( searchPtr, 'a' + i ) ) {
                           ++count;
                           searchPtr++;
                           }
                           }
                           character[i]=count;
                           }
                                                  
                           cout<<endl<<endl;
                           for (i = 0; i <= 25; i++) {
                            cout<< "(";
                            cout<< (char) ('A' + i);
                            cout<< " , ";
                            cout<< (char) ('a' + i);
                            cout<< ") = ";
                            cout<<(int)(character[i]);
                            cout<< endl;
                            }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you plan to read until EOF, then you need to check whether the input string is still valid, rather than reading six lines come hell or high water (even though you only have room for five lines of text). So read in some text into a buffer, then process that buffer, then read in some text into a buffer, then process that buffer, then read in some text into a buffer, then process that buffer, etc until cin is bad (or, perhaps, while cin is good).

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    And then, of course there is the advice you were already given. BTW, did you make 2 accounts?

    Quote Originally Posted by tabstop View Post
    1. You're using a char array instead of a string.
    2. You misspelled "getline" as "gets".
    3. You need to count every letter that comes in, rather than just the letter q.
    4. That means you'll need to go through every character typed in, and check whether it is a letter or a non-letter.
    5. If it is a letter, you'll have to add to a counter for that letter.
    Quote Originally Posted by AndrewHunter View Post
    Past tabstop's great advice, you should sit down and read CBoard's C++ Tutorials. Start at lesson 1 and work your way through. All the answers you need are there.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the strchr function
    By everyone0 in forum C Programming
    Replies: 1
    Last Post: 11-16-2010, 01:10 PM
  2. strchr trouble
    By sababa.sababa in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 07:19 AM
  3. using strchr
    By breaka in forum C Programming
    Replies: 3
    Last Post: 08-12-2006, 12:33 PM
  4. strchr()
    By paperbox005 in forum C Programming
    Replies: 8
    Last Post: 08-08-2004, 02:29 AM
  5. strchr
    By client in forum C Programming
    Replies: 2
    Last Post: 05-12-2002, 12:41 PM

Tags for this Thread


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