[英]remove space in string array and count spaces C
我有一個帶有數組的字符串行。 例如:- char array[]="I'm new to programing";
我想重寫變量刪除空格array[]="I'mnewtoprograming"
的代碼
我已經嘗試過構建這個
scanf("%[^\n]s",&line);
int len=strlen(line);
//remove space in lenth
for(int i=0;i<len;i++)
(line[i]==' ')?:newlen++;
if (line[i]==' ')
{
line[i]=line[i+1];
}
給我錯誤的理由
您需要跟蹤字符串中當前的 position 以及將下一個字符與空格進行比較的“尾部”。
char *removechar(char *str, int ch)
{
char *cpos = str, *tail = str;
while(*tail)
{
if(*tail != ch)
{
*cpos++ = *tail++;
}
else
{
tail++;
}
}
*cpos = 0;
return str;
}
為array[]
創建 For 循環並計算' '
空格。 line[i]=line[i+1];
這條線是錯誤的。
(line[i]==' ')?:newlen++;
這條線是正確的,我看到一些評論說這條線是錯誤的。它與 if 條件相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.