C 我无法删除字符串中的空格
我现在正在做reddit 现在我的问题是程序的底部没有完成,但我发布了整个程序,以防它可能与顶部有关 现在,我尝试从用户应该输入的字符串中删除空格,方法是将所有非空格字符放入另一个字符数组中。但是,每次我尝试打印带有假定删除空格的字符串时,它只打印第一个单词。一、 但是,您希望它将所有单词打印成一个组合单词 例如,当我尝试转换Hello World!,它应该给我一个地狱世界!,但它只是向我打招呼。我尝试过不同的方法来处理指针之类的东西,每次我都会遇到同样的问题。这是怎么回事C 我无法删除字符串中的空格,c,arrays,string,spaces,C,Arrays,String,Spaces,我现在正在做reddit 现在我的问题是程序的底部没有完成,但我发布了整个程序,以防它可能与顶部有关 现在,我尝试从用户应该输入的字符串中删除空格,方法是将所有非空格字符放入另一个字符数组中。但是,每次我尝试打印带有假定删除空格的字符串时,它只打印第一个单词。一、 但是,您希望它将所有单词打印成一个组合单词 例如,当我尝试转换Hello World!,它应该给我一个地狱世界!,但它只是向我打招呼。我尝试过不同的方法来处理指针之类的东西,每次我都会遇到同样的问题。这是怎么回事 #include &
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void)
{
char StringtobePacked[100]; //This is the string the user puts in
char StringtobePackedWOspaces[100]; //This is gonna be the string without spaces
int i , o , p; //incrementers/counters
double AmtfChrsnStrng;
//ask user for sentence input:
printf("Please type in sentence so I can pack it into the array:\n");
scanf("%s" , StringtobePacked);
//check for the amounts of letters in the sentence minus the spaces
for (i = 0; StringtobePacked[o] > i ; i++ , o++)
{
AmtfChrsnStrng++;
if (StringtobePacked[o] == ' ')
{
AmtfChrsnStrng--;
};
};
//now we know the amounts of letters in the sentence minus the spaces
//find the root and make that into the array size
double root = sqrt(AmtfChrsnStrng); //This is to find the square root of the (number) of letters in the string
int rootup = ceil(root); //this is to round up the double
char PackingArray[rootup][rootup]; //have a two dimensional array to pack sentence into
//make sure to check wether a sign is a space or not so as to not pack these
for (i = 0 , o = 0; StringtobePacked[i] != 0; i++)
{
if (StringtobePacked[i] != ' ')
{
StringtobePackedWOspaces[o] = StringtobePacked[i];
o++;
}
}
StringtobePackedWOspaces[o] = 0;
puts(StringtobePackedWOspaces);
//loop through the sentence in order to pack it into the array
// after end of column has been reached increment row
//now don't keep incrementing the column but increment it backwards so letters can be packed upwards
//print array by looping through , first columns then rows
//the starting position of the packing should be randomised
};
我建议您使用FGET而不是scanf
scanf("%s" , StringtobePacked);
用这个代替这个
fgets(StringtobePacked,100,stdin);
Beacause带有%s说明符的scanf将读取输入,直到遇到空白或字段宽度(如果已指定)。
这提供了所需的输出。您应该应用@ameyCU在其问题中发布的建议。您还应该将StringtobePacked[o]>i更改为StringtobePacked[o]!='\@BLUEPIXY在其评论中提到的“0”。除此之外,您还可以使用一些变量而不进行初始化。例如,在以下行中使用o和AmtfChrsnStrng,而不先将它们分别设置为0和0.0:
for (i = 0; StringtobePacked[o] > i ; i++ , o++)
{
AmtfChrsnStrng++;
...
为了安全起见,您应该更改:
int i , o , p; //incrementers/counters
double AmtfChrsnStrng;
致:
在调试模式下,代码可能在没有这些初始化的情况下工作,但在发布模式下可能会失败
也许你的代码有更多的问题,但这至少可以解决其中的一些问题
注意:如果您想改进循环,即计算句子中的字母数量减去空格,请查看。适合您的变量的解决方案如下所示:
for (i=0; StringtobePacked[i]; StringtobePacked[i]!=' ' ? i++ : *s++);
在执行这个for循环之后,变量i包含句子中的字母数量减去空格
char original[SIZE], packed[SIZE];
double amt = 0.0;
scanf("%f", original);
for (char * op = original, * pp = packed; *op; op++)
{
if (!isspace(*op))
{
*pp++ = *op;
amt++;
}
}
int dim = (int) ceil(sqrt(amt));
在C中处理字符串时,只需遍历源缓冲区和目标缓冲区中的指针。指针是C语言的精髓,请熟悉它们。这将使您的阵列尺寸保持在dim中。0 scanf%s,StringtobePacked;->扫描%99[^\n],字符串已打包;1 StringtobePacked[o]>i->StringtobePacked[o]!='\0'与scanf不同,fgets函数将换行符\n包含在StringToBacked中。在计算要打包的字符串中的字母时,必须考虑到这一点。例如,不要在\0处停止计数,但已经在\n处停止计数。是@honk您可以在没有换行符的情况下计数,然后我们可以执行此缓冲区[strcspnStringtobepacked,\n]=0;
char original[SIZE], packed[SIZE];
double amt = 0.0;
scanf("%f", original);
for (char * op = original, * pp = packed; *op; op++)
{
if (!isspace(*op))
{
*pp++ = *op;
amt++;
}
}
int dim = (int) ceil(sqrt(amt));