[英]I can't remove the spaces from a string
我目前正在reddit上进行这项挑战 。
现在我的问题在程序的底部(未完成),但我将整个内容发布了出来,以防它可能与程序的顶部有关。
现在,我试图从用户应通过将所有的非空格字符到另一个字符数组进入,字符串中删除空格。 但是,每次我打印时尝试打印带有假定已删除空格的字符串,它只会打印第一个单词。 但是,我希望它将所有单词打印到一个组合单词中。
例如,当我尝试转换Hello World!
,应该可以给我HelloWorld!
,但这只是给我Hello
。 每当遇到相同的问题时,我都尝试过使用指针和诸如此类的不同方法。 这里发生了什么?
#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
};
我建议您使用fgets()而不是scanf()。
scanf("%s" , StringtobePacked);
代替这个使用
fgets(StringtobePacked,100,stdin);
因为具有%s specifer的scanf()将读取输入,直到遇到空格或达到指定字段宽度为止。 这给出了期望的输出。
您应该应用@ameyCU在他们的问题中发布的建议。 您还应该将StringtobePacked[o] > i
更改为StringtobePacked[o] != '\\0'
如@BLUEPIXY在其注释中所述。 除此之外,您还可以使用一些变量而不初始化它们。 例如,在以下各行中使用o
和AmtfChrsnStrng
而不先将它们分别设置为0
和0.0
:
for (i = 0; StringtobePacked[o] > i ; i++ , o++)
{
AmtfChrsnStrng++;
...
为了安全起见,您应该更改:
int i , o , p; //incrementers/counters
double AmtfChrsnStrng;
至:
int i=0, o=0, p=0; //incrementers/counters
double AmtfChrsnStrng = 0.0;
在调试模式下,如果没有这些初始化,您的代码可能会起作用,但是在发布模式下,它很可能会失败。
也许您的代码还有更多问题,但这至少应该可以解决其中的一些问题。
注意:如果您想改善循环计算方式(该循环计算的是句子中的字母数量减去空格),请在此处查看 。 适用于您的变量的此解决方案如下所示:
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 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.