Originally Posted by
tabstop
If you pass in a constant bit of memory as s, then trying to write to it would cause a crash. (I'm assuming that's what you mean by the program ending, as opposed to the program ending because you didn't give it anything else to do.)
Well, thx tabstop, after lunch this bit of code works completely unchanged...
Here follows a version that does not need ctype.h inclusion any more because toupper(), tolower() and isspace() functions have been removed:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void str_capitalize(char[]);
int main() {
const char author_ok[] = "Sigmund Baginov";
char author[] = "SIGMUND BAGINOV";
str_capitalize(author);
printf("%s is %sas expected\n", author,
(0 == strcmp(author, author_ok) ? "" : "not "));
return EXIT_SUCCESS;
}
void str_capitalize(char s[]) {
int up = 1;
while ('\0' != *s) {
if (up) {
if (' ' != *s && '\t' != *s && '\n' != *s) {
*s += (*s >= 'a' && *s <= 'z' ? 'A' - 'a' : 0);
up = 0;
}
} else {
if (' ' == *s || '\t' == *s || '\n' == *s)
up = 1;
else
*s += (*s >= 'A' && *s <= 'Z' ? 'a' - 'A' : 0);
}
++s;
}
}