ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.?
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.)
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.?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.