LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Only part of the code executing (https://www.linuxquestions.org/questions/programming-9/only-part-of-the-code-executing-4175421007/)

chaacoali 08-08-2012 09:16 AM

Only part of the code executing
 
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
Code:

enter de string
jamiaj
length of de string 6

Why is the code after printing the length not working.

pixellany 08-08-2012 09:30 AM

Is this legal syntax?
Code:

for(j=k-1;j=0;j--)
maybe try putting a diagnostic printf inside that loop

chaacoali 08-08-2012 09:34 AM

why.? what is wrong with this code.? j starts from say 5 n decrements every time.

Nylex 08-08-2012 09:35 AM

Quote:

Originally Posted by pixellany (Post 4748982)
Is this legal syntax?
Code:

for(j=k-1;j=0;j--)

It is. The code wouldn't compile if it wasn't.

I'd probably avoid replying to threads if you're not sure you can help, so you don't remove them from the zero reply list.

chaacoali 08-08-2012 09:42 AM

Quote:

Originally Posted by Nylex (Post 4748989)
It is. The code wouldn't compile if it wasn't.

I'd probably avoid replying to threads if you're not sure you can help, so you don't remove them from the zero reply list.

Can u please tell me what could be wrong.? Breaking my head over this, not able to understand what is wrong.

NevemTeve 08-08-2012 09:55 AM

To start:

Code:

old: for(j=k-1;j=0;j--)
new: for(j=k-1;j>=0;j--)


rstewart 08-08-2012 10:00 AM

Quote:

why.? what is wrong with this code.? j starts from say 5 n decrements every time.
No it dosen't. You are doing an assignment with your statement of j=0, not a comparison - the loop is NOT being executed. Anyways the correct comparison wouldn't be j==0 either, the comparison that you want to do in your loop is j>=0.

You have other issues with your code as well that you need to look at, but I figured I would point out the first problem and let you figure out the rest.

chaacoali 08-08-2012 10:09 AM

Quote:

Originally Posted by NevemTeve (Post 4749011)
To start:

Code:

old: for(j=k-1;j=0;j--)
new: for(j=k-1;j>=0;j--)


Thanks a lot, silly mistake. The later part of the code is still not working. I edited my code to 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\n",str2);
printf("length of reversed string %d",strlen(str2));
      if(strcmp(str,str2)==NULL)
            {
                printf("palindrome");
        }
return 0;

This is the output
Code:


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


chaacoali 08-08-2012 10:14 AM

How did those extra 2 characters(junk ) get added to str2, also I'm getting this error

warning: comparison between pointer and integer in the strcmp fn line

NevemTeve 08-08-2012 10:14 AM

Insert this line before/after the cycle: str2[k]= '\0'

PS: strcmp returns a number, not a pointer, so use ==0, not ==NULL

chaacoali 08-08-2012 10:23 AM

I replaced NULL with 0 and the warning disappeared but still not getting the desired result

Code:

enter de string
james
length of de string 5
semaj��
length of reversed string 7
not palindrome[root@localhost c]# ./a.out
enter de string
jaiaj
length of de string 5
jaiaj��
length of reversed string 7
not palindrome


NevemTeve 08-08-2012 10:26 AM

Insert this line before/after the cycle: str2[k]= '\0'

chaacoali 08-08-2012 10:29 AM

Quote:

Originally Posted by NevemTeve (Post 4749030)
Insert this line before/after the cycle: str2[k]= '\0'

PS: strcmp returns a number, not a pointer, so use ==0, not ==NULL

Thanks a lot. That helped. So everytime I reverse a string or do any string operations of this sort I should make sure I replace the kth character by end of string.

But could you please explain how those junk characters are formed.? I'm only able to figure out the occurence of first junk character which is because of i++ statement. How is it that the other one occurs.?

NevemTeve 08-08-2012 11:45 AM

This thing is called memory garbage. What you have to remember is adding a terminating zero to the end of a string you create manually. (Strings created by library functions (strcpy, strcat, sprintf, getenv etc) are already zero-terminated.)

chaacoali 08-08-2012 12:10 PM

Quote:

Originally Posted by NevemTeve (Post 4749096)
This thing is called memory garbage. What you have to remember is adding a terminating zero to the end of a string you create manually. (Strings created by library functions (strcpy, strcat, sprintf, getenv etc) are already zero-terminated.)

I had a similar program but in that case this garbage values didn't appear. So you mean to say its not necessary that it should always happen, but it is possible for such a thing to happen(garbage values).?

So this problem, is it caused because of my str2[i++] statement or is it because of something else.?


All times are GMT -5. The time now is 03:21 AM.