Help with strchr function

This is a discussion on Help with strchr function within the C++ Programming forums, part of the General Programming Boards category; Code: char text[50]; char *searchPtr; char character[26]={'

Thread: Help with strchr function

'}; int count=0; int i,j; cout<<"Enter strings: \n\n"; while(cin.getline(text,50)){ for(i=0;text[i]!='

Thread: Help with strchr function

'; i++){ text[i]=tolower(text[i]); } for(i=0;i<=25;i++){ ...

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    Help with strchr function

    Code:
        
        char text[50];
        char *searchPtr;
        char character[26]={'\0'};
        int count=0;
        int i,j;
        
        cout<<"Enter strings: \n\n";
        
        while(cin.getline(text,50)){
        for(i=0;text[i]!='\0'; i++){
                text[i]=tolower(text[i]);
                }
                for(i=0;i<=25;i++){
                        for (  j = 0,  count = 0; j <= 50; j++ ) {
                                searchPtr=&text[i];
                                while(searchPtr=strchr(searchPtr,'a'+i)){
                                ++count;
                                searchPtr++;
                                }
                                }
                                character[i]=count;
                                }
                                }
                                cout<<endl<<endl;
                                
                                for(i=0;i<=25;i++){
                                        cout<<"("<<(char)('A'+i)<<" , "<<(char)('a'+i)<<") = "<<character[i]<<endl;
                                        }
                                        cout<<endl;
                                        system("pause");
                                        }
    can u help me to correct this? plz

    the counting of letters gives me wrong answer plz i need help . . .i really2x need it thanks advance


    the output should be like this . . .

    Enter strings:

    Hello World
    ^Z

    (H,h) = 1
    (E,e) = 1
    (L,l) = 3
    (O,o) = 2
    (W,w) =1
    (R,r) =1
    (D,d) =1

    can u help how to get with this output?
    if i input |hello world" the output should be like that it shouldn't be from
    (A , a) to (Z,z) it only base on the letters that entered

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "= count" is not the same as "adding one".

    EDIT: OIC, you're actually trying to count all the a, all the b, at one time. I guess that would work, although you appear to be starting at a weird place (shouldn't you always start at [0]?)

    Also, printing might be odd as Salem points out below.
    Last edited by tabstop; 08-22-2011 at 10:42 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    So what answers ARE you getting?

    I can help by reposting your code with some decent indentation.
    Code:
    int main()
    {
      char text[50];
      char *searchPtr;
      char character[26] = { '\0' };
      int count = 0;
      int i, j;
    
      cout << "Enter strings: \n\n";
    
      while (cin.getline(text, 50)) {
        for (i = 0; text[i] != '\0'; i++) {
          text[i] = tolower(text[i]);
        }
        for (i = 0; i <= 25; i++) {
          for (j = 0, count = 0; j <= 50; j++) {
            searchPtr = &text[i];
            while (searchPtr = strchr(searchPtr, 'a' + i)) {
              ++count;
              searchPtr++;
            }
          }
          character[i] = count;
        }
      }
      cout << endl << endl;
    
      for (i = 0; i <= 25; i++) {
        cout << "("
             << (char) ('A' + i)
             << " , "
             << (char) ('a' + i)
             << ") = "
             << character[i]
             << endl;
      }
      cout << endl;
      system("pause");
    }
    The only instantly odd thing I can see is using a char array to COUNT things.
    It's not that you can't, it's just that they will be output as chars (and not numbers).
    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.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Don't PM me for help.

    You need to sit down and rethink your logic.

    1. Think about the problem and what it is you want to do
    2. Come up with a solution to the problem
    3. Write psuedocode on how you would solve the problem using paper and pencil
    4. Convert your psuedocode to C
    5. Run your program and see results
    6. If you are still stuck, post your latest attempt, along with any error messages/warnings and output if you are getting something not expected.

    Additionally, getline is delimited by the newline char in your example so your while statement doesn't really do anything for you.
    As a side note, you shouldn't be using char arrays or cstring functions when you are working with C++. The C++ solution is at least 10 times easier to write.
    Last edited by AndrewHunter; 08-22-2011 at 12:02 PM. Reason: ...
    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. Question: The strchr function
    By everyone0 in forum C Programming
    Replies: 12
    Last Post: 11-16-2010, 01:35 PM
  2. the strchr function
    By everyone0 in forum C Programming
    Replies: 1
    Last Post: 11-16-2010, 01:10 PM
  3. strchr() function
    By sick in forum C Programming
    Replies: 13
    Last Post: 10-04-2008, 11:23 AM
  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