|
发表于 2020-3-17 22:59:11
|
显示全部楼层
- #include <stdio.h>
- #include <string.h>
- #define MAX 128
- int main(void)
- {
- char str[MAX] = { 0 }; //存放用户输入
- char *p = str; //指向用户输入
- char q[MAX][MAX] = { 0 };
- char *p1[MAX] = { 0 };
- int i, j = 0;
- int word = 0, count = 0, k = 0; //word 记录有多少个单词 count记录有多少个字符
- printf("请输入一个英文句子:\n");
- for (i = 0; (str[i] = getchar()) != '\n'; i++) //获取用户输入
- {
- count++; //记录有多少个字母
- if (*(p + i) == ' ')
- {
- count--; //如果有空格则减去
- }
- }
- str[i] = '\0'; //将最后的\n 转换为 \0
- char* temp = p;
- while (str[j] != '\0') {
- if (str[j] == ' ' ) {
- *p = '\0';
- if (strlen(temp) !=0){
- printf("%s\n", temp);
- }
- temp = (p + 1);
- }
- p++;
- j++;
- }
- printf("%s\n", temp);
- return 0;
- }
复制代码
下半部分我按照我的理解写了一份
你的代码里面有一个问题是,当你是a b c 两个空格的时候,你处理起来或许就不是很合理了 |
|