2011-11-30 30 views
1

我是C新手。我在GWBASIC中經驗豐富。在努力學習,我試圖寫一個程序,將個別字符轉換成字符串到數值爲這樣:將字符串數組字符轉換爲數字

1 2 3 4 5 6 7 8 9 
a b c d e f g h i 
j k l m n o p q r 
s t u v w x y z 

例如,對於字符串用戶輸入可以是「狗」, 表示程序然後將字符串B中的[d] [o] [g]存儲爲[4] [6] [7]。 下面的代碼適用於字符串w /最多四個字符,但必須有更多這樣做的有效方法。

int main() 
{ 
    char a[0]; 
    char b[0]; 
    scanf("%s",a); 
    if (a[0] == 'a' || a[0] == 'j' || a[0] == 's') b[0] = '1'; 
    if (a[0] == 'b' || a[0] == 'k' || a[0] == 't') b[0] = '2'; 
    if (a[0] == 'c' || a[0] == 'l' || a[0] == 'u') b[0] = '3'; 
    if (a[0] == 'd' || a[0] == 'm' || a[0] == 'v') b[0] = '4'; 
    if (a[0] == 'e' || a[0] == 'n' || a[0] == 'w') b[0] = '5'; 
    if (a[0] == 'f' || a[0] == 'o' || a[0] == 'x') b[0] = '6'; 
    if (a[0] == 'g' || a[0] == 'p' || a[0] == 'y') b[0] = '7'; 
    if (a[0] == 'h' || a[0] == 'q' || a[0] == 'z') b[0] = '8'; 
    if (a[0] == 'i' || a[0] == 'r') b[0] = '9'; 
    if (a[1] == 'a' || a[1] == 'j' || a[1] == 's') b[1] = '1'; 
    if (a[1] == 'b' || a[1] == 'k' || a[1] == 't') b[1] = '2'; 
    if (a[1] == 'c' || a[1] == 'l' || a[1] == 'u') b[1] = '3'; 
    if (a[1] == 'd' || a[1] == 'm' || a[1] == 'v') b[1] = '4'; 
    if (a[1] == 'e' || a[1] == 'n' || a[1] == 'w') b[1] = '5'; 
    if (a[1] == 'f' || a[1] == 'o' || a[1] == 'x') b[1] = '6'; 
    if (a[1] == 'g' || a[1] == 'p' || a[1] == 'y') b[1] = '7'; 
    if (a[1] == 'h' || a[1] == 'q' || a[1] == 'z') b[1] = '8'; 
    if (a[1] == 'i' || a[1] == 'r') b[1] = '9'; 
    if (a[2] == 'a' || a[2] == 'j' || a[2] == 's') b[2] = '1'; 
    if (a[2] == 'b' || a[2] == 'k' || a[2] == 't') b[2] = '2'; 
    if (a[2] == 'c' || a[2] == 'l' || a[2] == 'u') b[2] = '3'; 
    if (a[2] == 'd' || a[2] == 'm' || a[2] == 'v') b[2] = '4'; 
    if (a[2] == 'e' || a[2] == 'n' || a[2] == 'w') b[2] = '5'; 
    if (a[2] == 'f' || a[2] == 'o' || a[2] == 'x') b[2] = '6'; 
    if (a[2] == 'g' || a[2] == 'p' || a[2] == 'y') b[2] = '7'; 
    if (a[2] == 'h' || a[2] == 'q' || a[2] == 'z') b[2] = '8'; 
    if (a[2] == 'i' || a[2] == 'r') b[2] = '9'; 
    if (a[3] == 'a' || a[3] == 'j' || a[3] == 's') b[3] = '1'; 
    if (a[3] == 'b' || a[3] == 'k' || a[3] == 't') b[3] = '2'; 
    if (a[3] == 'c' || a[3] == 'l' || a[3] == 'u') b[3] = '3'; 
    if (a[3] == 'd' || a[3] == 'm' || a[3] == 'v') b[3] = '4'; 
    if (a[3] == 'e' || a[3] == 'n' || a[3] == 'w') b[3] = '5'; 
    if (a[3] == 'f' || a[3] == 'o' || a[3] == 'x') b[3] = '6'; 
    if (a[3] == 'g' || a[3] == 'p' || a[3] == 'y') b[3] = '7'; 
    if (a[3] == 'h' || a[3] == 'q' || a[3] == 'z') b[3] = '8'; 
    if (a[3] == 'i' || a[3] == 'r') b[3] = '9'; 
    printf("%s\n",b); 
    return 0; 
} 
+0

'「d」 - 「a''等於'3'。 –

+0

請注意,在C中'='運算符是賦值,爲了檢查相等性,使用==運算符。你可以這樣if(strA [0] =='a'|| strA [0] =='j'|| strA [0] =='s')strB [0] = 1; ...以及其他情況下的其他情況,爲每個字符檢查一次使用for或while循環,而不是使用遞增值。 – akoso

+0

@rankep:我認爲salicemspiritus希望'strB [0] ='1''而不是'strB [0] = 1'。 – ruakh

回答

0

仔細看看ASCII表格。你會看到所有的字母都用一些整數值編碼。作爲一個開始,如果你只有小寫字母,這足以的。減去「A」從任何信得到你想要的

int nr = strA[0] - 'a' + 1; 
//now you'd need to convert back to a string; better: strB should be an array of integer 

而且字符代碼,=是賦值運算符;您需要使用==來檢查是否相等。

6

假設你的編譯器使用ASCII編碼,那麼你可以用下面簡單的算術,讓您的答案:

1 + (strA[i] - 'a') % 9 

你真的不想用if陳述或者確實是一個長長的清單來實現這個一個switch聲明。

當然,如果您有非字母字符,數字字符,大寫字母等,您將會遇到輸入驗證問題。我認爲你可以簡單地忽略那些學習練習。

+0

只是一個方面說明:根據提供的圖表,我相信OP將不得不'%9'這個值。 – williamg

+0

@williamg我的猜測是圖表需要更正! ;-) –

+0

我也會這麼想,但在他的評論中,他使用了第1-9列,而不考慮這一行。然而,我有一個壞習慣,假設的東西... ;-) – williamg

0

試試這個:)

int strB[MAX_LEN] = {0}; 
char *strA = malloc (MAX_LEN * sizeof(char)); 
int i,c = 0,x; 

scanf("%s",strA); 

for(i = 0 ; i<strlen(strA) ; i++){ 
    x = strA[i] - 'a' + 1; 
    if(x >= 1 && x <= 9) 
     strB[c] = x; 
    else if(x <= 18){ 
     strB[c] = x - 10; 
    else if(x <= 26){ 
     strB[c] = x - 19; 
    if(x <= 26) 
     c++; 
} 

,或者您可以使用ninjalj方法在for循環中,如果您已位於檢查輸入:

for(i=0 ; i<strlen(strA) ; i++){ 
    strB[i] = (strA[i] - 'a') % 9 + 1; 
} 

或本:

for(i=0 ; i<strlen(strA) ; i++){ 
    if(strA[i] >= 'a' && strA[i] <= 'z'){ 
     strB[c] = (strA[i] - 'a') % 9 + 1; 
     c++; 
    } 
} 
+0

爲什麼你動態分配'strA',如果'strB'是靜態分配的? – Dave

1

對於ASCII碼,它會有點像:

... make sure strB has enough space ... 
for (i = 0; i < ... length of strA ... ; i++) { 
    /* you should always check your input is valid */ 
    if (strA[i] >= 'a' && strB[i] <= 'z') 
     strB[i] = (strA[i] - 'a') % 9 + 1; 
    else 
     strB[i] = ... some default value ... 
} 

對於EBCDIC:

for (i = 0; i < ... length of strA ... ; i++) { 
    /* you should always check your input is valid */ 
    if (strA[i] >= 'a' && strB[i] <= 'r') 
     strB[i] = strA[i] & 0xF; 
    else if (strA[i] >= 's' && strB <= 'z') 
     strB[i] = (strA[i] & 0xF) - 1; 
    else 
     strB[i] = ... some default value ... 
} 
+0

+1 EBDIC不錯的作品! –

+1

@DavidHeffernan:呃,好和EBCDIC在同一句話中,誰會想到? – ninjalj

+0

我需要定義我嗎? 如果我不知道,我會得到一個錯誤。 – salicemspiritus

2

要糾正你原來的做法,你需要做兩件事情:

  • 各地使用的字符常量單引號;
  • 使用==檢查是否相等;
  • 終止對帳單;

...所以你的代碼段變爲:

if (strA[0] == 'a') 
    strB[0] = '1'; 
if (strA[0] == 'b') 
    strB[0] = '2'; 
if (strA[0] == 'c') 
    strB[0] = '3'; 
+0

正確。我從那以後學過這個,謝謝。 – salicemspiritus

2

您可以準確鍵入以下爲GWBASIC編輯器,它會解決你的問題

10 INPUT A$ 

12 L = LEN(A$) 

15 FOR T = 1 TO L 

20 M$ = MID$(A$,T,1) 

25 GOSUB 70 

30 B$ = B$ + V$ 

35 NEXT T 

40 PRINT B$ 

50 END 

55 REM ----------------- 

70 REM - Subroutine to convert m$ into v$ 

72 X = ASC(M$) : REM this is the ascii value of m$ (eg. "a" = 97) 

74 X = X - 96 : REM so that 97 becomes "1" 

80 IF X > 9 THEN X = X - 9 : GOTO 80 

90 V$ = STR$(X) : REM just converting to a string type variable 

95 RETURN : REM takes you back to line 30 where this value is added to the 

96 REM final resulting B$ - when I say added I mean a char added to a string 

97 REM such that "APPL" + "E" = "APPLE" 

98 REM ------------------------------------------ DONE