打印出字符串中的单词
输入 Hello, this is a sample sentence.
输出
Hello
this
is
a
sample
sentence
代码实现
#include <stdio.h>
int isAlphabet(char ch) {
return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
}
void printWord(char *start, char *end) {
while (start < end) {
printf("%c", *start);
start++;
}
printf("\n");
}
void findWords(char *str) {
char *start = NULL;
char *end = NULL;
while (*str) {
while (*str && !isAlphabet(*str)) {
str++;
}
start = str;
while (*str && isAlphabet(*str)) {
str++;
}
end = str;
if (start < end) {
printWord(start, end);
}
}
}
int main() {
char str[] = "Hello, this is a sample sentence.";
findWords(str);
return 0;
}