Input: hello world how are you \n
Output: hello
world
how
are
you
思路:是空格就不输出,不是空格就输出,再碰到空格注意输出换行
指针实现:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 512
void count(char *p) {
int flag = 0;
while (*p != '\0') {
if (*p != ' ') {
printf("%c", *p);
flag = 1;
if (*(p + 1) == '\0') {
printf("\n");
}
}
else if(*p == ' '&&flag==1){
flag = 0;
printf("\n");
}
p++;
}
}
int main() {
char a[maxn] = {0};
while (gets(a) != NULL) {
count(a);
memset(a, 0, sizeof(a));
}
return 0;
}
/*
I am a student\n
hello world how are you \n
*/