Home > Backend Development > C++ > body text

How to convert string to integer in C language

王林
Release: 2023-09-05 16:41:06
forward
1777 people have browsed it

How to convert string to integer in C language

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

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

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

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

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

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

您现在可以应用所需的过滤器来创建一个包含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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!