C 获取字符串直到第一个数字
我需要从数组中获取第一个数字之前的所有字符。 我做到了这一点,它似乎工作正常:C 获取字符串直到第一个数字,c,arrays,string,C,Arrays,String,我需要从数组中获取第一个数字之前的所有字符。 我做到了这一点,它似乎工作正常: #include <stdio.h> int main() { char temp[128] = {0}; char str_active[128] = {0}; sprintf(temp, "%s", "AB01"); printf("Complete string.: %s\n", temp); int len = sizeof(temp) / sizeo
#include <stdio.h>
int main() {
char temp[128] = {0};
char str_active[128] = {0};
sprintf(temp, "%s", "AB01");
printf("Complete string.: %s\n", temp);
int len = sizeof(temp) / sizeof(char);
int index = 0;
while (index < len) {
if (isdigit(temp[index])) {
break;
} else {
index++;
}
}
snprintf(str_active, index + 1, "%s\n", temp);
printf("String before first digit.: %s\n", str_active);
return 0;
}
#包括
int main(){
字符温度[128]={0};
char str_active[128]={0};
sprintf(温度,“%s”,“AB01”);
printf(“完整字符串:%s\n”,临时);
int len=sizeof(temp)/sizeof(char);
int指数=0;
while(指数
我想知道我是否可以用更少的指令来做同样的事情,以便以更好的方式来做。该函数可以为您做:
函数的作用是:计算完全由不在reject中的字节组成的s的初始段的长度
#包括
#包括
int main(){
字符温度[128]={0};
char str_active[128]={0};
sprintf(温度,“%s”,“AB01”);
printf(“完整字符串:%s\n”,临时);
strncpy(str_活动,临时,strcspn(临时,“0123456789”));
printf(“第一位数字前的字符串:%s\n”,str\u处于活动状态);
返回0;
}
这是我得到的:“完整字符串:第一位数字前的AB01字符串。:AB”使用--@pmg Great!谢谢!@马修·布内尔,谢谢!
#include <stdio.h>
#include <string.h>
int main() {
char temp[128] = {0};
char str_active[128] = {0};
sprintf(temp, "%s", "AB01");
printf("Complete string.: %s\n", temp);
strncpy(str_active, temp, strcspn(temp, "0123456789"));
printf("String before first digit.: %s\n", str_active);
return 0;
}