[英]Function to remove spaces from string/char array in C
問的問題在這里是非常相似,我有一個問題。 區別在於我必須將一個參數傳遞給一個刪除空格並返回結果字符串/ char數組的函數。 我得到了代碼工作來刪除空格但由於某種原因我留下了原始數組遺留的尾隨字符。 我甚至嘗試過strncpy,但我遇到了很多錯誤。
這是我到目前為止:
#include <stdio.h>
#include <string.h>
#define STRINGMAX 1000 /*Maximium input size is 1000 characters*/
char* deblank(char* input) /* deblank accepts a char[] argument and returns a char[] */
{
char *output=input;
for (int i = 0, j = 0; i<strlen(input); i++,j++) /* Evaluate each character in the input */
{
if (input[i]!=' ') /* If the character is not a space */
output[j]=input[i]; /* Copy that character to the output char[] */
else
j--; /* If it is a space then do not increment the output index (j), the next non-space will be entered at the current index */
}
return output; /* Return output char[]. Should have no spaces*/
}
int main(void) {
char input[STRINGMAX];
char terminate[] = "END\n"; /* Sentinal value to exit program */
printf("STRING DE-BLANKER\n");
printf("Please enter a string up to 1000 characters.\n> ");
fgets(input, STRINGMAX, stdin); /* Read up to 1000 characters from stdin */
while (strcmp(input, terminate) != 0) /* Check for que to exit! */
{
input[strlen(input) - 1] = '\0';
printf("You typed: \"%s\"\n",input); /* Prints the original input */
printf("Your new string is: %s\n", deblank(input)); /* Prints the output from deblank(input) should have no spaces... DE-BLANKED!!! */
printf("Please enter a string up to 1000 characters.\n> ");
fgets(input, STRINGMAX, stdin); /* Read up to another 1000 characters from stdin... will continue until 'END' is entered*/
}
}
從input
刪除空格后,您沒有使用nul-terminator( \\0
)終止它,因為新長度小於或等於原始字符串。
只需在你的for循環結束時終止它:
char* deblank(char* input)
{
int i,j;
char *output=input;
for (i = 0, j = 0; i<strlen(input); i++,j++)
{
if (input[i]!=' ')
output[j]=input[i];
else
j--;
}
output[j]=0;
return output;
}
你沒有終止輸出,因為它可能會縮小,你就會把舊尾巴留在那里。
另外,我建議j
的處理總是在循環中遞增,然后如果不復制當前字符則必須手動遞減,這在某種程度上是次優的。 它不是很清楚,它正在做無意義的工作(遞增j
),甚至在不需要的時候也必須撤消。 相當混亂。
它寫得更容易:
char * deblank(char *str)
{
char *out = str, *put = str;
for(; *str != '\0'; ++str)
{
if(*str != ' ')
*put++ = *str;
}
*put = '\0';
return out;
}
正如其他人所提到的,源和目標都使用相同的字符串,並且不維護字符串的結尾。
你也可以用以下方式做。
char* deblank(char* input) /* deblank accepts a char[] argument and returns a char[] */
{
char *output;
output = malloc(strlen(input)+1);
int i=0, j=0;
for (i = 0, j = 0; i<strlen(input); i++,j++) /* Evaluate each character in the input */
{
if (input[i]!=' ') /* If the character is not a space */
output[j]=input[i]; /* Copy that character to the output char[] */
else
j--; /* If it is a space then do not increment the output index (j), the next non-space will be entered at the current index */
}
output[j] ='\0';
return output; /* Return output char[]. Should have no spaces*/
}
在for循環塊之后添加null(\\ 0)終止符后,必須返回字符串
char* deblank(char* input)
{
char *output=input;
for (int i = 0, j = 0; i<strlen(input); i++,j++)
{
if (input[i]!=' ')
output[j]=input[i];
else`enter code here`
j--;
}
output[j]='\0';
return output;
}
如果您需要一次過濾多個字符,可能會發現如下內容:
char *FilterChars(char *String,char *Filter){
int a=0,i=0;
char *Filtered=(char *)malloc(strlen(String)*sizeof(char));
for(a=0;String[a];a++)
if(!strchr(Filter,String[a]))
Filtered[i++]=String[a];
Filtered[i]=0;
return Filtered;
}
有用; 只需在*濾鏡中提供要刪除的字符列表。 例如“\\ t \\ n”,用於制表符,換行符和空格。
此代碼適用於O(n)的時間復雜度。
char str[]={"my name is Om"};
int c=0,j=0;
while(str[c]!='\0'){
if(str[c]!=' '){
str[j++]=str[c];
}
c++;
}
str[j]='\0';
printf("%s",str);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.