- 输入一行字符串(单词和若干空格), 输出该行单词个数。
Input:____hello_________world_ how___are___you___\n
Output: 5- 输入一行字符串(单词和若干空格),输出该行单词(每个单词一行)
Input:____hello_________world_ how___are___you___\n
Output: hello
world
how
are
you- 输入一行字符串,把字符串翻转
Input: I am a student
Output: student a am I
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<math.h>
int wordStat(char a[]){
int cnt=0, i=1;
if(a[0] != ' ')
cnt++;
while(a[i] != '\0'){
if(a[i] != ' ' && a[i-1] == ' '){
cnt++;
}
i++;
}
return cnt;
}
void wordPut(char a[]){
int i=1;
if(a[0] != ' ')
printf("%c", a[0]);
while(a[i] != '\0'){
if(a[i] != ' '){
printf("%c", a[i]);
}
else if(a[i]==' ' && a[i-1]!=' ')
printf("\n");
i++;
}
if(a[i-1] != ' ')
printf("\n");
}
void strTurn(){
char a[50]={0},b[50] = {0}, word[20]={0};
int i, j, cnt, num;
while(gets(a) != NULL){
i = 0; j = 0; cnt = 0; num = 0;
while(a[i] != '\0'){
i++;
}
i--;
while(i>=0){
if(a[i] == ' '){
printf(" ");
b[num] = ' ';
num++;
}
//word 处理
else{
word[j] = a[i];
if(a[i-1]==' ' && i>0 || i==0){
cnt = j;
while(cnt>=0){
printf("%c", word[cnt]);
b[num] = word[cnt];
num++;
cnt--;
}
j = 0;
}
else{
j++;
}
}
i--;
}
printf("\n");
b[num] = '\0';
printf("%s", b);
}
}