In the software development process, the operation of strings is quite frequent. A number of string-processing functions are available in the standard C language library. Today we will introduce some commonly used string processing functions.
1. String input and output
1.1 printf () scanf ()
We have previously learned the input and output of characters using "%c". The input and output of the string is roughly the same, with the only difference being "%s".
Take a look at the following code:
int main ()
{
Char str[10];
printf ("Please input words:\n");
scanf ("%s", str);
printf ("%s", str);
return 0;
}
The function of this code is to enter a line of string from the keyboard, and then print it out.
Attention:
String array str is used to receive strings entered from the keyboard
STR has a length of 10, so it is not possible to enter strings longer than 10
If the input string is longer than 10, the memory will be out of bounds, which could cause a system program to crash. But not every time, so you need attention.
1.2 Puts () gets ()
Puts () print a string to the screen
Gets () reads a string from the keyboard
These two functions are defined in string.h and need to be added when used
#include <string.h>
Puts ()
Format
Puts (character array)
Function
Output a string to the display (output, wrap, ready to use)
Gets ()
Format
Gets (character array)
Function
Enter a string that ends with a carriage return from the keyboard into an array of characters and automatically add "s"
Description
The input string length should be less than the number of character array dimensions, and the string can contain spaces
For example:
#include <stdio.h>
#include <string.h>
void Main ()
{
Char str[10];
int i;
printf ("Please input a string:\n");
Gets (str);
printf ("The input string is:\n");
Puts (str);
}
It is also important to note the problem of array length.
2. String conversions
Strings are often used for conversions with other types, and the following functions are used.
2.1 itoa ()
Function
Converts an integer to a string. ITOA is an abbreviation for the English integer to array
Usage
char *itoa (int value, char* string, int radix);
Parameters
Value: the integer to convert.
Radix: Is the meaning of the cardinality, that is, the value is converted to the number of radix, the range is between 2-36, for example, 10 means 10, 16 means 16 binary.
* String: The string to be saved after the conversion.
return value
char *: Points to the generated string, with *string.
Header file
"Stdlib.h"
program Example:
int main ()
{
int number = 123;
Char string[25];
Itoa (number, string, 10);
printf ("integer =%d string =%s\n", number, string);
return 0;
}
2.2 Atoi ()
Function
Converts a string to an integer number. The abbreviation for array to integer.
Function description
Atoi () scans the parameter nptr string, if the first character is not a number and is not the sign returns zero, otherwise the type conversion is started, and then the conversion is stopped when a non-numeric or terminator is detected, and the integer number is returned.
Prototype
int atoi (const char *nptr);
Header file
"Stdlib.h"
Program examples
int main ()
{
int n;
Char *str = "12345.67";
n = atoi (str);
printf ("string =%s integer =%d\n", str, n);
return 0;
}
Execution Result:
string = 12345.67 integer = 12345
2.3 sprintf ()
Function
Write formatted data to a string
Prototype
int sprintf (char *buffer, const char *format [, argument] ...);
return value
String Length (strlen)
This is a useful function, like printf, except that it does not print the results on the screen, but rather in an array. So what can it do? Say two of the most commonly used.
int main ()
{
Char s[50];
Char who[] = "I";
Char whom[] = "Tianhuaban";
sprintf (S, "%s am%s.", who, whom);
printf ("%s\n", s);
sprintf (S, "%10.3f", 3.1415626);
printf ("%s\n", s);
return 0;
}
The first sentence sprintf the two-character array into a new array, written in S. The second sprintf formats a floating-point number and writes it to the character array S.
3. String Common methods
3.1 strcpy
Function: Copy one string to another
Usage: char *stpcpy (char* destin, char* Source);
3.2 strcat
Function: string concatenation function
Usage: char *strcat (char* destin, char* Source);
3.3 STRCHR
Function: Finds the first match of a given character in a string
Usage: char *strchr (char* str, char c);
3.4 strcmp
Function: string comparison
Usage: int strcmp (char* str1, char* str2);
Description
When S1<s2, the return value <0
When S1=s2, the return value =0
When S1>s2, the return value >0
That is: two strings are compared from left to right by character (by the ASCII value size) until different characters are present or "s" is encountered.
3.5 strcpy
Function: String copy
Usage: char *strcpy (char* str1, char* str2);
3.6 strerror
Function: Returns a pointer to the error message string
Usage: char *strerror (int errnum);
3.7 Strset
Function: Sets all characters in a string to the specified character
Usage: char *strset (char* str, char c);
3.8 Strstr
Function: Finds the first occurrence of a specified string in a string
Usage: char *strstr (char* str1, char* str2);
C Language starting from scratch (14)-String processing