String Common functions
C language provides rich string processing functions, which can be divided into string input, output, merge, modify, compare, transform, copy, search several categories. Using these functions can greatly reduce the burden of programming. A string function for input and output that should include the header file "Stdio.h" before use, and the use of other string functions to include the header file "String.h". Here are some of the most common string functions.
1. String output function puts format: puts (character array name) function: The string in the character array output to the display. That is, the string is displayed on the screen
#include "stdio.h"
Main ()
{
static char c[]= "Basic\ndbase";
Puts (c);
}
You can see from the program that the escape character can be used in the puts function, so the output becomes two rows. The puts function can be completely superseded by the printf function. The printf function is usually used when you want to output in a certain format.
2. String input function gets format: Gets (character array name) function: Enter a string from the keyboard of the standard input device. This function gets a function value, which is the first address of the character array.
#include "stdio.h"
Main ()
{
Char st[15];
printf ("Input string:\n");
Gets (ST);
Puts (ST);
}
You can see that when you enter a string that contains spaces, the output is still all strings. Indicates that the gets function does not end with a space as a string input, and only ends with a carriage return as input. This is different from the scanf function.
3. String concatenation function strcat format: strcat (character array name 1, character array name 2) function: Concatenate the string in the character array 2 to the back of the string in character array 1, and delete the string flag "". The return value of this function is the first address of the character array 1.
#include "string.h"
Main ()
{
static char st1[30]= "my name is";
int st2[10];
printf ("Input your name:\n");
Gets (ST2);
strcat (ST1,ST2);
Puts (ST1);
}
This procedure connects the initialized character array with the dynamically assigned string. Note that the character array 1 should define enough lengths to not load the concatenated strings all
4. String copy function strcpy format: strcpy (character array name 1, character array name 2) function: Copy the string in character array 2 to the character array 1. The string end sign "" is also copied together. The number of characters is 2, or it can be a string constant. This is equivalent to assigning a string to an array of characters.
#include "string.h"
Main ()
{
static char st1[15],st2[]= "C Language";
strcpy (ST1,ST2);
Puts (ST1);p rintf ("\ n");
}
This function requires that the character array 1 should be of sufficient length, otherwise the copied string cannot be loaded.
5. String comparison function strcmp format: strcmp (character array name 1, character array name 2) feature: Compares strings in two arrays in ASCII order and returns comparison results by function return value.
String 1 = String 2, return value = 0;
String 2〉 string 2, return value 〉0;
String 1〈 string 2, which returns the value 〈0.
This function can also be used to compare two string constants, or to compare array and string constants.
#include "string.h"
Main ()
{int k;
static char st1[15],st2[]= "C Language";
printf ("Input a string:\n");
Gets (ST1);
K=STRCMP (ST1,ST2);
if (k==0) printf ("st1=st2\n");
if (k>0) printf ("st1>st2\n");
if (k<0) printf ("st1<st2\n");
}
In this program, we compare the strings in the input string and array st2, and return the results to K, and then output the result string according to K value. When the input is DBASE, the ASCII code is known as "DBASE" is greater than "C Language" so k〉0, the output result "st1>st2".
6. Measuring string length function strlen format: strlen (character array name) function: measure the actual length of the string (excluding the string end Flag ' ") and return the value as a function.
#include "string.h"
Main ()
{int k;
static char st[]= "C language";
K=strlen (ST);
printf ("The lenth of the string is%d\n", K);
}