简体   繁体   English

我无法从字符串中删除空格

[英]I can't remove the spaces from a string

I'm currently doing this challenge on reddit. 我目前正在reddit上进行这项挑战

Now my problem is on the bottom of the program (unfinished), but I posted the whole thing in case it might have something to do with the top of it. 现在我的问题在程序的底部(未完成),但我将整个内容发布了出来,以防它可能与程序的顶部有关。

Now I'm trying to remove spaces from the string the user is supposed to enter, by putting all the non-space characters into another character array. 现在,我试图从用户应通过将所有的非空格字符到另一个字符数组进入,字符串中删除空格。 However, every time I print try to print the string with the supposed removed spaces, it only prints the first word. 但是,每次我打印时尝试打印带有假定已删除空格的字符串,它只会打印第一个单词。 I, however, want it to print all the words into one single combined word. 但是,我希望它将所有单词打印到一个组合单词中。

For example, when I try to convert Hello World! 例如,当我尝试转换Hello World! , it's supposed to give me HelloWorld! ,应该可以给我HelloWorld! , but it's only giving me Hello . ,但这只是给我Hello I've tried different methods with pointers and whatnot and every time I face the same problem. 每当遇到相同的问题时,我都尝试过使用指针和诸如此类的不同方法。 What's going on here? 这里发生了什么?

#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
};

I would suggest you to use fgets() instead of scanf(). 我建议您使用fgets()而不是scanf()。

scanf("%s" , StringtobePacked);

Instead of this use this 代替这个使用

fgets(StringtobePacked,100,stdin);

Beacause scanf() with %s specifer will read input till it encounters whitespace or upto field width if it is specified . 因为具有%s specifer的scanf()将读取输入,直到遇到空格或达到指定字段宽度为止。 This gave the desired output. 这给出了期望的输出。

You should apply the suggestion as posted by @ameyCU in their question. 您应该应用@ameyCU在他们的问题中发布的建议。 You should also change StringtobePacked[o] > i to StringtobePacked[o] != '\\0' as mentioned by @BLUEPIXY in their comment. 您还应该将StringtobePacked[o] > i更改为StringtobePacked[o] != '\\0'如@BLUEPIXY在其注释中所述。 In addition to that you use some variables without initializing them. 除此之外,您还可以使用一些变量而不初始化它们。 For example, you use o and AmtfChrsnStrng in the following lines without first setting them to 0 and 0.0 respectively: 例如,在以下各行中使用oAmtfChrsnStrng而不先将它们分别设置为00.0

for (i = 0; StringtobePacked[o] > i ; i++ , o++)
{
    AmtfChrsnStrng++;
    ...

To be on the safe side, you should change: 为了安全起见,您应该更改:

int i , o , p; //incrementers/counters
double AmtfChrsnStrng;

to: 至:

int i=0, o=0, p=0; //incrementers/counters
double AmtfChrsnStrng = 0.0;

Your code might work without these initializations in debug mode, but in release mode it is likely to fail. 在调试模式下,如果没有这些初始化,您的代码可能会起作用,但是在发布模式下,它很可能会失败。

Maybe there are more issues with your code, but this should at least fix some of them. 也许您的代码还有更多问题,但这至少应该可以解决其中的一些问题。

Note: In case you want to improve your loop which counts the amount of letters in the sentence minus the spaces, please take a look here . 注意:如果您想改善循环计算方式(该循环计算的是句子中的字母数量减去空格),请在此处查看 This solution adapted to your variables would look like this: 适用于您的变量的解决方案如下所示:

for (i=0; StringtobePacked[i]; StringtobePacked[i]!=' ' ? i++ : *s++);

After executing this for loop, the variable i contains the amount of letters in the sentence minus the spaces. 执行完此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));

When processing strings in C, just walk pointers across the source and destination buffers. 在C语言中处理字符串时,只需在源缓冲区和目标缓冲区中遍历指针即可。 Pointers are the very essence of C, get comfortable with them. 指针是C的本质,请对它们感到满意。 This will leave you with the array dimensions in dim . 这将使您的数组维度为dim

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM