C++输入一个英文句子,如果两单词之间多于一个空格,则删除多余的空格,然后输出处理前后该句子的长度.
问题描述:
C++输入一个英文句子,如果两单词之间多于一个空格,则删除多余的空格,然后输出处理前后该句子的长度.
答
char *pSource = "This is aEnglishsentence";
char arrSentence[100] = {0};
int i = 0 ,j = 0; //i:源字符串 j:目标字符串
int nSpace = 0;
while ( pSource[i] != 0 )
{
if ( nSpace {
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);