Home  >  Article  >  Backend Development  >  How to convert string to integer in C language

How to convert string to integer in C language

王林
王林forward
2023-09-05 16:41:061745browse

How to convert string to integer in C language

首先使用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年之前所有电影的字符串数组

The above is the detailed content of How to convert string to integer in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete