C++: Writing backwards the content of a string, output is not the expected

I want to write a script that asks for the user to write a single word (stored as a string) and the output must be the same word written backwards, if the word is a palindrome (the word is the same written forward and backwards) the user will see the word written backwards and the message that such word is a palindrome or not. So far my code is stalled at writing backwards the original string since it doesn't make sense to compare the original and backwards strings if the output of the backwards string is not the expected. So here is my code and at the there's a link for an online compiler that shows the output.

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
 //Programa que determina si una palabra es palindroma

#include <iostream>
#include <string>

using namespace std;

int main()
{
	char palindro[16], reves[16];//original & backwards string
	
	cout << "Ingrese una palabra para determinar si es palindroma: ";
	cin >> palindro;
	
	int tam = sizeof(palindro);//setting the size of the original string
	
	for (int i = 0; i < tam; i++)
	{
		reves[i] = palindro[tam-i];//tam-i for writting backwards
	}
	
	cout << "La palabra al reves es: " << reves;//backwards output
	
	return 0;
	
}


Edit_1: At the 19th line where you can read palindro[tam-i] I'm following a proven (at least by myself) logic for writing the contents of a vector backwards.

http://goo.gl/0xdoRN
Last edited on
sizeof palindro is not the length of the string.
closed account (E0p9LyTq)
Several changes need to be made:
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
//Programa que determina si una palabra es palindroma

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
   char palindro[16], reves[16];//original & backwards string

   cout << "Ingrese una palabra para determinar si es palindroma: ";
   cin >> palindro;

   int tam = strlen(palindro);//setting the size of the original string
   
   for (int i = 0; i < tam; i++)
   {
      reves[i] = palindro[tam-i-1];//tam-i for writting backwards
   }

   reves[tam] = '\0'; // have to null terminate the string, as the original was
   
   cout << "La palabra al reves es: " << reves << endl;//backwards output

   return 0;
}
closed account (E0p9LyTq)
C++ strings take care of many of the details, such as knowing the size of the string and keeping track of the end of the string:

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

int main()
{
   std::string original = "";
   std::string reversed = "";
   
   std::cout << "Enter a single word string to reverse: ";
   std::cin >> original;

   for (int i = original.size(); i >= 0; i--)
   {
      reversed += original[i];
   }

   std::cout << "\nThe reversed string is: " << reversed << std::endl;

   return 0;
}


Change std::cin >> original; to std::getline(std::cin, original); and now you can reverse entire sentences, not single words.
closed account (E0p9LyTq)
The C++ library has several algorithms for reversing containers, std::reverse and std::reverse_copy.

Your program using reverse_copy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Programa que determina si una palabra es palindroma

#include <iostream>
#include <cstring>
#include <algorithm>

int main()
{
   char palindro[16], reves[16];//original & backwards string

   std::cout << "Ingrese una palabra para determinar si es palindroma: ";
   std::cin >> palindro;

   int tam = strlen(palindro);//setting the size of the original string
   
   std::reverse_copy(palindro, palindro + tam, reves);

   reves[tam] = '\0'; // have to null terminate the string, as the original was
   
   std::cout << "La palabra al reves es: " << reves << std::endl;//backwards output

   return 0;
}
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
//Programa que determina si una palabra es palindroma

#include <iostream>
#include <string>

using namespace std;

int main()
{
    const int size = 16;
    char palindro[size];
//  char reves[size];//original & backwards string
    
//    cin >> palindro;
    
//    int tam = sizeof(palindro);//setting the size of the original string
    cout << "Ingrese una palabra para determinar si es palindroma: ";
    for (int i = 0; i <= '\0'; i++)
    {
        cin.get(palindro,size);//tam-i for writting backwards
    }
    cout << endl;
    cout << "La palabra al reves es: ";//backwards output
    for(int i = size - 1; i >= 0; i--){
        cout << palindro[i];
    }
    cout << endl;

    return 0;
    
}


The above code will give you the targeted output, but try to understand what the first and second array is doing.

Also if you're trying to set two array sizes to the same, you can just simply set both of them to a const size, so it eliminates the need to set two.

The first array is setting each string literal to an element until the null terminator.

The second array is simply outputting the string backwards.

Edit: I commented out code you don't need essentially.
Last edited on
@FurryGuy: wow! Thanks a lot for showing me the different ways on how to make this task.

1st version
I simply can not believe that I have been using the wrong library, it should be obvious for me since I was trying the strlen() command and my compiler showed the message:
palindromo.cpp:15:27: error: ‘strlen’ was not declared in this scope
. On the 22nd line where you added:

reves[tam] = '\0'; // have to null terminate the string, as the original was


Can you refer me some info on the logic of this?

2nd version
Wow! Just classy, very classy. Very trimmed logic have to practice more for achieving a similar level.

@mrlowkey941: Thanks for your help. I still don't grasp the logic on using this cycle beginning at the 24th line:

for(int i = size - 1; i >= 0; i--){
cout << palindro[i];
}


Does this code prints one element of the string at the time?

Thanks a lot to both of you for your time. I'm marking this post as solved. You can PM me if you wish to help me with this other questions that originated from the original post.

closed account (E0p9LyTq)
The logic for null terminating a C string:

https://www.prismnet.com/~mcmahon/Notes/strings.html

A C string is usually declared as an array of char. However, an array of char is NOT by itself a C string. A valid C string requires the presence of a terminating "null character" (a character with ASCII value 0, usually represented by the character literal '\0').


The C string library functions require the char array to be null terminated, otherwise there is no way to know the length of the string.
closed account (E0p9LyTq)
juliotv wrote:
wow! Thanks a lot for showing me the different ways on how to make this task.


I do not like using C constructs, arrays and char array strings, in C++ code. Too error prone for out-of-bounds errors. I prefer using the containers and strings C++ provide.

Rewriting your program was a fun exercise for me to make C++ strings work. C++ makes -- in my opinion -- for neater, easier to read code.

I also used it as an exercise for C++ algorithms. I learned a few things about the <algorithm> library.
Topic archived. No new replies allowed.