c++去掉字符串中連續的空格,只保留一個

#include "stdafx.h"
#include <iostream>

using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
char str[]="how    a re   you   ?";
int i,j;
while(str[j]!='\0')
{
if(str[j]!=' ')
{
        str[i++]=str[j++];
}
else
{
str[i++]=str[j++];
while(str[j]==' ')
{
j++;
}
}
}
str[i]='\0';
cout<<str<<endl;
    return 0;
}
程序編寫過程中出現的問題:
char *pstr 與char str[]不同,利用pstr遍歷字符串就要用pstr++,不能用pstr[i];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章