#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

char your_string[256];
char new_string[256];
void ReverseString();

int main()
{
	char choice;
	do
	{
cout <<"Enter the string that you want to reverse: \n";
cin.getline(your_string, 256);
ReverseString();
cout << new_string << "\n";
cout <<"Do you want to continue? Press e to exit: ";
	cin>>choice;
	}
	while (choice != 'e');
return 0;
}

void ReverseString()
{
int x = strlen(your_string)-1;
for(int y = x; y >= 0; y--)
{
 new_string[x-y] = your_string[y];
}
}

I need the program to repeat it self. thanks

Recommended Answers

All 7 Replies

Well, your do-while loop is absolutely fine.
The problem you are suffering is that:
1. You are not initializing the array new_string when you are using it. Add a for(int i=0;i<256;(new_string[i]=0),i++); before the int x = strlen(your_string)-1; in definition of ReverseString().
2. You are not flushing the input stream. Add a cin.ignore(1000,'\n'); after cin>>choice Thats all,
Try not to use global variables. Use the code-tags to post the code.

commented: Nice work. thanks very much for your help +1

A couple other comments.

By declaring the strings in global space, they get set to all 0's. When you reverse the string, you get a valid string because there are NULL terminators in the destination. If you declared the strings inside main and passed them as arguments to the function (which is a better way to set things up) you'd find the destination string is not terminated - it will display the reversed string and lots of other junk until a byte with value 0 is encountered. Try it!

Also, without resetting the destination string, as siddhant suggests, a second input, smaller than your first, will result in leftover content.

Really, you don't need to reset the destination string - when creating string functions, you need to be sure to place a NULL terminator - character '\0' - after the last valid character of your new content. The correct position would at strlen( source_string ) - strlen is both the number of characters in a string, and the index of the NULL terminator.

Your prompt for doing another string should give the user a yes/no choice. As it reads, you either enter 'e' to quit or ....... ?
Better to ask "Do another or exit ( y or e ). That way the user knows to enter something if they want to continue.

commented: Good points +2

Vmanes I dont know exactly what u mean. Can u show me the code or explain it thanks.

Well, your do-while loop is absolutely fine.
The problem you are suffering is that:
1. You are not initializing the array new_string when you are using it. Add a for(int i=0;i<256;(new_string[i]=0),i++); before the int x = strlen(your_string)-1; in definition of ReverseString().
2. You are not flushing the input stream. Add a cin.ignore(1000,'\n'); after cin>>choice Thats all,
Try not to use global variables. Use the code-tags to post the code.

Thanks siddhant3s. I got it working but can you or someone else explain it to me please.

Alternatively to cin.ignore(...) and such tactics for consuming a leftover newline, you could use getline to read choice, thus not leaving any newlines in the input buffer.

First let us work about flushing the input stream. Well, when you cin>>choice The user press some character(like 'a' or 'e' or 'm') and the enter key(which means '\n'). So what happens is, that the character gets stored in the variable called choice but the '\n' still remains in the stream. So whenever the next input is required, that '\n' gets inputed there.
You can think like this: The input from keyboard is taken from the user and stored in a buffer( which can be thought of as a temporary storage ) . That means whatever the user types get stored first in buffer. Then from the buffer, it is passed on to your variable considering the capacity of the variable. i.e If you have cin a char variable c_var , and the buffer contains "Hello" then only H will be copied to c_var. And the rest of the input will still remain in buffer. If now, you issue another input statement(like getline() or whatever) all the left over characters("ello") gets passed to it.
Now, what is happening in your case is, that when the user enters a character and presses enter, the character gets stored in the choice, but the next character (which is '\n') get passed over. This is automatically get poured into the next input statement encountered .( which in your case is cin.getline(your_string, 256); )
Hence you never get a chance to Enter the string the second time.
Now what cin.ignore does is that it removes all the character from the stream till the number (which is passed in its argument) of characters and ignore the rests.
Read about cin.ignore() yourself now.

As far as my second point is concerned, it just say that when you were reversing the string second time, you didn't initialized the target string to zero.
Say you first passed "Hello" to the ReverseString() function. Now the new_string has the value "olleH" which you output to user.Good till now. Now see what happen if u ran the funtion second time with the string as "WTF" the new_string will now contain "FTWeH". Do you realize why that eH is still there? Guess it yourself!!

As a lesson in the need for strings to be properly terminated, play with this code a bit. Note how displaying bad_str can show a lot of junk as well as whatever was assigned to it when there's no proper termination of the string. Most string function will do this for you (except strncpy), but if you manipulate the string, as happens at lines 54-56, you can end up again with bad output.

//demonstrating C-string termination
//global string initialized to all NULL
//good local string initialized to all NULL
//bad local string not initialized
//Note how the strcpy functions ensure a NULL placed at end

#include <iostream>
#include <cstring>
using namespace std;

void my_strcpy(char dest[], const char source[]);
void my_strcpy2(char dest[], const char source[]);

char g_str[256];  //string declared in global space

int main()
{
   char good_str[256] = "";
   char bad_str[256];
   char the_end[] = "the end";
   int i;
   int len;

   //see initial states of strings
   cout << "global string: " << g_str << endl;
   cout << "good local string: " << good_str << endl;
   cout << "bad local string: " << bad_str << endl;

   //enter some text, note how bad string will still have junk at end
   cout << "Enter text for global string: " ;
   cin.getline( g_str, 256 );
   cout << "Enter text for local good string: " ;
   cin.getline( good_str, 256 );
   cout << "Enter text for bad local string: " ;
   cin.getline( bad_str, 256 );

   cout << "global string: " << g_str << endl;
   cout << "good local string: " << good_str << endl;
   cout << "bad local string: " << bad_str << endl;

   //tack some text to end of each string, overwriting NULL that 
   //getline inserted.  global and good local strings still OK
   cout << "Adding \"the end\" to the end of each string, the hard way." 
      << endl;

   len = (int) strlen( g_str );
   for( i = 0; i < 7; i++ )
      g_str[len+i] = the_end[i];

   len = (int) strlen( good_str );
   for( i = 0; i < 7; i++ )
      good_str[len+i] = the_end[i];

   len = (int) strlen( bad_str );
   for( i = 0; i < 7; i++ )
      bad_str[len+i] = the_end[i];

   cout << "global string: " << g_str << endl;
   cout << "good local string: " << good_str << endl;
   cout << "bad local string: " << bad_str << endl;

   //Using strcpy functions which correctly place NULL at end of destination
   my_strcpy( good_str, g_str );
   my_strcpy2( bad_str, g_str );

   cout << "global string: " << g_str << endl;
   cout << "good local string: " << good_str << endl;
   cout << "bad local string: " << bad_str << endl;

   return 0;
}

void my_strcpy(char dest[], const char source[])
{
   int len = (int) strlen( source );
   int i;

   for( i = 0; i < len; i++ )
      dest[i] = source[i];

   dest[i] = '\0' ; //must terminate copy
}

void my_strcpy2(char dest[], const char source[])
{
   int pos = 0;

   do
      dest[pos] = source[pos];
   while ( source[pos++] != '\0' );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.