Reversing a string (URGENT HELP!!)

I made a program to reverse a string.

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

void main()
{
char srcstr[30],desstr[30];
int i,len;
clrscr();
cout<<"\nenter the string\n";
cin>>srcstr;
len=strlen(srcstr);
for(i=0;srcstr[i]!='\0';i++)
{
desstr[--len]=srcstr[i];
}
desstr[i]=0;
cout<<desstr;
getch();
}


When I executed it..the string was reversed but some special characters were appearing in front of the reversed string..so my teacher asked me to add
desstr[i]=0; to the program. Please let me know..y there was a need to add "desstr[i]=0;"??
Try this

1
2
3
4
5
6
for ( i = 0; i < len - 1; i++ )
{
  desstr[i] =  srcstr[len - 1 - i];
}

desstr[i] = '\0';
Last edited on
Topic archived. No new replies allowed.