C++输入一个英文句子,如果两单词之间多于一个空格,则删除多余的空格,然后输出处理前后该句子的长度。
1个回答
展开全部
char *pSource = "This is a English sentence";
char arrSentence[100] = {0};
int i = 0 ,j = 0; //i:源字符串 j:目标字符串
int nSpace = 0;
while ( pSource[i] != 0 )
{
if ( nSpace < 1 ) //允许有一个空格
{
arrSentence[j++] = pSource[i];
}
if ( pSource[i] == ' ' )
{
nSpace ++;
if ( pSource[i+1] != ' ' )
{
nSpace = 0;
}
}
i++;
}
printf("String lenth:%d\n" ,j);
printf("String:%s\n" ,arrSentence);
char arrSentence[100] = {0};
int i = 0 ,j = 0; //i:源字符串 j:目标字符串
int nSpace = 0;
while ( pSource[i] != 0 )
{
if ( nSpace < 1 ) //允许有一个空格
{
arrSentence[j++] = pSource[i];
}
if ( pSource[i] == ' ' )
{
nSpace ++;
if ( pSource[i+1] != ' ' )
{
nSpace = 0;
}
}
i++;
}
printf("String lenth:%d\n" ,j);
printf("String:%s\n" ,arrSentence);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询