C--(13) string

Source: Internet
Author: User
Tags strcmp strtok uppercase letter

Knowledge Points:
1. Character and character processing functions
2. Nature of strings and string input and output functions
3. String Conversion Functions
4. Commonly used string processing functions
5. String Lookup function
6. String splitting function
7. String substitution
8. Common string processing function implementation
9.SSCANF sprintf function

================================
C must first be defined after the type is used, there is no string type, strings with a string of characters, the base element is the character

"Xiaoqiang\0"

Character and character processing
1 characters commonly used characters
Numeric characters: ' 0 ' 1 '-' 9 '
Alphabetic characters: ' M ' F ' a '-' Z ' a '--' z '
Other characters: ' + '-' * '/'! '

ASCII code for 2 characters commonly used characters
Numeric characters: 0 ' 0 ' =48-' 9 ' = 48 + 9
Alphabetic characters: ' A ' = 97--' Z ' = + + ' A ' = ' z ' = 65 + 25 = 90
Characters commonly used: ' = + ' \ n '

3. Review the ASCII code


4. Nature of characters

The computer stores the data in binary, how are the characters stored?

Thinking: sizeof (char) and sizeof (' a ') = sizeof (97) = 4


1) The nature of the character is an ASCII code of type int
sizeof (' a ') sizeof ("a")

2) The character variable holds the ASCII code, but the ASCII code only needs one byte to save
In all compilation systems, one byte is required to hold a character, and character data is stored in ASCII code.

3. Using Character judgment functions
IsDigit Numeric characters
Isalpha Letter (case)
Isalnum numeric characters and letters
Islower Small Letter
Isupper Capital Letters
Isspace Space character
0--Indicates that the test is not valid
1--Indicates that the test was established
Practice:
1. Randomly enter a string that contains uppercase and lowercase letters, numeric characters, and spaces, counting the number of letters, capitals, lowercase, and spaces

4. Using character conversion function//verification code
ToUpper Lowercase conversion to uppercase
ToLower uppercase to lowercase



Practice:
1. Randomly enter a string containing uppercase and lowercase letters and spaces, convert the original uppercase letter to lowercase, and lowercase to uppercase.

=================================
String
"Hello"
Use a string of characters to '
Two ways of expressing
Stack
A, one or more characters with "" string together, stored in a character array, the system by default add '.

Read-only data segment
B, not as a constant when referencing a character array

1. The nature of strings
1) string nature is a string constant
"Hello World", "Hello World"

2. Thinking: sizeof (' a ') and sizeof ("a")
Character constants are processed in 4 bytes
String constants to save memory space, all characters in a string are treated as char types

3.char *str represents the difference between a string and a char str[]


=================================
Input and output of strings
1. Input and output of strings
0) string input/Output function
1> recall scanf Get string
%c scanf ("%s"); String
%s printf ("%s", addr)
2> how to get a string with spaces
%[^ \ n] Regular expression

3> putsstring
4> string Output
The puts function adds a newline symbol after the string output.
Puts (str) = printf ("%s\n", str);
1) string input and overflow problem array out of bounds
Thinking: The length of the character array at the time of input
2) Workaround
Fgets
Usage: fgets (buf, Len, stdin); stdin stdout stderr Two kinds 1 Accept enter 2 accept
Gets a string of the specified length from standard input, containing the string terminator
(1) define an array of length 10, enter a string of characters, include a space carriage return, and output

=================================
String conversion function "100" + "200" = = = 100 + 200 = 300
Requirement: Now the user enters an arithmetic expression that asks for the corresponding result
Thinking: How to convert a character number in a string to a real number
1. String conversion function%d%f%o% #x% #o
1) atoi
How the Atoi function works
Starts the conversion from the first non-whitespace digit character until the first non-numeric characters end
Atol
Atof Atof returns double

2) Implement the Atoi function (supports positive and negative numbers)
Example: Implementing a negative conversion of single-digit numbers
Exercise: Char *str1 = "98765"
Char *str2 = "54321"
ret = 98765+ 54321

"101 0110" decimal 10 10110
Binary 86
Octal 8^6 + 8^4 + 8^2 + 8



3) strtol function 1010 0x123456

Enhanced version of 1>atoi 1010a1001
Long Strtol (char *str,char **p, int base);


Data that does not appear in the string that is incompatible with the current binary
1>atoi's enhanced version 2, 8, 10, 16

2> function Description: Converts the specified string to a 10 binary according to the base's binary mode
Base range is 2~36
Exercise: Enter a string, enter an integer number, and convert the string into integers by shaping the number of characters

====================================
String Common functions
1. String length calculation function "Hello"
Requirements: Users need to know the length of the number of characters entered
1) strlen function using = = string length

2) Thinking: The difference between strlen and sizeof
(a) strlen is a string function that only tests strings, and sizeof is an operator that can calculate any type of data
(b) The actual length of the string as seen in strlen, and sizeof calculates the length of the actual memory occupied
3) Think: strlen length of the size '

Char arr[10] = "ABC"


2. String copy function strcpy = = string copy
Notepad command + C and Comand + V

Related instructions: Use note

strncpy (dst,src,n) Copy the first n characters from SRC to src

Thinking: Whether strncpy will contain a terminator after a copy
Exercise: Implementing a String Copy module


3. Searching for individual characters
Find a character in Notepad for example

<a>str CH r character Lookup function
STRCHR finds the address where a character first appears, calculates the subscript of an array based on an address, or outputs from a specified character.


<B>STRRCHR find the last address where the character appears
Char *p1 = STRRCHR (a,c);
printf ("%s", p1);
Practice:
Enter a string, enter a character, find and print the first occurrence and last occurrence of the character and output the address of the character
Char Char


4.STRSTR string Lookup to [find a word in Notepad]

(a) Strstr string lookup function
(b) STR case STR string look-up function, insensitive to case


5.STRCMP string comparison function
Sort and find contacts for mobile phone correspondence contacts
STR CMP string comparison function

int strcmp (const char *s1,const char *S2)
String compare its function is to compare strings S1,S2

A character-by-character comparison from left to right the ASCII code difference of the characters with different return values

function Similarity function:
strcmp
strcasecmp
strncmp
strncasecmp

int strncmp (const char *S1, const char *S2, int n);
1) Comparison order: S1 is the same as S2
2) Comparison results: 0 is the same, not 0 is different, and the corresponding difference is returned
Thinking: What does a non-0 result mean
3) Thinking: What happens when S1 is longer than S2 length?
Returns the difference between the last character of the S1 and the S2 terminator
Practice:
1. Write a simulation login program, enter the user name and password, determine the user name and password
is consistent with the set


2. Write a validator to determine whether the user-entered string starts with Qianfeng



6.strcat String Connection
Copy one text to the back of another text.
The Strcat string connection function is used to concatenate two strings.

char * strcat (char *str1,char *str2)
"Related notes": Principle of connection


Char a[30] = "Qianfeng"
Char b[] = "Hulian"
Strcat (A, b);
Puts (a);

char * strcat (char *str1,char *str2, N); Connect prompt the first n characters in str2 to the back of STR1
Strcat,strncat
Exercise: First enter your last name, then enter the name, and finally the connected name output

Requirement: Users now need to split a string in a certain format

7. String splitting function

1) strtok use
char * strtok (char *str,const char *delim);
Decomposition of a set of strings, Str is the string to be decomposed, Delim is a string, the first call, str to point to the string to be split, and then re-call to set the STR to NULL, strtok in STR to find the Delim character and
To replace it until the entire string is searched.
1> the content to be split
2> separator, split marker
2) How to get the next string
3) Note
0> cannot intercept string constants
Length of the 1> after interception
2> What happens if you intercept another string before the next intercept?
Practice:
1.192.168.88.8
2. Find the longest word in the string and output the word

=====================================
Implementation of commonly used string processing functions
1. Implementation of character processing functions
2. Implementation of string processing functions
Strlen
int Mystrlen (const char *STR);

char* mystrcpy (char *dst, const char *SRC)


Char *mystrchr (char *str, int c);

Char *mystrrchr (char *str, int c);
====================================
sprintf and SSCANF

Requirements: Users need to export each user's information in the following format
1. String formatting functions
1) sprintf use
2) Comparison of printf and sprintf
int sprintf (const char *s, const char *FMT, ...)

C-and (13) strings

Related Article

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.