C++ Output 3 letter word forward/backward

Hello all. Back yet again with another brain puzzler.
I'm having trouble with another playaround program, searched before posting, and cant find anything that really helps me with my specific problem.

NOTE: if you are willing to help me with this, then you MUST enter a year that would make you "13 to 18" years old.

More info: "If the user is a teenage (13-18) ask them to input a 3 letter word i.e. "cat"

output: "Original: "cat"
output: "Backwards: "tac"

problem: I can cout "cat" easy enough,but my backwards is a mess.

My current Output when I run the program:

Enter the year you were born:
2006
Enter your first and last name:
john doe
Hello john doe you are 15 years old.
So you are a teenager.
The first letter of your first name is: j
The last letter of your last name is: e
Enter a 3 letter word:
cat
Original: cat
Backwards:
Backwards: t
Backwards: a
Backwards: c


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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #include <iostream>
#include <string>
using namespace std;

int main()
{

 string fullname; 
 string word;
 int year;
 int cyear = 2021;
 int i;
 bool threelw;

 

    cout << "Enter the year you were born: " << endl; //user enters year of birth.
    cin >> year;

    cout << "Enter your first and last name: " << endl; 
    cin.ignore();// ignores the white space between first name and last name. 
    getline(cin, fullname); //user enters first and last name seperated by a space.
  

    cout << "Hello " << fullname << " you are " << cyear - year << " years old." << endl;

    if (cyear - year <= 12)
    {
        cout << "So you are a child." << endl; // if they are 12 or younger, they're a child
    }

    if (cyear - year >= 13 && cyear - year <= 18)
    {
        cout << "So you are a teenager." << endl; // if they are 13 - 18 years old, they're a teenager
    }

    if (cyear - year > 18)
    {
    cout << "So you are an adult." << endl; // if they're older than 18, they're an adult
    }

    fullname.at(0) = tolower(fullname.at(0));

    cout << "The first letter of your first name is: "; // outputs the first letter of first name. 
    cout << fullname[0] << endl;

    cout << "The last letter of your last name is: "; // outputs the last letter of last name. 
    cout << fullname[fullname.length() - 1] << endl;

    if (cyear - year >= 13 && cyear - year <=18) // if the user is a teenager, have them input a 3 letter word.
    {
        cout << "Enter a 3 letter word: " << endl; // input of 3 letter word.
        cin >> word;
    }
    if (word.length() != 3)
    {
        cout << "You entered a word that is less than 3 letters." << endl; //if the word is less than 3, output error message.
        threelw= false;
    }
    if (word.length() > 3)
    {
        cout << "You entered a word that is more than 3 letters." << endl; //if the word is more than 3, output error message.
        threelw = false;
    }
    if (word.length() == 3) // if the word length is 3, output the original word, followed by the word backwards. 
    {
        cout << "Original: " << word << endl; 
    }
    for (int i = word.length(); i >= 0; --i)
    {      
        cout << "Backwards: " << word[i] << endl;
    }
  











}
1
2
3
4
5
6
7
8
9
 for (int i = word.length(); i >= 0; --i)
    {      
        cout << "Backwards: " << word[i] << endl;
    }
  
change to:
cout << "Backwards: " ;

for (int i = word.length(); i >= 0; --i)
    {      
        cout << word[i];
    }
cout << endl;


you can also try https://www.cplusplus.com/reference/algorithm/reverse/ or similar tricks to get c++ to do the reverse for you.
Last edited on
@ jonnin

always the MVP. Thanks a lot.
Always something so minuscule that drives me up the wall.
Using iterators makes going forwards and backwards through a std::string (or other C++ containers like a std::vector) with a for loop easy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

int main()
{
   std::string cat { "cat" };

   // https://en.cppreference.com/w/cpp/string/basic_string/begin
   for (auto itr { cat.cbegin() }; itr != cat.cend(); ++itr)
   {
      std::cout << *itr << ' ';
   }
   std::cout << '\n';

   // https://en.cppreference.com/w/cpp/string/basic_string/rbegin
   for (auto itr { cat.crbegin() }; itr != cat.crend(); ++itr)
   {
      std::cout << *itr << ' ';
   }
   std::cout << '\n';
}
c a t
t a c
Last edited on
C++11 introduced the range-based for loop. C++20 allows the range-based for loop to work in reverse:
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
#include <iostream>
#include <string>
#include <ranges>

int main()
{
   std::string cat { "cat" };

   // https://en.cppreference.com/w/cpp/language/range-for
   for (const auto& itr : cat)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';

   // https://www.fluentcpp.com/2020/02/11/reverse-for-loops-in-cpp/
   for (const auto& itr : cat | std::views::reverse)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';

   // range-based for loops work with regular arrays as well
   int arr[] { 5, 10, 15, 20, 25 };

   for (const auto& itr : arr)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';

   for (const auto& itr : arr | std::views::reverse)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';
}
c a t
t a c
5 10 15 20 25
25 20 15 10 5
Registered users can post here. Sign in or register to post.