Weird symbols in output.

I need to reverse a string using another string.
But this code gives weird symbols in output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
void main()
{ clrscr();
char str[10];
char str1[10];
gets(str);
int l=strlen(str);
for (int i=l-1; i>=0; i--)
   { for(int j=0; j<=l-1; j++)
	  { str1[j]=str[i];
	  }}
	  cout<<str1;
	  getch();
	  }
insert str1[l] = '\0'; after you for. ie between line 14 and 15.
You'll have to figure out the next issue.
Your logic has lots of issue.

1
2
3
4
5

for (int i=l-1; i>=0; i--)
   { for(int j=0; j<=l-1; j++)
	  { str1[j]=str[i];
	  }}


check out the above code..

1
2
3
4
5
6
7
8
int b=0;
for (int i=l-1; i>=0; i--,b++)
	{
						str1[b]=str[i];//you dont need nested loop
	}

	str1[b]='\0';
	  cout<<str1;


^^this should work.

You have to end your new string with a '\0'.

otherwise you will get all of those weird characters.
Topic archived. No new replies allowed.