将字符串数组字符转换为数字
我是 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
例如,字符串 A 的用户输入可能是“dog”, 然后,该程序会将 [d][o][g] 作为 [4][6][7] 存储在字符串 B 中。 下面的代码适用于最多四个字符的字符串,但必须有一种更有效的方法来执行此操作。
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;
}
I am new to C. I am experienced in GWBASIC. In an effort to learn, I am attempting to write a program that will convert the individual chars in a string to a numerical value as so:
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
For example, user input for string A could be 'dog',
said program would then store [d][o][g] as [4][6][7] in string B.
The below code works for a string w/up to four chars, but there must be a more efficient way of doing this.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假设您的编译器使用 ASCII 编码,那么您可以使用以下简单的算术来获得答案:
您确实不想使用一长串
if
语句或实际上是来实现此目的switch 语句。
如果您有非字母字符、数字字符、大写字符等,那么您自然会遇到输入验证问题。我想你可以简单地忽略这些来进行学习练习。
Assuming that your compiler uses an ASCII encoding then you can use the following simple arithmetic to get your answer:
You really don't want to implement this with a long list of
if
statements or indeed aswitch
statement.Naturally you will have input validation issues if you have non alphabetical characters, numeric characters, upper-case characters and so on. I presume you can simply ignore those for a learning exercise.
要纠正您原来的方法,您需要做两件事:
==
检查相等性;;
终止语句。...所以你的片段变成:
To correct your original approach, you need to do two things:
==
to check for equality;;
.... so your snippet becomes:
您可以在 gwbasic 编辑器中准确输入以下内容,它将解决您的问题
You can type the following exactly into a gwbasic editor and it will solve your problem
对于 ASCII,它有点像:
对于 EBCDIC:
For ASCII, it would go somewhat like:
For EBCDIC:
仔细看看 ASCII 表。您将看到所有字母都用某个整数值进行编码。首先,如果您只有小写字母,则只需从任何字母中减去“a”的字符代码即可得到您想要的内容
此外,
=
是赋值运算符;您需要使用==
来检查相等性。Have a closer look at an ASCII table. You'll see that all letters are encoded with some integer value. As a start, if you only have lower case letters, it's enough to substract the character code of 'a' from any letter to get what you want
Also,
=
is the assignment operator; you need to use==
to check for equality.试试这个:)
或者你可以在for循环中使用ninjalj方法,如果你已经检查了输入:
或者这个:
try this :)
or you can use ninjalj approach in the for loop, if you aleady checked the input:
or this :