LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2012, 09:16 AM   #1
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Rep: Reputation: Disabled
Post 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.
 
Old 08-08-2012, 09:30 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Is this legal syntax?
Code:
for(j=k-1;j=0;j--)
maybe try putting a diagnostic printf inside that loop
 
Old 08-08-2012, 09:34 AM   #3
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
why.? what is wrong with this code.? j starts from say 5 n decrements every time.
 
Old 08-08-2012, 09:35 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by pixellany View Post
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.
 
1 members found this post helpful.
Old 08-08-2012, 09:42 AM   #5
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Nylex View Post
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.
 
Old 08-08-2012, 09:55 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
To start:

Code:
old: for(j=k-1;j=0;j--)
new: for(j=k-1;j>=0;j--)
 
1 members found this post helpful.
Old 08-08-2012, 10:00 AM   #7
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
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.
 
1 members found this post helpful.
Old 08-08-2012, 10:09 AM   #8
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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
 
Old 08-08-2012, 10:14 AM   #9
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-08-2012, 10:14 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

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

PS: strcmp returns a number, not a pointer, so use ==0, not ==NULL
 
1 members found this post helpful.
Old 08-08-2012, 10:23 AM   #11
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-08-2012, 10:26 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Insert this line before/after the cycle: str2[k]= '\0'
 
Old 08-08-2012, 10:29 AM   #13
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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.?

Last edited by chaacoali; 08-08-2012 at 10:31 AM.
 
Old 08-08-2012, 11:45 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.)
 
Old 08-08-2012, 12:10 PM   #15
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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.?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Only executing signed code derchris Linux - Security 5 02-23-2011 03:06 PM
Application sometimes hang when executing this C code archieval Programming 2 11-02-2010 08:05 AM
executing C++ code from within PHP code vineet7kumar Programming 1 06-03-2007 04:13 PM
Executing command line through Code socialjazz Programming 2 10-02-2006 03:09 PM
executing a command as part of printf... thatbloke Linux - Newbie 6 05-06-2004 04:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration