help with string reverse

K

kyle.tk

I am trying to make a function to reverse a string. What I have right
now ends in a segfault.

#include <string.h>
#include <stdio.h>

/* Reverse a string */
void strev(char *s){
char tmp;
int f = 0;
int l = strlen(s) - 1;
while (f < l){
tmp = s[f];
s[f] = s[l];
s[l] = tmp;
f++;l--;
};
return;
}

int main(void){
char *str = "kyle";
strev(str);
printf("s = %s\n",str); /* I want the to output elyk */
return 0;
}
 
M

Michael Mair

kyle.tk said:
I am trying to make a function to reverse a string. What I have right
now ends in a segfault.

#include <string.h>
#include <stdio.h>

/* Reverse a string */
void strev(char *s){

You pass a pointer s.
The object s points to may be modified through s.
char tmp;
int f = 0;
int l = strlen(s) - 1;

Consider making sure that s != NULL before calling
strlen().
while (f < l){
tmp = s[f];
s[f] = s[l];
s[l] = tmp;
f++;l--;
};

The ; is unnecessary.

So is the return;
}

int main(void){
char *str = "kyle";

"kyle" is a string literal.
You must not modify string literals. Consider declaring
pointers to string literals as const char*; this does not
change the access semantics for the string literal.
Alternatively, if you want an array of char containing
your string, then declare it as
char arr[] = "kyle";
In this case, "kyle" is copied into the array as part
of the initialisation.
strev(str);

With
const char *str = "kyle";
you would get a diagnostic message from your compiler
telling you that strev() might modify whatever str
points to even though str points to something which must
not be modified through str. This is the reason for your
segfault.
With
char arr[] = "kyle";
there is no warning or error from the compiler -- and
correctly so, because the contents of arr may be modified.

Have also a look at the comp.lang.c FAQ -- there your problem
and others are discussed:
http://c-faq.com/
printf("s = %s\n",str); /* I want the to output elyk */
return 0;
}

Note: str followed by lower case is a name reserved by the
implementation, so str_rev() may be a better name.

Cheers
Michael
 
K

Keith Thompson

kyle.tk said:
I am trying to make a function to reverse a string. What I have right
now ends in a segfault.

#include <string.h>
#include <stdio.h>

/* Reverse a string */
void strev(char *s){
char tmp;
int f = 0;
int l = strlen(s) - 1;
while (f < l){
tmp = s[f];
s[f] = s[l];
s[l] = tmp;
f++;l--;
};
return;
}

int main(void){
char *str = "kyle";
strev(str);
printf("s = %s\n",str); /* I want the to output elyk */
return 0;
}

<http://www.c-faq.com/decl/strlitinit.html>

Try:

char str[] = "kyle";
 
C

CBFalconer

kyle.tk said:
I am trying to make a function to reverse a string. What I have right
now ends in a segfault.

#include <string.h>
#include <stdio.h>

/* Reverse a string */
void strev(char *s){
char tmp;
int f = 0;
int l = strlen(s) - 1;
while (f < l){
tmp = s[f];
s[f] = s[l];
s[l] = tmp;
f++;l--;
};
return;
}

int main(void){
char *str = "kyle";
strev(str);
printf("s = %s\n",str); /* I want the to output elyk */
return 0;
}

Try this (after include <string.h>) :

/* ======================================= */
/* reverse string in place. Return length */
size_t revstring(char *string)
{
char *last, temp;
size_t lgh;

if (lgh = strlen(string)) { /* ensure last-- valid */
last = string + lgh; /* points to '\0' */
while (last-- > string) {
temp = *string; *string++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

The names "strrev" and "strev" are not available in the user
namespace. They are reserved for the implementation.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
 
A

August Karlstrom

kyle.tk said:
I am trying to make a function to reverse a string. What I have right
now ends in a segfault.

#include <string.h>
#include <stdio.h>

/* Reverse a string */
void strev(char *s){
char tmp;
int f = 0;
int l = strlen(s) - 1;
while (f < l){
tmp = s[f];
s[f] = s[l];
s[l] = tmp;
f++;l--;
};
return;
}

int main(void){
char *str = "kyle";

You want

char str[] = "kyle";
strev(str);
printf("s = %s\n",str); /* I want the to output elyk */
return 0;
}

I'd write the reverse procedure like this:

void reverse(char *s)
{
int k, len, tmp;

len = strlen(s);
for (k = 0; k < len / 2; k++) {
tmp = s[k];
s[k] = s[len - 1 - k];
s[len - 1 - k] = tmp;
}
}


August
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top