簡體   English   中英

我的程序無法正確處理字符串的值

[英]My program doesn't manipulate a string's values correctly

我需要編寫一個程序來獲取字符串的輸入值,然后它會忽略不是數字的字符,並使用字符串中的數字來創建一個整數並顯示它。 如練習中所述,這里有一些字符串變成了整數

我想像通過向量一樣遍歷字符串,然后使用isdigit(s [i])測試每個位置是否為數字,然后將這些值放在另一個向量中,該向量使用這些數字創建數字。 最后應該輸出數字。 我不能一生都想出什么問題了,請幫忙。

#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
    char *s;
    scanf("%s", s);
    printf("%s\n", s);

    int i, n=0, v[100], nr=0;
    for(i=0; i<strlen(s); i++)
    {
        if (isdigit(s[i]) == 1)
        {
            v[i] = s[i];
            n++;
        }
    }
    for(i=0;i<n;i++)
    {
        printf("%c\n", v[i]);
    }

    for(i=0; i<n; i++)
    {
        nr = nr * 10;
        nr = nr + v[i];
    }
    printf("%d", nr);
    return 0;
}

指針s未初始化,這是您的主要問題。 但是,還有其他問題。

  • isdigit()被記錄為返回非零返回碼,該返回碼不一定為1。

  • 需要將isdigit()的參數isdigit()轉換為unsigned char以避免潛在的不確定行為。

  • 您的數組v也使用相同的索引變量i這是不對的。 存儲數字時,請使用其他變量為v編制索引。

  • 您需要減去'0'來獲得等價的每個數字。

  • scanf()的格式%s無法處理帶有空格的輸入(除其他問題外)。 因此,請使用fgets()


#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(void)
{
char s[256];
fgets(s, sizeof s, stdin);
s[strcspn(s, "\n")] = 0; /* remove trailing newline if present */
printf("%s\n", s);

int i, n = 0, v[100], nr = 0;
size_t j = 0;
for(i = 0; i < s[i]; i++)
{
    if (isdigit((unsigned char)s[i]))
    {
        v[j++] = s[i];
        n++;
    }
}
for(i = 0;i < j; i++)
{
    printf("%c\n", v[i]);
}

if (j) { /* No digit was seen */
    int multiply = 1;
    for(i= j-1 ; i >= 0; i--) {
        nr = nr + (v[i] - '0') * multiply;
        multiply *= 10;
    }
}

printf("%d", nr);
return 0;
}

另外請注意,如果輸入中包含太多數字,則nr的整數溢出(和/或multiply )將不成立。

問題的另一個潛在根源是,如果輸入超過100位數字,它將溢出數組v ,導致無法定義的行為

非常感謝您的幫助,我按照某人的建議更換了
v [i] = s [i]-> v [n] = s [i]並用char s [100]更改char * s現在可以正常工作,我擺脫了變量nr,只輸出數字而不分離他們通過\\ n。 也感謝調試器的評論,我不知道我可以有效地使用它。

首先,您沒有分配任何內存,我將其更改為固定數組。

您對scanf使用將在第一個空格處停止(如在第一個示例輸入中一樣)。

接下來,在將數字寫入int v[]時,不會使用正確的數組索引。 但是,我刪除了所有這些內容,只使用了出現的任何數字。

您沒有閱讀isdigit的手冊頁。 不會為數字返回1 它返回一個非零值,因此我刪除了顯式測試,並將其保留為非零結果的隱式測試。

我將字符串的長度和循環類型更改為size_t ,移動了循環的多個strlen調用。

您還沒有看到數字的字符值不是整數值,因此我減去了'0'來使它們成為整數。

最后,我將目標類型更改為unsigned因為您將忽略任何減號。

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(void)
{
    char s[100];                              // allocate memory
    unsigned nr = 0;                          // you never check a `-` sign
    size_t i, len;                            // correct types
    if(fgets(s, sizeof s, stdin) != NULL) {   // scanf stops at `space` in you example
        len = strlen(s);                      // outside of the loop
        for(i=0; i<len; i++) {
            if(isdigit(s[i])) {               // do not test specifically == 1 
                nr = nr * 10;
                nr = nr + s[i] - '0';         // character adjustment
            }
        }
        printf("%u\n", nr);                 // unsigned
    }
    return 0;
}

計划會議:

a2c3   8*5+=
2385

只是用這個

#include <stdio.h>
#include <ctype.h>

int main()
{
    int c;
    while ((c = getchar()) != EOF) {
        switch (c) {
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
        case '\n':
            putchar(c); break;
        }
    }
    return 0;
} /* main */

這是一個示例執行:

$ pru_$$
aspj pjsd psajf pasdjfpaojfdapsjd 2 4 1
241
1089u 0u 309u1309u98u 093u82 40981u2 982u4 09832u4901u 409u 019u019u 0u3 0ue 
10890309130998093824098129824098324901409019019030

十分優雅! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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