printing trash?

how to program print only the inversion of the word?
thank you

#include<iostream>
#include<stdio.h>

using namespace std;

void inverte(char *t){
char *aux;
aux = new char[strlen(t)+1];

//testes
cout <<"\nt[2]:"<< t[2];
cout << "\ntamanho t: "<< strlen(t);
cout << "\ntamanho aux: "<< strlen(aux);

//Invertendo
for (int i=0;i<strlen(t);i++){
aux[i]=t[strlen(t)-1-i]; //Nletra-1 - i (o -1 pq se não aux[0] é \0)
cout<<"\nAux: "<<aux[i];
};

cout << "\naux: "<<aux;
t=aux;
cout <<"\n" <<&t[0];
};

void main(){

char s[]= "saud";

//LENDO A PALAVRA
char *p;
cout << "Qual palavra: ";
char pal[100];
cin >> pal;
p= new char[strlen(pal)+1];
strcpy(p,pal);
cout << p;
//********Fim de LER PALAVRA

inverte(p);
//cout << "\nInversao: "<<p;


//inverte(s);
cin.get();
cin.get();
};
As for me I have not understood the question. That only to print a word in the reverse order the following function can be written


1
2
3
4
5
6
void print( const char *s, std::ostream &os = std::cout )
{
   const char *p = s + std::strlen( s );

   while ( p != s ) os << *--p;
}
Last edited on
It seems I have understood what you want.

1
2
3
4
5
6
7
8
9
10
void inverte( char *t )
{
 //Invertendo
   for ( char * p = t + strlen( t ) ; t != p && t != --p; ++t )
   {
      char c = *t;
      *t = *p;
      *p = c;
   }
}   
Last edited on
the problem is:

I want to write a word like "ABCDEFG" and the program needs to reverse
"GFEDCBA"




your code is not picking up the first words, what's happening ?

For example, if you type 12345, 321 prints
Last edited on
My code works correctly. It is you who are even unable cimply copy and paste it.
Topic archived. No new replies allowed.