Can someone please tell me what is wrong with this code

int main()
{
char str[10],str2[10];
int k,i=0,j;
printf("enter de string\n");
scanf("%s",str);
k=strlen(str);
printf("length of de string %d\n",k);
        for(j=k-1;j=0;j--)
        {
        str2[i++]=str[j];
        }
printf("%s",str2);
        for(i=0;i<k;i++)
        {
            if(strcmp(str,str2)==0)
                {
                printf("palindrome");
                }
        }
return 0;
}

this is my output

enter de string
jamiaj
length of de string 6

Why is the code after printing the length not working.

Your loop for(j=k-1;j=0;j--) is wrong. Try for(j=k-1;j>=0;j--)
You are assigning j in the test condition instead of testing it to be >= 0.

Edited 4 Years Ago by rubberman

Ok thanks I figured out that and edited my code to this.

int main()
{
char str[10],str2[10];
int k,i=0,j;
printf("enter de string\n");
scanf("%s",str);
k=strlen(str);
printf("length of de string %d\n",k);
        for(j=k-1;j>=0;j--)
        {
        str2[i++]=str[j];
        }
printf("%s\n",str2);
printf("length of reversed string %d",strlen(str2));
       if(strcmp(str,str2)==0)
            {
                printf("palindrome");
        }
return 0;

This is my output

enter de string
james
length of de string 5
semaj��
length of reversed string 7

Can u tell me how 7 characters are formed. 6th character is formed because of the str2[i++] statement, but how is 7th character a junk.?

Edited 4 Years Ago by chaacoali

place a null character i.e. '\0' after the loop (between 12-13) lines
as soon as compiler gets a null character, it terminates the string.

There you go.......

int main()
{
  char str[10],str2[10];
  int k,i=0,j;
  printf("enter de string\n");
  scanf("%s",str);
  k=strlen(str);
  printf("length of de string %d\n",k);
  for(j=k-1;j>=0;j--)
  {
     str2[i++]=str[j];
  }
  str2[i]='\0';      // placed null character at the end.
  printf("%s\n",str2);
  printf("length of reversed string %d",strlen(str2));
  if(strcmp(str,str2)==0)
  {
     printf("palindrome");
  }
  return 0;
}
Comments
Good boy. Hope you got a good grade doing his code for him...

Yes I understod that but why exactly only two junk characters are printed if I dont place the '/0' .? Dont judge the program by looking at the palindrome statement. I want to know why the junk characters are printed.

every character has a particular ASCII value. To satisfy the string length, compiler can take any characters. For you, compiler took 2 quesmark type symbols, whereas it may take some other symbols for someone else. As you have taken string length as [10] but you are entering a string of length [5], compiler puts the garbage characters to make your entered string length equal to 10.

hope you understood!

When you copied the string into str2, you neglected to terminate the string with '\0'. So those ? characters were just what happened to be in memory after your string. The printf() printed the string until it found a '\0' -- in this case it was close. In other cases you could have displayed pages of junk.

@WaltP

It depends on the environment you are coding in.
Chaacoali got these two symbols but you may get different or even the junk you are talking about!

@WaltP

It depends on the environment you are coding in.
Chaacoali got these two symbols but you may get different or even the junk you are talking about!

Duh... Isn't that what I said? I thought I was fairly clear about it.

No, it doesn't depend on the environment you're coding in.

well, it depends on the compiler?
I've seen diff characters getting printed in diff compiling environments!

those are the outputs i got in
Borland turbo C++.. prints "semaj# Rjames"

DEV C++.. prints a downward arrow with @ this symbol

Edited 4 Years Ago by Vish0203

This article has been dead for over six months. Start a new discussion instead.