【求助】【C++】怎么用<cstring>中的函数(strtok,strcmp之类的)将字符串
charsentence[]="ThereIsTooMuchSpaceBetweenYouAndMeAndHimAndHer"中的单词用空格分开并将大写转换为小写和统计a...
char sentence[]="ThereIsTooMuchSpaceBetweenYouAndMeAndHimAndHer"中的单词用空格分开并将大写转换为小写和统计and出现的次数?
展开
1个回答
展开全部
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char sentence[]="ThereIsTooMuchSpaceBetweenYouAndMeAndHimAndHer";
char *newsen = NULL,*p= NULL;
int big = 0,newlen = 0,andi=0,i=0,j=0;
for (i=0;i<strlen(sentence);i++)
{
if (sentence[i]>='A' && sentence[i]<='Z')
{
big++;
}
}
newlen = strlen(sentence) + 1 + big;
newsen = new char[newlen];
memset(newsen,0,sizeof(char)*newlen);
for (i=0;i<strlen(sentence);i++)
{
if (sentence[i]>='A' && sentence[i]<='Z')
{
if (i!=0)
{
newsen[j++] = ' ';
}
newsen[j++] = sentence[i] + 32;
}else
{
newsen[j++] = sentence[i];
}
}
cout<<newsen;
p=strtok(newsen," ");
while(p!=NULL)
{
if (strcmp(p,"and") ==0)
{
andi ++;
}
p=strtok(NULL," ");
}
cout<<"\r\nand["<<andi<<"]"<<endl;
return 0;
}
there is too much space between you and me and him and her
and[3]
Press any key to continue
启帆信息
2024-11-19 广告
2024-11-19 广告
启帆信息是英伟达中国区代理商,原厂授权代理,提供全面的软件技术解决方案以及NVIDIA以太网产品、交换机等产品,欢迎前来咨询!...
点击进入详情页
本回答由启帆信息提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询