string declaration and cout function

Hi.
I'm trying to reverse a string which read from the user, and print the reversed string.
I have two issues: How to declare unknown size of the reversed string?
Why "cout" command is printing only the first character of the string?

[code]
#include <iostream>
#include <string>
using namespace std;

int main()
{

string org_word;
string back_word[100];
int i, len;
getline (cin, org_word);

len = org_word.length();

for (i = 0; i < len; i++)
back_word[i] = org_word[len-i - 1];



cout << "len = " << len << endl;
cout << "i = " << i << endl;
cout << back_word<< endl;

system("pause");
return 0;
}
Last edited on
closed account (E0p9LyTq)
string back_word[100]; creates an array with 100 strings. You really don't want that.

How to declare unknown size of the reversed string?

by resizing your reversed string with the known size (length) of your original string after getting it from the user.

Depending on how your create your reversed string you don't need to resize the string.
You are confusing strings and arrays. You do not need 100 strings. Just two: org_word and back_word.

BTW, I would also get rid of the habit of using abbreviated names like that. Type stuff out:

1
2
3
4
int main()
{
  string forward_string;
  string reversed_string;


For your exercise there are quite a few different things you can do. Usually professors expect someone to reverse a string “in-place”, meaning you modify the input string.

But it is perfectly fine to do it with two stings too.

Your compiler should have complained at you, at the very least on line 22. Fix line 9 to not be an array and that problem should be fixed.


The more pressing problem is that you need to think a little more about the algorithm. I recommend you get a piece of paper and a pencil and practice making it work, yes, by hand, with a string that is three characters long and then with a string that is four characters long.

You must be careful of bounds: do not try to index characters outside the string (< 0 or >= len)


Hope this helps.
Last edited on
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
   std::cout << "Enter a string: ";
   std::string original_string;
   std::getline(std::cin, original_string);

   std::cout << "Size (length) of original string: " << original_string.size() << '\n';

   // if you can use a C++ algorithm std::reverse works on strings
   std::string reverse_string1 = original_string;

   std::reverse(reverse_string1.begin(), reverse_string1.end());

   std::cout << "\nReversed string (using std::algorithm):\n";
   std::cout << reverse_string1 << '\t' << reverse_string1.length() <<'\n';

   // using std::string iterators to reverse loop through a string
   // concatenate to the reverse string using "+"
   std::string reverse_string2;

   for (auto itr = original_string.rbegin(); itr != original_string.rend(); itr++)
   {
      reverse_string2 += *itr;
   }

   std::cout << "\nReversed string (using iterators):\n";
   std::cout << reverse_string2 << '\t' << reverse_string2.size() << '\n';

   // reversing a string using a brute force method
   std::string reverse_string3;

   for (int i = original_string.size() - 1; i >= 0; i--)
   {
      reverse_string3 += original_string[i];
   }

   std::cout << "\nReversed string (brute force):\n";
   std::cout << reverse_string3 << '\t' << reverse_string3.size() << '\n';
}
Enter a string: This is a test
Size (length) of original string: 14

Reversed string (using std::algorithm):
tset a si sihT  14

Reversed string (using iterators):
tset a si sihT  14

Reversed string (brute force):
tset a si sihT  14
Last edited on
Topic archived. No new replies allowed.