函数删除字符串/字符数组空间用C [英] Function to remove spaces from string/char array in C
问题描述
问题问 是非常相似,我是什么具有的一个问题。不同的是,我必须通过一个参数传递给一个函数,删除空格和返回结果字符串/字符数组。我得到了code工作把空格去掉,但由于某种原因,我离开了尾随从原始数组遗留下来的字符。我甚至尝试函数strncpy,但我有很多的错误。
下面是我到目前为止有:
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
StringMax的的#define 1000 / * Maximium输入尺寸为1000个字符* /字符* deblank(字符*输入)/ * deblank接受一个char []参数,并返回一个char [] * /
{
字符*输出=输入;
的for(int i = 0,J = 0; I< strlen的(输入);我++,J ++)/ *评估每一个字符输入* /
{
如果(输入由[i]!='')/ *如果该字符不是空格* /
输出[J] =输入[I] / *该字符复制到输出的char [] * /
其他
j--; / *如果它是一个空间,然后不会增加产出指数(J),下无空间将当前索引中输入* /
}
返回输出; / *返回输出的char []。不应该有空格* /
}
诠释主要(无效){
字符输入[StringMax的];
终止的char [] =END \\ N的; / *哨兵值,退出程序* / 的printf(STRING DE-BLANKER \\ n);
的printf(请输入一个字符串到1000字符\\ n方式>);
与fgets(输入,StringMax的,标准输入); / *读取多达从标准输入1000个字符* / 而(STRCMP(输入,终止)!= 0)/ *检查阙退出! * /
{
输入[strlen的(输入) - 1] ='\\ 0';
的printf(你输入:\\%s \\的\\ n,输入); / *打印原始输入* /
的printf(你的新字符串是:%S \\ n,deblank(输入)); / *打印从deblank(输入)输出应该没有空格... DE-空白! * / 的printf(请输入一个字符串到1000字符\\ n方式>);
与fgets(输入,StringMax的,标准输入); / *读取多达从标准输入另一个1000个字符... ...将继续下去,直到END中输入* /
}
}
从输入删除空格之后
您还没有NUL终止符终止它( \\ 0
),因为新的长度小于或等于原始字符串
在年底只需NUL,终止它的for循环:
的char * deblank(字符*输入)
{
INT I,J;
字符*输出=输入;
对于(i = 0,J = 0; I< strlen的(输入);我++,J ++)
{
如果(输入由[i]!='')
输出[J] =输入[I]
其他
j--;
}
输出[J] = 0;
返回输出;
}
The question asked here is very similar to what I am having a problem with. The difference is that I must pass an argument to a function that removes the spaces and returns the resulting string/char array. I got the code working to remove the spaces but for some reason I am left with trailing characters left over from the original array. I even tried strncpy but I was having lots of errors.
Here is what I have so far:
#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*/
}
}
After removing the white spaces from the input
you have not terminated it with nul-terminator (\0
) because the new length is less than or equal to the original string.
Just nul-terminate it at the of end your for loop:
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;
}
这篇关于函数删除字符串/字符数组空间用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!