1.
/*
Character
Char, stored in memory in ASCII;
operation function #include <ctype.h>
*/
int main (int argc, const char * argv[]) { printf ("Isalnum (' a ') =%d\n", Isalnum (' a '));//isalnum (' a ') =1 judgment is a character printf ("Isalpha (' a ') =%d\n", Isalpha (' a ')),//isalpha (' a ') =1 judgment is the English alphabet printf ("IsDigit (' a ') =%d\n", IsDigit (' a ')) //isdigit (' a ') =0 judgment is the numeric character printf ("Islower (' a ') =%d\n", Islower (' a '));//judgment is lowercase character printf ("Isupper (' a ') =%d\n" , Isupper (' a '));//judgment is the uppercase character printf ("Isxdigit (7) =%d\n", Isxdigit (7));//judgment is 16 binary number printf ("ToLower (' a ') =%c\n" , ToLower (' a '));//turn into lowercase printf ("ToUpper (' a ') =%c\n", ToUpper (' a '));//turn into uppercase letters printf ("Digittoint (' a ') =%d\n ", Digittoint (' a '));//16 into 10-input printf (" Ishexnumber (' a ') =%d\n ", Ishexnumber (' a '));//equivalent isxdigit judgment is 16 binary printf ("Isnumber (' a ') =%d\n", Isnumber (' a '));//equivalent isdigit judgment is 10 binary numeric character return 0;} <span style= "color: #e32400;" ></span>
2.
/*
manipulation functions for strings
1: basic input and output
printf ("%s", str); start with the STR address and print to the end of '
scanf ("%s", str); Starting with the address of str , the padding character ;// If the destination string is not large enough, it may go out of bounds ;
*/
int main () { char str[100] = {' A ', ' a ', ' a ', ' a ', ' a '}; scanf ("%s", str);//he will automatically add ' + ' after the input is finished; printf ("%s\n", str); for (int i=0;i<5;i++) { printf ("%c,ascii is%d\n", Str[i],str[i]); } return 0;}
3.
/*
Get ( receive address )// and scanf the same is to ' \ n ' cutoff , you can receive space ; but scanf can not receive space ;
Puts ( string header address )
Char *fgets (char * buff, int bufsize, FILE *); most read bufsize-1 characters
*/
int main () { char str[100] = {' A ', ' a ', ' a ', ' a ', ' a '};// gets (str);//Only one address can be run out of bounds; the runtime system will give an alarm; warning:this program uses gets (), which is unsafe. Fgets (str, 5, stdin);//the security function, which means that only 5-1 characters are read from the buffer into STR; Puts (str); return 0;}
int main () { char str[100]={};//1:buffer reasonably large; 2: First clear 0 is a good habit; scanf ("%s", str); printf ("%s\n", str); return 0; }
4.
/*
sprintf ( destination string , formatted string , parameter list ): Prints the formatted string to the destination string ;
sizeof () storage , the number of bytes occupied ; is an operator ;
strlen () function , strong correlation with content ; #include <string.h>
*/
int main () { char str[100]={' A ', ' a ', ' a ', ' a '};// printf ("Hello iOS%d\n", 1518); sprintf (str, "Hello iOS%d\n", 1518);//convert arbitrary to string; printf ("%s", str);}
5.
/*
Common handling functions for strings :
Find the character , return NULL if not found , find the return address ;
*/
#include <stdlib.h> #include <string.h>char *mystrchr (const char *s, char ch) { char *p= (char *) s; while (*p) { if (*p = = ch) { return p; } p++; } Return null;//not found, returns null pointer;} int main () { char *p = "HelloWorld"; char *q; Q = MYSTRCHR (P, ' l ');//character lookup, find the position of the character from inside the string, different from MEMCHR (), because MEMCHR is looking for 0 1; The result is Lloworld printf ("%s\n", q); Q = STRRCHR (P, ' l ');//Reverse lookup character result for LD printf ("%s\n", q); return 0;}
6.
/*
string comparison
int strcmp (const char *, const char *);
*/
int main () { char *p1= "hello1"; Char *p2= "Hello0"; int rst; rst = strcmp (P1, p2); printf ("%d\n", rst); rst = strncmp (P1, p2,3);//limit the comparison range; printf ("%d\n", rst); return 0;}
7.
/*
string copy ;
*/
int main () { char str[100]={}; char *src = "Hello";// strcpy (STR,SRC);//The destination buffer is large enough, otherwise it will cross;// printf ("%s\n", str); strncpy (str,src,sizeof (str)-1);//n=sizeof (DST)-1; In order to protect the destination buffer, it is necessary to reserve the Terminator; the copy is true length; mainly to limit the time to prevent cross-border printf ("%s\n", str); return 0;}
8.
/*
Find a string ;
Char *strstr (const char *, const char *);
*/
int main () { char *str = "Helloqianfeng Isniubi"; Char *search = "Qianfeng"; char *p; p = strstr (str,search);//is case-sensitive; printf ("%s\n", p); return 0;}
9.
/*
string concatenation function ;
*/
int main () { char str[100]= "Hello"; Char *p = "ios1518";// strcat (str, p);//append P string to str string to ensure that Str has sufficient space;// printf ("%s\n", str); Strncat (str, p, sizeof (str)-strlen (str)-1);//concatenation of the number of =sizeof (str)-strlen (str)-1, need to reserve a terminator; printf ("%s\n", str); return 0;}
10.
/*
string segmentation function ;
Char *strtok (char *, const char *);
*/
int main () { char str[100]= "hello%>< (&* (World.qian>feng"; Char *demi = "%>< (&*."; Char *p=str;// p = strtok (str, demi);// printf ("%s\n", p);// P = strtok (NULL, Demi);// printf ("%s\n", p );// P = strtok (null, demi);// printf ("%s\n", p);// P = strtok (NULL, Demi);// printf ("%s\n", p);// p = strtok (NULL, Demi);// printf ("%s\n", p);//split failed, return NULL, return end;/For (int i=0;i<40;i++)// { printf ("Str[%d]=%c---ascii=%d", i,str[i],str[i]);// printf ("\ n");/ } while (P=strtok (P, Demi)) { printf ("%s", p); p = NULL; } return 0;}
11.
/*
extracting integers
*/
int main () { char *p= " -1234.89"; int i; I=atoi (p); printf ("%d\n", i);//-1234 char *p2= "1234.56"; Double F; F=atoi (p2); printf ("%lf\n", f);//1234.000000 return 0; }
12.
// Flip String
Char *reverse (char *s) { int len = (int) strlen (s); for (int i=0; I<strlen (s)/2; i++) { char sh = s[i]; S[i] = s[len-1-i]; S[len-1-i] = sh; } return s;} int main () { char str[100]= "1234"; Reverse (str); printf ("%s", str);//4321 return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C language some common string operations