Thread: strrev! whats wrong?

  1. #1
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Question strrev! whats wrong?

    Well here is my program to reverse a string.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void xstrrev(char *s, char *s1);
    
    int main()
    {
    	char s[]="srini";
    	char s1[10];
    
    	xstrrev(s,s1);
    
    	return 0;
    }
    
    void xstrrev(char *s, char *s1)
    {
    	int len,i;
    	len=strlen(s);
    
    	while(*s!='\0')
    		s++;
    
    	s--;
    
    	for(i=len;i>=0;i--)
    	{
    		*s1=*s;
    		len--;
    		s--;
    		s1++;
    	}
    	*s1='\0';
    	printf("%s",*s1);
    }
    Please tell where am i doing wrong?

  2. #2
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    Well i got it working. Here is the code

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void xstrrev(char *s);
    
    int main()
    {
    	char s[]="srini";
    
    
    	xstrrev(s);
    
    	return 0;
    }
    
    void xstrrev(char *s)
    {
    	int len,i;
    	char s1[10],*s2;
    	s2=s1;
    	len=strlen(s);
    
    	while(*s!='\0')
    		s++;
    
    	s--;
    
    	for(i=0;i<len;i++)
    	{
    		*s2=*s;
    		s--;
    		s2++;
    	}
    	*s2='\0';
    	printf("%s",s1);
    }

    But i still want to know why the previous code didnt work?
    Please help

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    the code :

    while(*s!='\0')
    s++;
    s--;

    Doesn't actually do anything. Once it has executed the s is still pointing to the same place in memory. Try actually printing the value of s and its location in memory along side it and you can see.

    The code below also achieves what you were looking for:


    void xstrrev(char *s, char *s1)
    {
    int len,
    int i;

    len= (strlen(s) -1); // To ensure you don't start on NULL

    for(i= 0; i <= len ;i++)
    {
    *(s1 + i)=*(s + (len - i));
    }
    *(s1 + i)='\0';

    printf("%s\n", s1);
    }

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Actually .. Looking at this again I could be wrong .. Anyone care to shoot me down!

    That while loop will actually reference a different value.. Oops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM