Trying to make my own reverse string

This is a discussion on Trying to make my own reverse string within the C Programming forums, part of the General Programming Boards category; Code: int lstrlenQ(const char *s) { int i = 0; while(*s) { s++; i++; } return i; } int lReverse(const ...

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307

    Trying to make my own reverse string

    Code:
    int lstrlenQ(const char *s) {
    	int i = 0;
    	while(*s) {
    		s++;
    		i++;
    	}
    	return i;
    }
    
    int lReverse(const char *str, char *buffer) {
    	int npos = lstrlenQ(str);
    	const char *p = str + npos;
    	while (npos) {
    		*buffer = *p;
    		buffer++;
    		p--;
    		npos--;
    	}
    	*buffer++ = '\0';
    	return 0;
    }
    But return and empty string...any ideas?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    TBH, I have no idea what you're asking at all, but you're assigning the '\0' in the wrong spot I believe. It should be:

    Code:
    *buffer = '\0'

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    You are reversing the Null character also...so the output string will be Null followed by your string ,so while displaying you are getting empty string which is nothing but Null.In order to solve you can change the line const char *p = str + npos; to const char *p = str + npos-1;

  4. #4
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307
    Quote Originally Posted by drop2kumar View Post
    You are reversing the Null character also...so the output string will be Null followed by your string ,so while displaying you are getting empty string which is nothing but Null.In order to solve you can change the line const char *p = str + npos; to const char *p = str + npos-1;
    Oh..yeah! I understand
    Code:
    int lReverse(const char *str, int nSize, char *buffer) {
    	const char *p = str + nSize - 1;
    	while (nSize) {
    		*buffer = *p;
    		buffer++;
    		p--;
    		nSize--;
    	}
    	*buffer = '\0';
    	return 0;
    }
    Thanks =)
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help] About reverse string
    By kingofdcp in forum C Programming
    Replies: 3
    Last Post: 06-06-2009, 02:53 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. string in reverse
    By Teele777 in forum C Programming
    Replies: 2
    Last Post: 04-18-2002, 07:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21