Characters in strings

This is a discussion on Characters in strings within the C++ Programming forums, part of the General Programming Boards category; Having major problems(C++ student) trying to figure the code that will report the number of instances of a character in ...

  1. #1
    Unregistered
    Guest

    Characters in strings

    Having major problems(C++ student) trying to figure the code that will report the number of instances of a character in a string.

    Using elementary code (Borland Builder5)
    The section of code has to:

    read in a string value from the keyboard
    read in a character from the keyboard
    report the number of occurrences of the character
    then replace the selected character with the asterisk character and write out the revised string.

    Any help appreciated

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    OK and what code have you tried so far? Are you using strcmp() to test for character matches? What errors are you getting when you try to parse the string?

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    2

    characters in a string

    As you will see I am in a state of confusion, I just cannot get my head round the concept of the code required. Am I being clear enough in what i am trying to do?




    AnsiString Line;
    int Index;
    int CountChar;
    char Wanted;
    char OldChar;
    char NewChar;

    Line = ReadStringPr("Enter a line of text: ");
    Wanted = ReadCharPr("Enter a character to record its occurrences: ");
    Index = 1;
    CountChar = 0;


    {
    CountChar = CountChar + 1;
    Index = Index + 1;
    WriteIntPrCr("Number of occurrences is ", CountChar);
    }
    getchar();
    return 0;
    }

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Where are the implementations for ReadStringPr(), ReadCharPr() and WriteIntPrCr()?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Just start at the beginning of the string and go through each character, testing for the key each time. If the two characters match then replace the one in the string with an asterisk and increment a counter:
    Code:
    #include <iostream>
    #include <string>
    
    int searchString ( std::string& searchIn, const char searchWith )
    {
      int occurances = 0;
      for ( int i = 0; i < searchIn.length(); i++ ) {
        if ( searchWith == searchIn[i] ) {
          occurances++;
          searchIn[i] = '*';
        }
      }
      return occurances;
    }
    
    int main()
    {
      char searchKey;
      std::string userInput;
      std::cout<<"Enter a string: ";
      std::getline ( std::cin, userInput );
      std::cout<<"Enter a character to search for: ";
      std::cin>>searchKey;
    
      std::cout<<"There were "<< searchString ( userInput, searchKey ) 
               <<" occurances of "<< searchKey <<'\n';
      std::cout<<"Modified String: "<< userInput <<'\n';
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    This is a small sidequestion to Prelude..I notice that you use std:: specific instead of using namespace std. I am curious for your reasoning behind that style? (I might mention that I haven't yet decided for a style myself)

    Code:
    #include <iostream>
    
    int main()
    {
      std::cout<<"Informative text\n";
      std::cout<<"Logical solution\n";
    
      return 0;
    }
    versus

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      cout<<"Informative text\n";
      cout<<"Logical solution\n";
    
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    2
    I seem out of my depth here, dont want to waste peoples time but do want understand the subject. The ReadStringPr etc. are as I have used them in my course so far. (Would the fact i'm on a course modified version of Borland builder have any bearing on the matter?)

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I am curious for your reasoning behind that style?
    When you open the entire namespace globally, it fills the global scope with unnecessary names. This is exactly the opposite of what namespaces were designed for, so I opted for the safety over convenience method instead of the more commonly used convenience over safety.

    In general, you want as little as possible open to the global scope. This goes for namespaces as well as variables, so it's considered good practice to specify namespace instead of open everything with using statements.

    >Would the fact i'm on a course modified version of Borland
    >builder have any bearing on the matter?
    Not if your school knows what they're doing...which they probably don't. It's best to stick with the standard I/O classes and functions instead of relying on the hacked up code that most schools seem to be so skilled at.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by steveg
    The ReadStringPr etc. are as I have used them in my course so far.
    So how did the functions work in your course? Prelude just handed you your homework on a platter. What do you not understand? You read through a string (char array) testing for instances of a predefined char and incrimenting an int to reflect the total number of instances.
    Last edited by jdinger; 06-13-2002 at 02:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 12-05-2008, 01:16 PM
  2. Replies: 4
    Last Post: 10-29-2008, 03:22 AM
  3. Check strings which should only contain some characters.
    By qingxing2005 in forum C Programming
    Replies: 2
    Last Post: 06-17-2008, 09:32 AM
  4. Converting C strings to list of characters
    By kazbo in forum C Programming
    Replies: 11
    Last Post: 02-14-2005, 09:17 AM
  5. Scanning characters & strings
    By rain_e in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 12:43 AM

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