【问题标题】:I can't remove the spaces from a string我无法从字符串中删除空格
【发布时间】:2015-09-22 17:15:25
【问题描述】:

我目前在 reddit 上做 this challenge

现在我的问题在程序的底部(未完成),但我发布了整个内容,以防它可能与它的顶部有关。

现在我试图通过将所有非空格字符放入另一个字符数组中来从用户应该输入的字符串中删除空格。但是,每次我打印尝试打印带有假定已删除空格的字符串时,它只打印第一个单词。但是,我希望它将所有单词打印成一个单独的组合单词。

例如,当我尝试转换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
};

【问题讨论】:

  • 0) scanf("%s" , StringtobePacked); --> scanf("%99[^\n]" , StringtobePacked); 1) StringtobePacked[o] &gt; i --> StringtobePacked[o] != '\0'

标签: c arrays string spaces


【解决方案1】:

我建议你使用 fgets() 而不是 scanf()。

scanf("%s" , StringtobePacked);

用这个代替这个

fgets(StringtobePacked,100,stdin);

因为带有 %s 说明符的 scanf() 将读取输入,直到遇到空格或指定字段宽度为止。 这给出了所需的输出。

【讨论】:

  • scanf 相比,函数fgets 将换行符\n 包含在StringtobePacked 中。在计算StringtobePacked 中的字母时必须考虑这一点。例如,不要在\0 处停止计数,而应在\n 处停止计数。
  • 是的@honk 你是对的,没有换行符,那么我们可以做到这一点buffer[strcspn(Stringtobepacked, "\n")] = 0;
【解决方案2】:

您应该应用@ameyCU 在他们的问题中发布的建议。正如@BLUEPIXY 在他们的评论中提到的,您还应该将StringtobePacked[o] &gt; i 更改为StringtobePacked[o] != '\0'。除此之外,您使用一些变量而不初始化它们。例如,您在以下行中使用oAmtfChrsnStrng,而没有先将它们分别设置为00.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;

在调试模式下,您的代码可能会在没有这些初始化的情况下工作,但在发布模式下它可能会失败。

也许您的代码存在更多问题,但这至少应该可以解决其中的一些问题。

注意:如果您想改进计算句子中字母数量减去空格的循环,请查看hereThis 适用于您的变量的解决方案如下所示:

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

执行此for 循环后,变量i 包含句子中的字母数量减去空格。

【讨论】:

    【解决方案3】:
    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 中的数组维度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 2017-11-20
      相关资源
      最近更新 更多