String manipulation functions related to ASCII code in C language

  • 2020-04-02 03:17:05
  • OfStack

C toascii() function: converts characters to the corresponding ASCII code
The header file:


#include <ctype.h>

Definition function:


int toascii(int c);

Toascii () converts the parameter c toa 7-bit unsigned char value, the eighth bit is cleared, and the character is converted to an ASCII character.

Return value: returns the value of the successfully converted ASCII character.

Example: convert int a to ASSII code character.


#include <stdlib.h>
main(){
  int a = 217;
  char b;
  printf("before toascii() : a value =%d(%c)n", a, a);
  b = toascii(a);
  printf("after toascii(): a value =%d(%c)n", b, b);
}

Execution results:


before toascii() : a value =217()
after toascii() : a value =89(Y)

C strcoll() function: compares strings according to the environment variable LC_COLLATE
The header file:


#include <string.h>

The strcoll() function compares strings according to the environment variable LC_COLLATE, and its prototype is:


  int strcoll( const char * str1, const char * str2 );

Str1 and str2 are two strings to be compared.

Strcoll () compares s1 and s2 strings in the literal order specified by the environment variable LC_COLLATE.

By default, LC_COLLATE is "POSIX" or "C", and strcoll(), like STRCMP (), compares string sizes based on ASCII.

If the LC_COLLATE locale is set, the comparison is made according to the language sorting mode set by LC_COLLATE. For example, Chinese characters are compared according to pinyin.

Returns 0 if the strings str1 and str2 are the same. If str1 is greater than str2, it returns a value greater than 0, otherwise it returns a value less than 0.


Related articles: