C语言怎么将一段英文遍历为每个单词
例如下面的python是这样子的,但是用C怎么表达出来
用C语言写出Python这样的结果
#include <stdio.h>
#include <string.h>
int main()
{
char str[250] = "Youth is not a time of life; it is a state of mind; it is not a matter of rosy cheeks, red lips and supple knees; it is a matter of the will, a quality of the imagination, a vigor of the emotions; it is the freshness of the deep springs of life.";
for(int i=0; i<strlen(str); i++)
{
printf("%c", str[i]);
if(str[i] == ' ') // 遇空格换行
printf("\n");
}
return 0;
}
搜索字符串中的空格,遇到空格就换行
#include <stdio.h>
int main()
{
char s[1000],i=0;
gets(s);
while(a[i] != '\0')
{
if(a[i] != ' ')
printf("%c",a[i]);
else
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
char str[255];
while (scanf("%s", str) == 1)
printf("%s\n", str);
return 0;
}
有帮助请采纳
#include<stdio.h>
#include<string.h>
#define max 150 //句子总单词数最大值
#define lmax 25 //句子单个单词字母总数最大值
int isc(char t) //判断一个字符是不是字母
{
if((t<='z'&&t>='a')||(t<='Z'&&t>='A'))
return 1;
else
return 0;
}
int isw(char t[],int b,int e)//判断字符串从下标b开始到下标e是不是一个单词
{
int flag=1;
for(int i=b; i<=e; i++)//下标b开始到下标e,中间有不是字母的,返回
{
if(isc(t[i])==0)
{
return 0;
}
}
if(b>0)//不是字符串第一个字符
{
if(isc(t[b-1])==0&&isc(t[e+1])==0)//下标b-1和下标e+1不是字母,返回
{
return 1;
}
}
else
{
if(isc(t[e+1])==0)
{
return 1;
}
}
return 0;
}
void prin(char t[],int b,int e)//打印字符串从下标b开始到下标e之间的内容
{
for(int i=b; i<=e; i++)
{
putchar(t[i]);
}
printf("\n");
}
int main()
{
char t[max]= {' '};
char wt[max][lmax];
while(t)
{
gets(t);
int count=0;
int len=strlen(t);
int Left=0,Right=0;//左指针Left,右指针Right
int p;
for(Left=0; Left<len; Left++)
{
for(Right=0; Right<len-Left; Right++)
{
if(isw(t,Left,Left+Right)==1)//判断从左指针到右指针,之间是不是一个单词
{
prin(t,Left,Left+Right);
count ++;
break;
}
}
}
printf("\n");
printf("单词数:%d\n",count);
printf("输入quit退出\n");
if(strcmp(t,"quit")==0)
break;
}
return 0;
}
虽然已经采纳了,可以参考
#include <stdio.h>
#include <string.h>
int main(int, char **)
{
printf("请输入一段英文句子:");
char sentence[1024 + 1];
(void)fgets(sentence, sizeof(sentence) - 1, stdin);
char delim[] = " \n";
char *t = strtok(sentence, delim);
while (t != NULL)
{
puts(t);
t = strtok(NULL, delim);
}
}