How can I reverse a string without using strlen.
I am not clear with logic part of this question.
17 replies to this topic
#1
Posted 21 September 2010 - 08:45 PM
|
|
|
#2
Posted 21 September 2010 - 09:33 PM
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 21 September 2010 - 11:10 PM
If you use sizeof(s) when s is defined as a string, the correct for loop is:
If you do strrev in-place, you only have to loop for (sizeof(s) - 1) / 2 positions.
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
Posted 21 September 2010 - 11:57 PM
Ok what if strlen,sizeof strrev type of functions are not available.(Meaning standard C libraries are not available.)
#5
Posted 22 September 2010 - 12:06 AM
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#6
Posted 22 September 2010 - 01:11 AM
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:
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
Posted 22 September 2010 - 01:19 AM
Ok I am trying your 2nd way of pointers I am having some problem I am getting a segmentation fault
Can you point what could be error.
What is the error in above program.
#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
Posted 22 September 2010 - 01:30 AM
onus said:
...
What is the error in above program.
What is the error in above program.
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
Posted 22 September 2010 - 01:34 AM
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?
wrong in above?
#10
Posted 22 September 2010 - 01:37 AM
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
Posted 22 September 2010 - 01:39 AM
Thanks zorah it became clear.
Here is my modified code
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
Posted 22 September 2010 - 01:44 AM
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:
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


Sign In
Create Account


Back to top









