簡體   English   中英

我無法從字符串中刪除空格

[英]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在其注釋中所述。 除此之外,您還可以使用一些變量而不初始化它們。 例如,在以下各行中使用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;

在調試模式下,如果沒有這些初始化,您的代碼可能會起作用,但是在發布模式下,它很可能會失敗。

也許您的代碼還有更多問題,但這至少應該可以解決其中的一些問題。

注意:如果您想改善循環計算方式(該循環計算的是句子中的字母數量減去空格),請在此處查看 適用於您的變量的解決方案如下所示:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM