I want this to reverse my string order by words. Like if a string is "Cat is running" then it should be "running is Cat". Here is the code:
#include<iostream>
#include<string>
using namespace std;
void reverseString(string str);
int length, lastLength;
int main() {
string s;
cout << "Enter a string to reverse its words: ";
getline(cin, s);
lastLength = s.length() - 1;
length = lastLength;
cout << "\nThe string in reverse order is ";
cout << endl;
}
void reverseString(string str) {
if (length < 0)
return;
else {
if (str.at[length] == " " || length == 0)
{
if (length == 0)
length = -1;
for (int i = length + 1; i < lastLength; i++)
cout << str.at[length];
lastLength = length - 1;
}
length--;
reverseString(str);
}
}
It shows some error of pointer and array. I don't know how to resolve this. Any help will be truly appreciated! :)
You have two different errors. .at
is a method so it should be called as .at()
not .at[]
. Second, you compare char
to string
(" "). So, you should replace " " with ' '.
#include<iostream>
#include<string>
using namespace std;
void reverseString(string str);
int length, lastLength;
int main() {
string s;
cout << "Enter a string to reverse its words: ";
getline(cin, s);
lastLength = s.length() - 1;
length = lastLength;
cout << "\nThe string in reverse order is ";
cout << endl;
}
void reverseString(string str) {
if (length < 0)
return;
else {
if (str.at(length) == ' ' || length == 0) // <- note the changes here
{
if (length == 0)
length = -1;
for (int i = length + 1; i < lastLength; i++)
cout << str.at(length); // <- note the changes here
lastLength = length - 1;
}
length--;
reverseString(str);
}
}
I didn't check the logic. You may continue working on the logic :)
std::string
has many helper functions, such as string::find
, string::rfind
, and std::substr
which you can use to manipulate the string, instead of accessing the characters individually. For example:
void reverseString(std::string str, size_t end)
{
size_t pos = str.rfind(' ', end);
if (pos == std::string::npos)
{
cout << str.substr(0, end + 1) << endl;
}
else
{
cout << str.substr(pos + 1, end - pos) << endl;
reverseString(str, pos - 1);
}
}
int main()
{
std::string s = "Enter a string to reverse its words";
cout << s << endl;
reverseString(s, s.length());
}
Here's a version that attempts to preserve the logic in your solution, with only a little C++ <string>
convenience:
void output_reverse_string(string str, int last, int current) {
/* Terminating condition: we've exhausted the input: */
if (current == 0) {
std::cout << str.substr(current, 1 + last - current);
return;
}
/* Recurse until we've found a space: */
if (str.at(current) != ' ') {
output_reverse_string(str, last, current - 1);
return;
}
/* Since we've found a space, output the following word: */
std::cout << str.substr(current + 1, last - current);
/* Just for readability, can be skipped: */
std::cout << " ";
/* Recurse on the *remaining* string contents: */
output_reverse_string(str, current - 1, current - 1);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.