1. Instance program: string.c program:
#include<stdio.h> #define MSG "YOU MUST have many talents .tell me some." #define LIM 5 #define LINELEN 81 int main() { char name[LINELEN]; char talents[LINELEN]; int i; const char m1[40]="limit yourself to one line's worth."; const char m2[]="IF you can't think of your anything,fake it."; const char*m3="\nENough about me,what's your name?"; const char *mytal[LIM]={"adding numbers swiftly","mulityplying accurately","stashing data","flowing instructions to the letter","understanding C language"};//Initialize a string pointer array printf("hi , i'm clyde the computer." "i have many talents.\n"); printf("let me tell you some talents.\n"); puts("what were they?"); for (i=0;i<LIM;i++) puts(mytal[i]); puts(m3); gets(name); printf("well, %s,%s\n",name,MSG); printf("%s \n %s\n",m1,m2); gets(talents); puts("let me see if i have got that list:"); puts(talents); printf("thanks for the information .%s.\n",name); return 0; }
Run result:
You can see that there are several ways to define strings: using string constants, char arrays, char pointers, string arrays,
2. Consider a string as a pointer:
Instance program:
#include<stdio.h> int main() { printf("%s,%p,%c\n","we","are",*"spare farers"); return 0; }
The%s format outputs the string "we",%p format produces a hexadecimal address, so if "are" is an address,%p should output the address of the first character in the string.Last
* "spare farers" should produce the value in the address you point to, that is, the first character of the string * "spare farers".
3. strlen() gets the length of the string and shortens the string function
Sample program:
#include<stdio.h> #include<string.h> void fit(char *,unsigned int); int main(void) { char mesg[]="Hold on to your heads,hackers."; puts(mesg); fit(mesg,7); puts(mesg); puts("let's look at some more of the string."); puts(mesg+8); return 0; } void fit (char *string,unsigned int size) { if(strlen(string)>size) *(string+size)='\0'; }
Run result:
The fit() function places one of the eighth elements of the array
'\0', instead of the original space character, the output of the put function stops at the first space character.Ignoring the other elements of the array, however, the other elements of the array still exist, and mesg+8 represents the address of the mesg[8],'t'character, so the puts function continues to output until it encounters an empty character in the original string.
4. strcat() stands for the string concatenation function. The function accepts two string parameters and adds a copy of the second string to the end of the first string so that the first string is called a new combination of strings and the second string remains unchanged.This function is of type char* (pointer to char), which returns the value of its first parameter, which is the address of the first character of the string after which the second string is added.
Instance program:
#include<stdio.h> #include<string.h> #define size 80 int main() { char flower[size]; char addon[]="s smell like old shoes,"; puts("what's your favorite flowes?"); gets(flower); strcat(flower,addon); puts(flower); puts(addon); return 0; }
Run result:
5. The strncat() function, the strcat function, does not check whether the first array can hold the next second string.If you do not allocate enough space to the first array, you will have problems overflowing the extra characters into adjacent storage units.The strncat() function is used.This function requires another parameter to indicate the maximum number of characters allowed to be added, such as strncat(bugs,addon,13), which adds the contents of addon to the bugs until they are added to 13 characters or encounter empty characters.
6. strcmp() function.The user's response is compared to an existing string.Represents (string comarison) strcmp(a,b), and if the parameters of the two strings are the same, the return value is 0.Compare strings, not arrays.Used to compare strings instead of characters.
Wait.
7. An example of string ordering
Let's take an example of sorting strings by alphabet.Mainly used for strcmp()
Sample program:
#include<stdio.h> #include<string.h> #define size 81 #define lim 20 #define halt''// Terminate input with empty character void start(char *string[],int num);//String Sort Function int main(){ char input[lim][size]; char *ptstr[lim]; int ct=0; int k; printf("input up to %d lines,and i will sort them.\n",lim); printf("to stop.press the enter key at a lines start\n"); while (ct<lim&& gets(input[ct])!=NULL&&input[ct][0]!='\0') { ptstr[ct]=input[ct]; ct++; } start(ptstr,ct); puts("\n here the soreted list:\n"); for(k=0;k<ct;k++) puts(ptstr[k]); return 0; } void start(char *string[],int num) { char *temp; int top,seek; for (top=0;top<num-1;top++) for(seek=top+1;seek<num;seek++) if(strcmp(string[top],string[seek])>0) { temp=string[top]; string[top]=string[seek]; string[seek]=temp; } }
Run result: