Jump to content

how to reverse a string without using strlen

- - - - -

  • Please log in to reply
17 replies to this topic

#1
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
How can I reverse a string without using strlen.
I am not clear with logic part of this question.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
sizeof() can be a replacement for strlen(). You could also use pointers, a simple example:

char s[] = "012456789";
char* ps = s;
while(*ps != '\0') {
    //do strrev here
    ps++;
}
I'll leave the reverse part up to you, it is not hard.

Edited by Alexander, 23 September 2010 - 11:21 PM.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
If you use sizeof(s) when s is defined as a string, the correct for loop is:

for (i = 0; i < sizeof(s) - 1; i++)
Otherwise the reversed string will start with the null terminator.

If you do strrev in-place, you only have to loop for (sizeof(s) - 1) / 2 positions.

#4
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
Ok what if strlen,sizeof strrev type of functions are not available.(Meaning standard C libraries are not available.)

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

onus said:

Ok what if strlen,sizeof strrev type of functions are not available.(Meaning standard C libraries are not available.)

Use pointers, my second example.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
You can't reverse a string if you don't know its length, because new positions for characters depend on length. So you must employ some method to find out the length of the string. Using sizeof is not an option in general case, but only when string is allocated as static array.

Here is the function implementation which first counts characters in string and then reverses the string:

#include <stdio.h>

void strrev(char *string)
{
    
    int len = 0;
    while (string[len])
        len++;

    int down = 0;
    int up = len - 1;

    while (up > down)
    {
        char c = string[down];
        string[down++] = string[up];
        string[up--] = c;
    }

}

int main(int argc, char* argv[])
{

    char string[] = "0123456789";

    printf("Original: %s\n", string);
    strrev(string);
    printf("Reversed: %s\n", string);

}
And here is the output:
Original: 0123456789
Reversed: 9876543210


#7
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
Ok I am trying your 2nd way of pointers I am having some problem I am getting a segmentation fault



#include<stdio.h>

int main ()

{

int i;

char *p,*s;

 p="abcdefgh";

 i=0;

        while (*p != '\0')

        {

        i++;

        printf(" i=%d  %c \n",i,p[i]);

        }

}

when I run the above program.
Can you point what could be error.
What is the error in above program.

#8
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts

onus said:

...
What is the error in above program.
Here:
    int i;
char *p,*s;
 p="abcdefgh";
 i=0;
        while (p[i] != '\0')
        {
        printf(" i=%d  %c \n",i,p[i]);
        i++;
        }
But this doesn't revert the string, of course. You can't revert the string efficiently within the loop which counts characters.

#9
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
Ya I know this will not reverse the string I am trying to understand as how the logic which above was suggested to me will work I am writing that part of code while I am posting it here.Why was use of *p != '\0'
wrong in above?

#10
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
It's wrong because you're always testing whether the first character of the string is end of string, but it's not. So you have an endless loop. *p is the first character and p[i] (or *(p+i)) is ith character in the string.

#11
onus

onus

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
Thanks zorah it became clear.

Here is my modified code

#include<stdio.h>
int main ()
{
int i,j;
char *p,*s;
 p="abcdefgh";
 i=0;j=0;
        while (p[i] != '\0')
        {
        printf(" i=%d  %c \n",i,p[i]);
        i++;
        }
        i--;
       while (i>=0){
         s[j]=p[i];
         j++;
        }
  printf(" %s\n",s);
}
to reverse the string this code is giving me segmentation fault.

#12
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
You must put s=p somewhere before starting the second loop. And you must decrease i in the second loop, e.g. like s[j++] = p[i--].

And, for lovers of beautiful programming, here is a tremendously inefficient implementation of strrev function which doesn't know the length of the string before it starts reverting it, but finally it does revert it in a record breaking O(n^2) time:

void strrev(char *string)
{

    for (int i = 0; string[i]; i++)
        for (int j = i - 1; j >= 0; j--)
        {
            char c = string[j + 1];
            string[j + 1] = string[j];
            string[j] = c;
        }

}
This is actually equal to selection sort.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users