我想加上字符串数据类型的数字
I want to plus the number form string datatype
我有以下字符串输入:
Num: 12345
我想打印出输入的数字总和 (1+2+3+4+5 = 15):
total:15
我试过了,但是 for
循环中的 atoi()
有问题,我得到一个错误:
[Error] invalid conversion from 'char' to 'const char*'
我该如何解决这个问题或如何以更简单的方式解决它?
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char numstr[100];
int total = 0;
main(){
printf("Num:");
scanf("%s", numstr);
for(int i = 0; i < strlen(numstr); i++){
total += atoi(numstr[i]);
}
printf("%d", total);
}
atoi()
并不像您想象的那样。
一种可能的方法是使用 atoi()
或更好的 strtol()
将用户输入转换为整数类型,然后使用取模运算符提取每个数字和添加它们。
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(void){ //correct signature
int total=0; // no need to be global
char numstr[100] = {0}; // same
int ret = -1;
printf("Num:");
ret = scanf("%99s",numstr);
if (ret != 1){ // basic sanity check for scanf
printf("Error in scanning\n");
exit (-1);
}
long int converted = strtol(numstr, NULL, 10); // convert to integer
printf("%ld\n\n", converted);
for (int i = 0; converted; i++){
total += converted %10; // add the digit
converted /= 10; // reduce the last added digit
}
printf("%d",total);
return 0; // return 0 is implicit for `main()`, anyways
}
您可以将数字字符(在 ASCII 中)减去 0x30(即 ASCII 字符零“0”),以将 ASCII 数字字符转换为其十进制等效字符。
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char numstr[100];
int total=0;
main(){
printf("Num:");
scanf("%s",numstr);
for(int i = 0;i<strlen(numstr);i++){
total += numstr[i] - 0x30;
}
printf("%d",total);
}
字符串“12345”将是
1 -> 0x31 - 0x30 = 1
2 -> 0x32 - 0x30 = 2
3 -> 0x33 - 0x30 = 3
4 -> 0x34 - 0x30 = 4
5 -> 0x35 - 0x30 = 5
简单!将输入作为字符串并将字符减去'0'。这将为您提供该特定位置的号码。请参阅下面的代码:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char numstr[100];
int total=0;
main(){
printf("Num:");
scanf("%s",numstr);
for(int i = 0;i<strlen(numstr);i++){
total += (numstr[i]-'0');
}
printf("%d",total);
}
省去了使用 atoi() 或其他函数的麻烦。
解决您的问题的更简单方法是替换
total += atoi(numstr[i]);
和
total += numstr[i] - '0';
您可能想先检查 isdigit(numstr[i]) 是否为真,以进行错误检查。
您可以通过从数字字符中减去零字符来获得整数值。
total += numstr[i] - '0';
是因为0
字符的值等于十进制的48
(或十六进制的0x30
),1
字符等于49
十进制,2
和 50
等等..
从自身中减去 0
字符,得到 0
(十进制)。从字符 1
中减去 0
字符得到 1
(十进制)等等。
我有以下字符串输入:
Num: 12345
我想打印出输入的数字总和 (1+2+3+4+5 = 15):
total:15
我试过了,但是 for
循环中的 atoi()
有问题,我得到一个错误:
[Error] invalid conversion from 'char' to 'const char*'
我该如何解决这个问题或如何以更简单的方式解决它?
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char numstr[100];
int total = 0;
main(){
printf("Num:");
scanf("%s", numstr);
for(int i = 0; i < strlen(numstr); i++){
total += atoi(numstr[i]);
}
printf("%d", total);
}
atoi()
并不像您想象的那样。
一种可能的方法是使用 atoi()
或更好的 strtol()
将用户输入转换为整数类型,然后使用取模运算符提取每个数字和添加它们。
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(void){ //correct signature
int total=0; // no need to be global
char numstr[100] = {0}; // same
int ret = -1;
printf("Num:");
ret = scanf("%99s",numstr);
if (ret != 1){ // basic sanity check for scanf
printf("Error in scanning\n");
exit (-1);
}
long int converted = strtol(numstr, NULL, 10); // convert to integer
printf("%ld\n\n", converted);
for (int i = 0; converted; i++){
total += converted %10; // add the digit
converted /= 10; // reduce the last added digit
}
printf("%d",total);
return 0; // return 0 is implicit for `main()`, anyways
}
您可以将数字字符(在 ASCII 中)减去 0x30(即 ASCII 字符零“0”),以将 ASCII 数字字符转换为其十进制等效字符。
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char numstr[100];
int total=0;
main(){
printf("Num:");
scanf("%s",numstr);
for(int i = 0;i<strlen(numstr);i++){
total += numstr[i] - 0x30;
}
printf("%d",total);
}
字符串“12345”将是
1 -> 0x31 - 0x30 = 1
2 -> 0x32 - 0x30 = 2
3 -> 0x33 - 0x30 = 3
4 -> 0x34 - 0x30 = 4
5 -> 0x35 - 0x30 = 5
简单!将输入作为字符串并将字符减去'0'。这将为您提供该特定位置的号码。请参阅下面的代码:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char numstr[100];
int total=0;
main(){
printf("Num:");
scanf("%s",numstr);
for(int i = 0;i<strlen(numstr);i++){
total += (numstr[i]-'0');
}
printf("%d",total);
}
省去了使用 atoi() 或其他函数的麻烦。
解决您的问题的更简单方法是替换
total += atoi(numstr[i]);
和
total += numstr[i] - '0';
您可能想先检查 isdigit(numstr[i]) 是否为真,以进行错误检查。
您可以通过从数字字符中减去零字符来获得整数值。
total += numstr[i] - '0';
是因为0
字符的值等于十进制的48
(或十六进制的0x30
),1
字符等于49
十进制,2
和 50
等等..
从自身中减去 0
字符,得到 0
(十进制)。从字符 1
中减去 0
字符得到 1
(十进制)等等。