Maison  >  Article  >  développement back-end  >  Comment convertir une chaîne en entier en langage C

Comment convertir une chaîne en entier en langage C

王林
王林avant
2023-09-05 16:41:061745parcourir

Comment convertir une chaîne en entier en langage C

首先使用strchr()函数从左括号 '(' 中提取字符。

char *name="The Matrix(1999)";
char *ps;
ps=strchr(name,'(');

然后将括号 () 内的每个字符添加到 char 数组

char y[5]=""; int  p;
for (p=1;p<strlen(ps+1);p++) {
   y[p-1]=ps[p];
}
y[4]=&#39;\0&#39;;

最后使用atoi()函数将结果字符串转换为整数

year=atoi(y);
printf("year=%d",year);

您现在可以应用所需的过滤器来创建一个包含2008年之前所有电影的字符串数组

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer