trouble adding content to string

This is a discussion on trouble adding content to string within the C++ Programming forums, part of the General Programming Boards category; I am supposed to 1 space between letters and 3 spaces between words in a string (not c-style string). I ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    tx
    Posts
    8

    trouble adding content to string

    I am supposed to 1 space between letters and 3 spaces between words in a string (not c-style string). I know how to check for letters and add a space between each one. The problem is I'm not sure how to add the space between letters without accidentally erasing a letter.

    for example....

    "hey"

    would become

    "h y" from my code below.

    Any tips how I can accomplish "h e y"?


    Code:
    #include<iostream>
    #include<string>
    #include<cctype>
    using namespace std;
    
    int main()
    {
        
        
        string one = "hey you";
    
    
             //Checks for letters and inserts 1 space next to each letter until it reaches the end of the string
             for (int i=0; j < one.length(); i++)
             {
                  if (isalpha(one[i])) //if character at one[i] is an alphabet, insert a space at one[i+1]
                   one[j+1] = ' '; 
             }
    
    
        
        cout << one << endl;
      
        return 0;
        
    }
    Last edited by letsgetaway; 01-20-2008 at 11:15 AM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I would approach the problem with string methods: string.insert() for adding a blank between letters and string.replace() to change a single blank into three blanks.

    Todd

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    tx
    Posts
    8
    Ok thanks I'll give it a try. Those string library functions aren't covered well in my course's book so I had no idea how to use them. And a hello to a fellow houstonian.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    There's no need to change a single blank into three. Just insert a blank between every two characters. A single blank will automatically become three.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Good point Cornedbee!

    letsgetaway - I'm an ex-Houstonian!! Once you move to Katy, there's no going back!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    tx
    Posts
    8
    Quote Originally Posted by Todd Burch View Post
    Good point Cornedbee!

    letsgetaway - I'm an ex-Houstonian!! Once you move to Katy, there's no going back!
    LOL! I guess you have a point since they close roads to Katy every year. j/k I've actually never been there for the 2 years I've been in Houston..well more like Spring.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    You can also use strlen with a char array to acomplish a similar feat.

    Code:
    for ( int i = 0; i < strlen(word); i++ )
    {
       std::cout << word << "   "; // number of spaces beteen the quotes is the amount of 
                                                  // spaces the characters are spaced
    }
    I'm just trying to be a better person - My Name Is Earl

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    tx
    Posts
    8
    I'm still lost how to actually use those functions. This program is a simple form but actually I'm reading a line from a file one character at a time.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If you are reading a char at a time, in a loop, just append the character you read to a new string, and then append a blank to the new string, and then keep repeating your loop.

    (I went to High School in Spring - graduated 1980... before many on this forum were born).

    Todd

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Code:
    #include<iostream>
    #include<string>
    #include<cctype>   // you don't need this 
    using namespace std;
    
    int main()
    {
        
        
        string one = "hey you";
        create a second string here 
    
             //Checks for letters and inserts 1 space next to each letter until it reaches the end of the string
             for (int i=0; j < one.length(); i++) // where did "j" come from?   Use i  
             {
                  // No need to test for isalpha.  Just append the char to the new string, followed by a blank 
                  if (isalpha(one[i])) //if character at one[i] is an alphabet, insert a space at one[i+1]
                   one[j+1] = ' '; 
             }
    
    
        
        cout << one << endl;
      
        return 0;
        
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM

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