"dough" <> wrote in news:1128301233.146582.222730
@g14g2000cwa.googlegroups.com:
> Can anyone tell me why I get a segmentation fault with the following
> code and call?
>
> char *strlwr(char *s)
> {
> while( s != NULL && *s != '\0' )
> {
> *s = tolower(*s);
> s++;
> }
>
> return s;
> }
>
> printf("%s", strlwr("HAPPY"));
This is a FAQ:
http://www.eskimo.com/~scs/C-faq/q16.6.html
Also, functions with the str prefix are in reserved by the standard.
Note that the function above will return a pointer to the terminating nul
character.
#include <ctype.h>
#include <stdio.h>
char *mystrlwr(char *s) {
if (s) {
char *t = s;
while (*t) {
*t = tolower((unsigned char) *t);
++t;
}
}
return s;
}
int main(void) {
char s[] = "HELLO";
printf("%s\n", mystrlwr(s));
return 0;
}
D:\Home> gcc -Wall s.c -o s.exe
D:\Home> s
hello
--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)