1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Goal - convert words coming in to a format
// suitable for word counts - strip leading and trailing
// non-alpha characters and convert to lower case.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
// read from stdin, write to stdout
char s[100];
while (scanf("%99s", s) == 1) {
char *begin = s, // first character
*end = s + strlen(s); // terminating NULL character
// remove leading non-alpha
while (! isalpha(*begin) && begin < end)
begin++;
// remove trailing non-alpha
while (end > begin && !isalpha(*(end-1))) {
end--; *end = '\0';
}
// convert to lower case
for(int i=0; begin[i] != '\0'; i++)
begin[i] = tolower(begin[i]);
// print
if (strlen(begin) > 0)
printf("%s\n", begin);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX