c ++计算字符串长度_C ++程序计算字符串中的单词数

c ++计算字符串长度

Here you will get C++ program to count number of words in string.

在这里,您将获得C ++程序来计算字符串中的单词数。

First we initialize a counter variable to 1. Now iterate through string and count all the spaces, for each space increase the value of counter. Finally print the counter variable, it contains the number of words.

首先,我们将计数器变量初始化为1。现在遍历字符串并计算所有空格,为每个空格增加counter的值。 最后打印计数器变量,它包含单词数。

#include<iostream>
#include<stdio.h>
 
using namespace std;
 
int main()
{
	char a[100];
	int i,count=1;
	
	cout<<"Enter a string:";
	gets(a);
	
	for(i=0;a[i]!='\0';++i)
	{
		if(a[i]==' ')
			count++;
	}
	
	cout<<"\nThere are "<<count<<" words in the given string";
 
	return 0;
}

Output

输出量

Enter a string:how are you buddy?

输入一个字符串:你好吗?

There are 4 words in the given string

给定的字符串中有4个单词

翻译自: https://www.thecrazyprogrammer.com/2011/03/c-program-to-count-no-of-words-in-string.html

c ++计算字符串长度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值