例如:如果用戶輸入: (23 22 43)
我希望該功能完全執行以下操作:
for(int i =0; i <strlen(x);i )
{
if (x[i]==' ')
{
deletfunc(x[i]);
deletfunc(x[i 1]);
cout<<x;
}
}
這樣輸出將是(2323)
不使用 index ----> 不知道陣列中 char 的確切數量例如我不能說 deletefunc[3] ,我不知道 是第 3 個或第 4 個或第 2 個元素等等 可能重復不止一次。
請如果有人可以幫助我已經嘗試執行此任務 4 天了
uj5u.com熱心網友回復:
通常在使用 C 風格的字串時,教師會說:“沒有索引!” 他們希望您使用指標。
這是您可以使用指標的一種方法
char * p = x; // point p at start of array x
while (*p) // loop until p points to the null terminator - the end of the string
{
if (*p==' ') // if value at p is
{
deletfunc(p - x); // distance between p and x is index
if (*(p 1)) // make sure there is a p 1 to erase
{
deletfunc(p 1 - x);
}
}
p ; // advance pointer to next character in array x
}
cout << x; // print revised string
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473525.html
下一篇:如何制作隨機不重復的數字?
