C language 10-character and string common handler functions

Source: Internet
Author: User
Tags strcmp

    • One, character processing function
    • Second, string processing function

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore.

One, character processing function

The two character handler functions described below are declared in the stdio.h header file.

1. Character output function Putchar

Putchar (65); Aputchar (' A '); Aint a = 65;putchar (a); A

In the above 3 usages, the output is capital A.

* Putchar can only output one character at a time, while printf outputs more than one character at a time

printf ("%c%c%c", ' a ', ' B ', ' a ');

2. Character input function GetChar

Char c;c = GetChar ();

GetChar will give the user input character assigned value to the variable C.

* The GetChar function can read into spaces and tabs until it encounters a carriage return. SCANF cannot read into spaces and tabs.

* GetChar can only read one character at a time. SCANF can receive multiple characters at the same time.

* GetChar can also read into the carriage return line break, this time you have to hit 2 times enter. The return line of the 1th knock is read into the GetChar, and the 2nd hit of the Enter key represents the end of the input.

Second, string processing function

The string handler functions described below are declared in the String.h header file, which is included in the header file before use.

1.strlen function

* This function can be used to measure the number of characters in a string, excluding the

1 int size = strlen ("MJ"); Length of  3 char s1[] = "LMJ"; 4 int size1 = strlen (S1);///Length of  6 char s2[] = {' m ', ' j ', ' + ', ' l ', ' m ', ' j ', ' + ' };7 int size2 = strlen (s2); Length of 2

Looking at line 7th, the Strlen function calculates the number of characters from the first address of S2 until a null character is encountered. Because S2 's 1th is only 2 characters MJ before, so the length is 2.

2.strcpy function

1 Char s[10];2 strcpy (S, "LMJ");

The strcpy function copies the "LMJ" string on the right to the character array S. Start with the first address of S and copy the characters one by one until it is copied to the. Of course, the tail of s will definitely keep a.

* Suppose that there are several \0,strcpy functions in the right string that will only copy the contents of the 1th, and the contents of the following are not copied

1 char s[10];2 char c[] = {' m ', ' j ', ' mm ', ' l ', ' m ', ' j ', '};3 ' strcpy (S, c);

The contents of the last string s are: MJ

3.strcat function

Char s1[30] = "Love"; Strcat (S1, "OC");

The Strcat function will stitch the "OC" string on the right to the end of the S1, and the final S1 will become "LOVEOC"

The STRCAT function starts the connection string from the 1th character of S1, and the 1th character of the S1 is overwritten by the string on the right, and the end of the S1 is retained after the connection is completed.

* Note the following situation

1 char s1[30] = {' L ', ' m ', ' j ', ' + ', ' L ', ' o ', ' V ', ' e ', ' s1 '};2 strcat (S1, "OC"); 3 printf ("%s",);

The 1th line initializes the S1 with 2, and after the 2nd row of the Strcat function, the output is:

4.strcmp function

* This function can be used to compare the size of a 2 string

* Call Form: strcmp (String 1, String 2)

* Two strings are compared from left to right (by the size of the ASCII values of the characters) until the characters are different or meet the ' + '. If all characters are the same, the return value is 0. If not, returns the difference in the ASCII value of the first character in the two string. That is, the string 1 is greater than the string 2 o'clock the return value of the function is positive, otherwise negative.

1 char s1[] = "abc"; 2 char s2[] = "abc"; 3 char s3[] = "abc"; 4 char s4[] = "CCB"; 5 6 printf ("%d,%d,%d", strcmp (S1, S2), S TRCMP (S1, S3), strcmp (S1, S4));

Output Result:

    • S1 and S2 are the same, so return 0
    • S1 and S3 are the 2nd characters, the ASCII value of B is 98,b ASCII value is 66,b-b = 32, so return 32
    • S1 and S4 are the 1th characters are not the same, the ASCII value of a is 97,c ASCII value is 99,a-c =-2, so return-2

C language 10-character and string common handler functions

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.