String-related operations-string operations for written test content

Source: Internet
Author: User

The first question about string operations ~~~~~~ Conversion between strings and Integers


Some Considerations for converting strings and integers:

1. The Null String "" and only one positive or negative number should be taken into account. For example, there is nothing behind "+ ".

2. At the same time, the maximum value of a positive integer is 0x7fff FFFF, and the minimum negative integer is 0x8000 0000. Therefore, you need to determine whether the integer overflow and underflow occur in two cases.


The following code converts a string to an integer:

# Include <stdio. h> # include <stdlib. h> Enum status {kvalid = 0, kinvalid}; int g_nstatus = kvalid; // use global variables to record whether the returned integer is valid long strtointcore (const char * digit, int minus) {long num = 0; while (* digit! = '\ 0') {// if the end is not read, continue to loop if (* digit> = '0' & * digit <= '9 ') {int flag = minus? -1: 1; num = num * 10 + flag * (* digit-'0 '); // If ((! Minus & num> 0x7fffffff) | (minus & num <(signed INT) 0x80000000) {num = 0; break; // exit the loop if removed.} digit ++;} else {num = 0; // If not, the number returned is 0 break; // exit while loop} If (* digit = '\ 0') {g_nstatus = kvalid;} return num;} int strtoint (const char * Str) {g_nstatus = kinvalid; int minus = 0; long num = 0; If (STR! = NULL & * Str! = '\ 0') {If (* STR =' + ') {STR ++;} else if (* STR = '-') {// if it is the symbol STR ++; minus = 1 ;}// if it is null after the +-symbol, if (* Str! = '\ 0') {num = strtointcore (STR, minus) ;}} return (INT) num ;}int main () {char * P; P = "123567 "; int M = strtoint (p); printf ("% d", m); System ("pause"); Return 0 ;}


String-related operations-string operations for written test content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.