Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert a string to an integer in C language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to convert a string into an integer in C language. the article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

1 topic

Function: fun ()

Function: convert a string to an integer

Description:

[cannot use string function provided by C language]

Enter: string "- 1234"

Output: integer-1234

2 train of thought

Idea: convert the characters at each position of the string to the corresponding ASCII code.

For example, if the decimal integer corresponding to the character '0percent color 9' is 48,57, then subtract the corresponding integer by 48 to get the corresponding integer.

Binary decimal character 0011 00483000011000149311001100105032200110011513330011 0100523440011 0101533550011 0110543660011 0111553770011 1000563880011 1001573993 code # include # include/** function: fun () function: convert string to an integer description: [string function provided by C language cannot be used] input: string "- 1234" output: integer-1234**/long fun (char * p) {int r = 0; / number of digits long res = 0L / / converted digits int pos = 1; / / digits * 10 * 100. Int size = strlen (p); / / string length if (p [0] = ='-') {r = size-1;} else {r = size;} / / conversion from forward to backward needs to be calculated on a case-by-case basis. That is, if it is not a negative number, it needs to start from p [0] and if it is a negative number, it needs to start from p [1]. So, it can be calculated from back to front, and the number of cycles is r for (int I = 0; I < r; + + I) {res + = (p [size-1Mui]-48) * pos; pos * = 10;} return p [0] = ='-'?-res:res } int main (int argc, char const * argv []) {char s [6]; printf ("Enter a string:"); gets (s); long res = fun (s); printf ("Convert Result:% ld\ n", res); return 0;}

Example result:

$gcc ex1.c-o demo$. / demoEnter a string:-1234Convert Result:-1234 $. / demoEnter a string: 9089Convert Result: 9089 the above is how to convert a string into an integer in the C language shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report