Reputation: 13
I have just started learning c++ and I was trying to create a simple program using functions to reverse a string but this was my attempts and it doesn't appear to be working. It just outputs the string the right way around. Just to clarify I am not looking for an inbuilt function or something like that that does it for me but where the errors are in my logic. If there is a function for it though I would be interested to know. Thanks for any help.
#include <iostream>
#include <string>
// A program to practice creating and calling a function to reverse a string
using namespace std;
//declaring the function before use
string reverseString(const string& input,string& output);
//main function
int main()
{
string userInputStr;
cout << "Please enter a string to be reversed: ";
getline(cin,userInputStr);
string reversedString = userInputStr;
reverseString(userInputStr, reversedString);
cout << userInputStr << " reversed is " << reversedString;
return 0;
}
string reverseString(const string& input, string& output)
{
char characterInString;
int counter;
int lengthOfString = input.length();
for (counter = 0; counter >= lengthOfString; counter++)
{
characterInString = input[(lengthOfString - counter)];
output = output + characterInString;
}
return output;
}
Thanks for everyone who helped. For those who are interested the updated code that I have managed to fix is below.I know there are simpler ways of doing it which I am looking into but I have only been learning for a couple of days so this is just using what I currently understand.
#include <iostream>
#include <string>
// A program to practice creating and calling a function to reverse a string
using namespace std;
//declaring the function before use
string reverseString(const string& input,string& output);
//main function
int main()
{
string userInputStr;
cout << "Please enter a string to be reversed: ";
getline(cin,userInputStr);
string reversedString = "";
reverseString(userInputStr, reversedString);
cout << userInputStr << " reversed is " << reversedString << endl;
return 0;
}
string reverseString(const string& input, string& output)
{
char characterInString;
int counter;
int lengthOfString = input.length();
for (counter = 0; counter < lengthOfString +1; counter++)
{
characterInString = input[(lengthOfString - counter)];
output = output + characterInString;
}
return output;
}
Upvotes: 1
Views: 261
Reputation: 310950
In this loop
for (counter = 0; counter >= lengthOfString; counter++)
{
characterInString = input[(lengthOfString - counter)];
output = output + characterInString;
}
there are several errors.
The first one is the loop will never iterate except the case when string input is empty and consequently its length is equal to zero. Only in this case condition
counter >= lengthOfString
will be evaluated to true.
The second problem is that index evaluated like
(lengthOfString - counter)
will be outside the valid range of indices when counter is equal to 0.
And at last you already initialized output with input string when called this function
string reversedString = userInputStr;
reverseString(userInputStr, reversedString);
It would be better to declare the function the following way
string reverseString( const string& input );
and it could be defined like
string reverseString( const string& input )
{
return std::string( input.rbegin(), input.rend() );
}
In main the function would be called like
string reversedString = reverseString(userInputStr );
If you want to write the function without using the reverse iterators then you could write it like
string reverseString( const string& input )
{
std::string output;
auto n = input.size();
output.reserve( n );
while ( n != 0 )
{
output += input[--n];
}
return output;
}
Upvotes: 0
Reputation: 1918
for (counter = 0; counter >= lengthOfString; counter++)
This creates a loop which runs when counter is greater than or equal to the length of the string which, as counter initially equals 0, means that this never runs.
The reason that making counter < lengthOfString
won't work for you either is because .length()
returns the number of characters in the string. Therefore a string of 5 characters returns 5, with valid indexes of 0-4. So you need to start looking at lengthOfString-counter-1
to ensure that when counter=0
you are truly looking at the last character.
You actually may be copying the NULL character at the end of the string with your current code, which would mean that the first character of your return string is \0
and that is why the string looks empty.
Upvotes: 0
Reputation: 2309
Your for loop must start from 1 :
for (counter = 1; counter <= lengthOfString; counter++)
{
characterInString = input[(lengthOfString - counter)];
output = output + characterInString;
}
Also you had the wrong character there : '>' instead of '<'. I also don't know why you set the output string equal to the input string?
#include <iostream>
#include <string>
// A program to practice creating and calling a function to reverse a string
using namespace std;
//declaring the function before use
string reverseString(const string& input,string& output);
//main function
int main()
{
string userInputStr;
cout << "Please enter a string to be reversed: ";
getline(cin,userInputStr);
string reversedString = "";
reverseString(userInputStr,reversedString);
cout << userInputStr << " reversed is " << reversedString;
return 0;
}
string reverseString(const string& input, string& output)
{
char characterInString;
int counter;
int lengthOfString = input.length();
for (counter = 1; counter <= lengthOfString; counter++)
{
characterInString = input[(lengthOfString - counter)];
output = output + characterInString;
}
return output;
}
Here's ideone to provide results : http://ideone.com/47A3y1
Upvotes: 0
Reputation: 14705
1 - The output
contains text when you start your reversal. You probably want to access the elements in output
along the lines of
output[counter] = input[opposite];
in addition to fixing the loop condition.
2 - a reasonable way to implement your function using the string type could be.
std::string reverse(const std::string& in){
return std::string(in.rbegin(), in.rend());
}
Upvotes: 0
Reputation: 227390
Learning C++ is also about learning what the language and standard library can do for you. For example, learning that a string can be built from two iterators, and that reverse iterators exist:
string reversedString(userInputStr.rbegin(), userIntput.rend());
Upvotes: 1