我已經編寫了以下程序,它將刪除string1中存在於string2中的所有常用字符。C++:通過函數使用指針操縱字符串
#include<iostream>
#include<string>
#include<iterator>
using namespace std;
void DelCommonChar(char *input, string s2)
{
string s1(input);
string::iterator it1;
string::iterator it2;
for(it1=s1.begin();it1<s1.end();it1++)
{
for(it2=s2.begin();it2<s2.end();it2++)
{
if(*it1==*it2){it1=s1.erase(it1);it1--;}
}
}
cout<<s1<<endl; // Line Number 20
}
int main()
{
char c[32];
strncpy(c,"Life of Pie",32);
DelCommonChar(c,"def");
cout<<c<<endl; //Line Number 29
}
Output:Li o pi ......... printed through line number 20.
但現在我想chagne變量c[32]
本身是在main function
,我想line number 29
打印輸出。
你能幫我嗎,如何改變變量c[32]
只在函數DelCommonChar
裏面?
注意:我不想更改函數返回數據類型void
。
請不要用這樣的行號碼發佈代碼;它可以防止讀者將其粘貼到編輯器中並能夠編譯它。 –
@OliCharlesworth我想通過查看行號來簡化讀者,因爲我已經提到了問題中的行號。使用記事本++來垂直選擇列和刪除可以提供幫助。 – CodeCodeCode
@CodeCodeCode:我很確定讀者不需要行號。只是用評論或其他東西來標記興趣點。例如,在行結尾處出現「// <--- ---這是一個問題」。 – SigTerm