Thread: reversing a character string

  1. #16
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ah now you've provided code so its ok for us to give you some
    Code:
    for (index=0; string1[index] != '\0'; index++)
      ; // Empty body.  We are just trying to get index to be at the null character
    
    for (--index; // index was pointing at the null, we need to go to the previous character
        index >= 0; // when index == 0 we are at the first character
        index--) // step back to the previous character
      cout << string1[index] << endl;

  2. #17
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    So close....

    I haven't done it as a function yet, I usually try t get it working w/o using a function and then do the function...but I don't know why my output is doing funky things. I got it to output the word backwards...but it also does a bunch of weird characters. Another problem is if I type in a 5 letter word like 'angel' and then a 3 letter word like 'fie' it comes out eifna it is not clearing the memory after each run through. I thought the endl; cleared the buffer ? The last problem is in trying to break the WHILE loop. I have tried setting it to != '1' and it yells at me, I tried != 'x' and it errored...so for now I have left it at !='\0' but theres no way to escape the program. I am sorry for being such a bother. I can't even sleep because I have code swimming through my brain and until I get this fixed it is relentless....is that sad or what?


    Output 2



    Code:
    //Heather Conn
    //Program 7
    //This program will read in a word of 1-10 letters and output the word reversed
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    //Declare std
    using std::cout;
    using std::cin;
    using std::endl;
    using std::ios;
    using std::cerr;
    using std::ofstream;
    
    
    //Start the program
    int main ()
    {
    
    //Declare the variables
    	char string1[10];
    	int index;
    	int idx;
    	char reverse[10];
    	
    
    //Open the file for output
    	ofstream data_out ("output.txt", ios::out);
    
    //Error routine if file can not be created
    	if (!data_out) {
    		cerr << "File could not be opened" << endl;
    		exit (1);
    	}
    
    //Describe program to user
    	cout << "This progam will output any word consisting of 1-10 characters in reverse." << endl;
    	data_out << "This progam will output any word consisting of 1-10 characters in reverse." << endl;
    
    //WHILE loop to loop program--This is not working but I am having trouble finding a value to end the loop
    	while (string1 != "\0"){
    
    //Get word and output to file
    		cout << "\nPlease enter a word 1-10 characters in length: ";
    		cin >> string1;
    		data_out << "\nThe word is: " << string1 << endl;
    
    //Initialize idx
    		idx=0;
    	
    //Set up loop
    			for (index=0; string1[index] != '\0'; index++);	//starts at 0 and counts up till it finds '\0'
    		
    			for (--index; index >= 0; index--) {
    				reverse[idx] = string1[index];
    				++idx;
    				
    			}
    			
    //Output reversed text
    				cout << "The word reversed is: " << reverse << endl;
    				data_out << "\nThe word reversed is: " << reverse << endl;
    
    //End WHILE
    	}
    
    //End program
    		return 0;
    }
    Last edited by HConn; 11-15-2004 at 05:08 AM. Reason: to add link to my output

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The wierd characters are because you didn't add a null to the end of the string. Remember a string by definition is a series of characters terminated with a null.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In order for the strings to have 10 possible visible char and still have the null terminating char, each of these need to be 11 elements long, not 10.

    char string1[10];
    char reverse[10];

    When you get done reading the char of string1 into reverse, you need to give it a null char to make reverse a string, not just a char array.
    Code:
    for (--index; index >= 0; index--) 
    {
      reverse[idx] = string1[index];
    				  ++idx;
    }
    //reverse[idx] is now the first available "empty" index in reverse.
    reverse[idx] = '\0';

  5. #20
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    *lightbulb* I forgot that since there was no carriage return associated with reverse that it wouldn't automatically have a null at the end.

    Yall thanks for the help...strings/arrays are making a little more sense now. I don't know why this particular concept is so difficult for me, but at least now I can say I don't fall into the "as clear as mud" category.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM