C language character and string function collection

  • 2020-06-15 09:56:14
  • OfStack

Character processing function

int tolower(char ch) If ch is a capital letter ('A'-'Z') return the corresponding lowercase letter ('a'-'z')

int toupper(char ch) If ch is a lowercase letter ('a'-'z') return the corresponding uppercase letter ('A'-'Z')

int _tolower(char ch) Returns the corresponding lowercase letter of ch ('a'-'z')

int _toupper(char ch) Returns ch corresponding uppercase letter ('A'-'Z')

int toascii(char c) Returns c corresponding ASCII

Take a chestnut:


#include<stdio.h>
void main(){
 char ch1='j';
 printf("%c\n",tolower('H'));// Output: h
 printf("%c\n",_toupper('h'));// Output: H
 printf("%d\n",toascii('a'));// Output: 97
}

Character judgment function

int isalpha(char ch)  Returns a non-0 value if ch is a letter ('A'-'Z','a'-'z'),(returns 1024) or 0 if not

int isalnum(char ch)  Returns a non-0 value if ch is a letter ('A'-'Z','a'-'z') or a number ('0'-'9'), otherwise returns 0

int isascii(char ch)  Returns a non-0 value if ch is a character (0-127 in ASCII code), otherwise returns 0

int iscntrl(char ch)  Returns a non-0 value if ch is invalid character (0x7F) or normal control character (0x00-0ES69en1ES70en), otherwise returns 0

int isdigit(char ch) Returns a non-0 value if ch is a number ('0'-'9'), otherwise returns 0

int toupper(char ch)0 Returns a non-0 value if ch is a printable character (no Spaces)(0x21-0x7ES81en), otherwise returns 0

int islower(char ch)  Returns a non-0 value if ch is lowercase ('a'-'z'), otherwise returns 0

int isupper(char ch) Returns a non-0 value if ch is uppercase ('A'-'Z'), otherwise returns 0

int isprint(char ch) Returns a non-0 value if ch is a printable character (including Spaces)(0x20-0ES99en7ES100en), otherwise returns 0

int ispunct(char ch) Returns a non-0 value if ch is the punctuation character (0x00-0ES106en1ES107en), otherwise returns 0

int isspace(char ch)  If ch is a space (' '), horizontal TAB (' \ t '), a carriage return (' \ r), paper feed line feed (' \ f), vertical TAB (' \ v '), a newline character (' \ n ') returns non-zero value, otherwise it returns 0

int isxdigit(char ch) Returns a non-0 value if ch is hexadecimal ('0'-'9','A'-'F','a'-'f'), otherwise returns 0

Take a chestnut:


#include<stdio.h>
void main(){ char ch1='j';
 printf("%d\n",isalpha(ch1));// Output: 1024
 printf("%d\n",isalnum(ch1));// Output: 8
 printf("%d\n",isdigit(ch1));// Output: 0:
}

Type conversion

Str- > double

Header file: ES138en.h

Function prototype: double strtod(const char *nptr,char **endptr);

Note: nptr is the original string, endptr is converted to discard the following content, NULL does not return, the original string number can only be before the control or plus or minus sign.

Return value: plus or minus double value

Take a chestnut:


#include<stdio.h>
#include<stdlib.h>
void main(){
 char *ch1=" -100.65987ffjj";
 char *endss;
 printf("%lf\n",strtod(ch1,NULL));// Output: -100.659870
 printf("%lf\n",strtod(ch1,&endss));// Output: -100.659870
 printf("%s\n",endss);// Output: ffjj
}

Str- > long int

The header file: stdlib h

Function prototype: long int strtol(const char *str, char **endptr, int base)

Return value: long integer, extracted as base, then converted to long int

Parameters:

str -- a string to be converted to a long integer.

endptr -- A reference to an object of type char*, whose value is set by the function to the next character after the value in str.

base - Cardinality, must be between 2 and 36 (inclusive) or a special value of 0 (e.g. 0x is automatically set to base 106, etc.).

Take a chestnut:


#include<stdio.h>
#include<stdlib.h>
void main(){
 char *ch1="0101jjx";
 char *endss;
 printf("%ld\n",strtol(ch1,NULL,2));// Output: 5
 printf("%ld\n",strtol(ch1,&endss,10));// Output: 101
 printf("%s\n",endss);// Output: jjx
}

Str- > int

Header file: stdlib.h

Prototype: int atoi(const char *nptr);

Note: the original string must begin with a space or a number or a plus or minus sign

Take a chestnut:


#include<stdio.h>
#include<stdlib.h>
void main(){
 char *ch1=" 11.963xxx";
 printf("%d\n",atoi(ch1));// Output: 11
}

str- > double

The atof() string is converted to the double character number in a similar way to stoi

str- > long int

atol() string converted to long integer, used similar to stoi

String handling function

Length calculation:

strlen () function:

Header file: ES235en.h

Prototype: int strlen(const char *str)

Return value: Returns the length of the string before '\0' or 0

Take a chestnut:


#include<stdio.h>
#include<string.h>
void main(){
 // char ch[]={'a','b',0,'c'};// 0 Or ' \0'
 char ch[]={'a','b','\0','c'};
 printf("strlen To: %d\n",strlen(ch)); // The output 2
}

Operator sizeof ()

The operator (operator) in C/C++ returns the number of bytes of memory occupied by an object or type

Take a chestnut:


#include<stdio.h>
void main(){
char ch[]={'b',0,'c'};
 int inx=10;
 printf("ch===sizeof:%d\n",sizeof(ch));// Output: 3
 printf("int===sizeof:%d\n",sizeof(inx));// Output: 4
}

Copy (replace) function:

strcpy () function

Header file: ES263en.h

Prototype: char *strcpy(char *dest, const char *src);

Return value: Replace str with the string '\0' or 0 before the end of dest with the return value of dest first address or you can access dest directly to get the final result

Take a chestnut:


#include<string.h>
void main(){
 char ch1[100]="123456789";
 char *ch2="abc";
 printf("%s\n",strcpy(ch1,ch2));// The output abc
 printf("%s\n",ch1);// Output: abc
 printf("%s\n",ch2);// Output: abc
}

strncpy () function

Header file: ES282en.h

Prototype: char *strncpy(char *dest, const char *src, int n)

Return value: Replace dest with the string '\0' or 0 or n before the end with the return value of dest first address or you can access dest directly to get the final result

Note: this n value is important, if the '\0' at the end of src is copied, it will be replaced, if the copy is less than or equal to strlen(), the unused contents of dest will be retained.

Take a chestnut:


#include<stdio.h>
#include<string.h>
void main(){
 char ch1[100]="123456789";
 char *ch2="abc";
 printf("%s\n",strncpy(ch1,ch2,strlen(ch2)));// Output: abc456789
 printf("%s\n",ch1);// Output: abc456789
 printf("%s\n",ch2);// Output: abc
}

The comparison function

strcmp() and strncmp() functions

Header file: ES312en.h

Prototype:

int strcmp(const char *s1, const char *s2);

int strncmp(const char *s1, const char *s2,int n);

Return value: If the parameters s1 and s2 are the same, return 0; s1 greater than s2, return greater than 0; s1 less than s2, return less than 0.

Take a chestnut:


#include<stdio.h>
#include<string.h>
void main(){
 char *ch1="BCD";
 char *ch2="BCd";
 printf("%d\n",strcmp(ch1,ch2)); // Output: -32
 printf("%d\n",strncmp(ch1,ch2,2));// Output: 0
}

With strncasecm strcasecm () ()

Ignoring letter case for comparison, other functions like strcmp() are similar

Take a chestnut


#include<stdio.h>
void main(){ char ch1='j';
 printf("%d\n",isalpha(ch1));// Output: 1024
 printf("%d\n",isalnum(ch1));// Output: 8
 printf("%d\n",isdigit(ch1));// Output: 0:
}
0

Additional function

strcat() and strncat() functions

Header file: ES351en.h

Prototype:

char *strcat(char *dest, const char *src)

char *strcat(char *dest, const char *src,int n)

Return value: Append src to dest, return the first address of dest or access dest directly to get the final result

Take a chestnut:


#include<stdio.h>
void main(){ char ch1='j';
 printf("%d\n",isalpha(ch1));// Output: 1024
 printf("%d\n",isalnum(ch1));// Output: 8
 printf("%d\n",isdigit(ch1));// Output: 0:
}
1

To find the character

strchr() and strrchr() functions

Header file: ES378en.h

Prototype:

char *strchr(const char *s,char c) // From left to right

char *strrchr(const char *s,char c) // From right to left

Return value: Returns the location itself found, or NULL if the search fails

Take a chestnut:


#include<stdio.h>
void main(){ char ch1='j';
 printf("%d\n",isalpha(ch1));// Output: 1024
 printf("%d\n",isalnum(ch1));// Output: 8
 printf("%d\n",isdigit(ch1));// Output: 0:
}
2

Find string

strstr () function

Header file: ES401en.h

The prototype

char *strstr(char *str1, const char *str2); // From left to right

Return value: Returns the first address in the found string

Note: the strrstr() function is not self-contained and can be simulated with strstr()

Take a chestnut:


#include<stdio.h>
#include<string.h>
void main(){
 char *ch1="1234562321";
 printf("%s\n",strstr(ch1,"23"));//234562321
 if(!strstr(ch1,"5566")){
  printf("-------------\n");// Successfully output here 
 }
}

conclusion


Related articles: