【发布时间】:2021-10-21 23:48:08
【问题描述】:
我有一个要解决的问题。我有一个字符串和整数数组,我只想将整数(163)转换为 C 中的实际整数。
我已设法找到我想要的数字 (163) 数组位置,但我不确定如何将它们转换为数字。我曾尝试使用 strtol、atoi 和 strtoumax,但没有成功。 我在下面添加了我的代码。
char busy[30] = {"this is; it was; 163; 234;;"};
int tag = 0;
int location = 0;
for (int i = sizeof(busy); i > 0; i--) {
printf("%c\n", busy[i]);
if (busy[i] == ';') {
tag = tag+1;
printf("tag is %i \n", tag);
}
if (tag == 4) {
printf("for loop is %i\n", i);
location = i;
break;
}
}
location = location+1;
int loca_saved = 0;
int loca_finish = 0;
int tag1 = 0;
while (busy[location] != ';') {
if (busy[location] == ' ' && busy[location - 1] == ';') {
//printf("not printing whitespace between semicolon and characters\n");
location++;
}
else {
if (tag1 == 0) {
tag1 = tag1+1;
loca_saved = location; //this is to tell me the array location for the first char
}
if (busy[location + 1] == ';') {
loca_finish = location; //this is to tell me the array location for the last char
}
putchar(busy[location]); //this is to print my desired characters(163)
location++;
}
}
【问题讨论】:
-
“我尝试使用...”。不幸的是,您没有向我们展示您是如何尝试的,或者您的尝试以何种方式失败。您的代码不包含任何转换您的数字的尝试。
标签: arrays c string sorting integer