0
02 - Create an algorithm that asks the user to enter its name and store in an array, the program must store in another vector the name written backwards. At the end, the two vectors should be written. Ex.: Vinicius - suiciniv
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cstring>
using namespace std;
main(){
char vetor[10];
cout << "Digite um nome:";
cin >>vetor;
vetor[strlen(vetor)] = '\0';
char contrario[strlen(vetor)];
int tamanho =strlen(vetor-1);
int j=0;
for(int i=0;i<strlen(vetor);i++){
cout<<vetor[i];
}
for(int i=strlen(vetor)-1;i>=0;i--){
//cout <<vetor[i];
contrario[j] = vetor[i];
j+=1;
}
cout<< "--" <<contrario;
return 0;
}
You need to end the character null with the character vector
contrario
. The propostium, the verycin
will put the null terminator, you do not need to put it in hand after reading.– Jefferson Quesado
Can you edit the code please? so I can understand better. I’m starting in c++, and my first programming language .. I don’t understand anything yet
– Vitor Lima
About null terminator, you can find several answers here in Stackoverflow in English on the subject. For example, this one here It seems to me to be very intuitive and well written. I won’t edit your code because it doesn’t make sense in your learning, you should understand the concepts I speak and, not understanding them, go after their meaning.
– Jefferson Quesado
Guys, I was able to solve this problem using string and getline. #include <iostream> #include <string> using namespace Std; main (){ string word; Cout << "Type Word:"; getline(Cin,word); for (int i=0;i<word.size();i++){ Cout <<word[i]; } Cout <<Endl; for(int i=word.size()-1;i>=0;i-){ Cout<word[i]; } Return 0; }
– Vitor Lima