Question about strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joestevens232
    New Member
    • Oct 2006
    • 43

    #1

    Question about strings

    Im writing a progrem that takes input (a sentence) from the user and I about it in the proper format. All i need now is to figure out how to get the spacing correct in the sentence. I need all the strings of two or more blank spaces should be compressed to one single space. Here is what I got so far. Any ideas on how to go about doing the spacing would be greatly appreciated. Thanks!!

    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    int main()
    {
    string str;
    cout << "Enter your sentence and then press enter. " << endl;
    getline (cin, str);
    str[0] = toupper (str[0]);
    for (int i = 1; i < str.length(); i++)
    {
    str[i] = tolower (str[i]);
    }
    cout << str << endl;

    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can do this in 1 variable but I think it is easier (and requires less copying of data to do in in 2.

    Code:
    Initialise result string to ""
    Get input string
    
    For each character in the input string
        If it is not space or the previous character was not space
            Copy character to the result string
        End If
    End For

    Comment

    • joestevens232
      New Member
      • Oct 2006
      • 43

      #3
      Originally posted by Banfa
      You can do this in 1 variable but I think it is easier (and requires less copying of data to do in in 2.

      Code:
      Initialise result string to ""
      Get input string
      
      For each character in the input string
          If it is not space or the previous character was not space
              Copy character to the result string
          End If
      End For
      ALright i tried to do what you said but I'm a begginner programmer and Im not sure how to do some of the things you said: here's what i got now:

      string result;
      string str;
      cout << "Enter your sentence and then press enter. " << endl;
      getline (cin, str);
      str[0] = toupper (str[0]);
      for (int i = 1; i < str.length(); i++)
      {
      str[i] = tolower (str[i]);
      }
      result = "";
      i = 0;
      for (int i = 0; i < str.length(); i++)
      if (str[i] != " " || str[i-1] != " ")
      return = result;

      cout << str << endl;

      Im getting errors witht hat...I don't know how to copy character to result string or enter in if not a space i just guessed to use " "....Please help

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        you have a couple of errors indicated by // ** comments
        Code:
            string result;
        string str;
        cout << "Enter your sentence and then press enter. " << endl;
        getline (cin, str);
        str[0] = toupper (str[0]);
        for (int i = 1; i < str.length(); i++)
        {
        str[i] = tolower (str[i]);
        }
        result = "";
        //i = 0;                               // ** removed
        for (int i = 0; i < str.length(); i++)
           if (str[i] != ' ' || str[i-1] != ' ') // ** replaced " with '
             return = result;
        cout << str << endl;
        you now need to replace the line
        Code:
        return = result;
        with code which adds the character from str[i] to result

        Comment

        Working...