Take in 'x' amount of a string and reverse it

Hello, folks. The main objective I am trying to accomplish is taking in a user string and saving the reverse to it. I also must take in an 'x' amount of strings from the user.

Example:
1
2
3
4
5
6
7
8
9
10
How many strings do you want to create?: 2
Enter the strings:

ABCDE
FGHIJK

The reverse of the strings you entered is:

EDCBA
KJIHGF


And obviously if they were to put 6 for the amount of strings then it would go like:

ABC
DEF
GHI
JKL
MNO
PQR

and return the reverse.. you see what Im saying. Here's the doozy. I can only use arrays, loops and c++ strings. So this means, no vectors, pointers c-strings (can't do char c[50]) as well as no c++ string iterators, so this means, no using the reverse function or swap or c.end or c.begin or whatever else there is. I can use .append and .size and what not.

I had an idea of doing something along the lines of
1
2
3
4
5
  // to get 'x' amount of strings and store in variable
  for (int i = 0; i < numOfLines; ++i)
  {
     getline(cin, str);
  }


Since I can't use vectors or pointers or anything, I also thought about just adding the strings together? to get all the inputs into one variable but I'm not sure how to make it so it also adds the new line tab so instead of doing

enter string:
hello
world

// "helloworld"

/* hello
world */

I want it to save to a variable like the 2nd one.
All help is appreciated, like I said, I'm super limited to what I can use.
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
#include <iostream>
#include <string>
#include <iomanip>

std::string reverse( std::string str )
{
    // return a string constructed using reverse iterators
    // https://en.cppreference.com/w/cpp/string/basic_string/rbegin
    return { str.rbegin(), str.rend() } ;
}

int main()
{
    std::string full_string ; // holds the concatenation of all the input strings

    std::cout << "enter strings (enter an empty string to end input)\n" ;
    std::string str ;

    // for each non-empty string str that is read in
    while( std::cout << "? " && std::getline( std::cin, str ) && !str.empty() ) 
        full_string += str ; // append str to full_string

    std::cout << "concatenated strings: " << std::quoted(full_string) << '\n'
              << "     reverse of that: " << std::quoted( reverse(full_string) ) << '\n' ;
}
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
#include <iostream>
#include <string>

std::string reverse_string(const std::string&);

int main()
{
   const int num_str = 4;

   std::string str_arr[num_str] { "Hello World!", "Good-bye World",
                                  "What's up?", "Another string" };

   for (int loop = 0; loop < num_str; loop++)
   {
      std::cout << str_arr[loop] << '\n';
   }
   std::cout << '\n';

   std::string rev_str_arr[num_str];

   for (int loop = 0; loop < num_str; loop++)
   {
      rev_str_arr[loop] = reverse_string(str_arr[loop]);
      std::cout << rev_str_arr[loop] << '\n';
   }
}

std::string reverse_string(const std::string& str)
{
   std::string rev_str { };

   for (int loop = str.size() - 1; loop >= 0; loop--)
   {
      rev_str += str.at(loop);
   }

   return rev_str;
}

Hello World!
Good-bye World
What's up?
Another string

!dlroW olleH
dlroW eyb-dooG
?pu s'tahW
gnirts rehtonA
@JLBorges, he stated he can't use iterators.
> I can only use arrays, loops and c++ strings. So this means, no vectors, pointers c-strings (can't do char c[50])
> as well as no c++ string iterators,
> so this means, no using the reverse function or swap or c.end or c.begin or whatever else there is.
> I can use .append and .size and what not.

Ah! Rewritten using just append and size:

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
#include <iostream>
#include <string>
#include <iomanip>

std::string reverse( std::string str )
{
    std::string rev ;

    if( str.size() == 0 ) return rev ; // empty string ie. if( str.empty() )
    
    // std::ptrdiff_t is used for array indexing, if negative values are possible.
    // https://en.cppreference.com/w/cpp/types/ptrdiff_t
    // note: std::string::difference_type is the same as std::ptrdiff_t.
    for( std::ptrdiff_t i = str.size() - 1 ; i >= 0 ; --i )
        rev.append( 1, str[i] ) ; // append 1 character str[i] to rev

    return rev ;
}

int main()
{
    std::string full_string ;

    std::cout << "enter strings (enter an empty string to end input)\n" ;
    std::string str ;
    while( std::cout << "? " && std::getline( std::cin, str ) && str.size() != 0 )
        full_string.append(str) ; // append contents of str to the full_string

    std::cout << "concatenated strings: " << std::quoted(full_string) << '\n'
              << "     reverse of that: " << std::quoted( reverse(full_string) ) << '\n' ;
}
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
#include <iostream>
#include <string>
using namespace std;

string backwards( const string &str )
{
   int N = str.size();
   string result( N, ' ' );
   for ( int p = 0, q = N - 1; p < N; p++, q-- ) result[p] = str[q];
   return result;
}

int main()
{
   int n;
   string str, result;
   cout << "How many strings do you want to create? ";   cin >> n;   cin.ignore( 1000, '\n' );
   cout << "Enter the strings, one per line:\n";
   for ( int i = 0; i < n; i++ )
   {
      getline( cin, str );
      result += backwards( str ) + '\n';
   }
   cout << '\n' + result;
}


How many strings do you want to create? 2
Enter the strings, one per line:
abcde
fghij

edcba
jihgf

Topic archived. No new replies allowed.