输入一个字符串,把这个字符串中的每个单词打印出来,即输出到屏幕

本文介绍了一个函数,用于从输入字符串中分离出每个单词,并使用另一个函数将这些单词逐个输出到屏幕上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

输入一个字符串(或英文),把这个字符串中的每个单词打印出来,即输出到屏幕;

结果如下图:

void str_split(char* str)  这个函数用来获得每个单词,并用下面的str_show()函数输出到屏幕。

void str_show(int n,char* str,int begin,int end) :打印某个单词。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define IS_ALPHA(ch) (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
#include<ctype.h> 
void str_show(int n,char* str,int begin,int end)
{
	printf("%d:",n);
	int i;
	for(i=begin;i<=end;i++)
	{
		printf("%c",str[i]);	
	}	
	printf("\n");
}

void str_split(char* str)
{
	int index_head,index_tail;
	int cnt=0;
	index_head=0;
	while(str[index_head]!='\0')
	{
		for( ;str[index_head]!='\0';index_head++)
		{
			if(isalpha(str[index_head])&&(index_head-1!=-1 && str[index_head-1]==' '|| index_head-1==-1))	
				break;
		}
		index_tail=index_head;
		while(isalpha(str[index_tail]))
		{
			index_tail++;		
		}
		cnt++;
		str_show(cnt,str,index_head,index_tail-1);
		index_head=index_tail;
	}	
}
int main(int argc, char* argv[])
{
	char buf[1024];
	while(gets(buf)!=NULL)
	{
		str_split(buf);	
	}
	system("pause");
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值