String Class

This is a discussion on String Class within the C++ Programming forums, part of the General Programming Boards category; I'm working with the String Class and I'm trying to get my program so that when you inpute a word ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    String Class

    I'm working with the String Class and I'm trying to get my program so that when you inpute a word it will outpute it in reverserse. ie. Test will cout tseT.

    I can get it to work for the one word but I need to get it to work for a variety of words. To display each of the letters I use

    First = Word.length()-1;
    Next = Word.length()-2;
    To take the certain letter in a certain position and then
    cout << Word.substr(First,Length);
    cout << Word.substr(Next,1);
    to outpute the last and next letters (t and s).

    What I am trying to do is create some sort of a loop that will first output what I have and then subtract one and then output again. I've tried a for loop, do while, and while. They didn't seem to work but I think its just cause I didn't use either of them right. I believe it would be a for loop, but if anyone can help me out and give me some clue on the loop that is all I need. I know where to place it and all.

    Thanx
    CO

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You can either use a for loop or STL reverse -

    Code:
    #include <iostream> 
    #include <string>
    #include <algorithm>	//for std::reverse
    
    using namespace std;
    
    int main()
    {
    
    	string name = "zen";
    	int nLength=name.length();
    	for(int i=nLength-1;i>=0;i--)
    		cout << name[i];
    
    	cout << endl;
    	
    
    	// ...or using std::reverse
    	reverse(name.begin(),name.end());
    
    	cout << name << endl;
    	return 0;
    
    }
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM

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