C language and character related processing functions

Posted by citytours on Sun, 09 Jan 2022 06:07:16 +0100

String input function

In C language, the input of string can use scanf function or gets function

Enter a string with the scanf function

The general form of the period is: scanf ('% s', character array name). Scanf processing can input a string, an integer and a single character

When using the scanf function to input a string, spaces are treated as separators between strings, so spaces are not allowed in the string

So if the input string contains spaces, we can use the gets function

When using the gets function, spaces are allowed

Its general form is as follows: gets (character array name)

Let's write a simple code to demonstrate the usage of this function

#include <stdio.h>
int main()
{
	char ch[10];
	gets(ch);    //We can enter a string, such as hello world
	printf("%s", ch);
	return 0;
}

String output function

The most common function is printf. We use it every day when we learn C language to input the data we want to print. Right? Printf function can output string, and put function can also output string

Its general form is: puts (string reference);

Where a string reference can be a string constant or a character array name or a character pointer variable or a character pointer expression

You can also write the string to be output directly in parentheses

Note: the put function has a feature that the printf function does not have, that is, the put function can wrap lines automatically, and it also allows spaces to be included

#include<stdio.h>
int main()
{
	char a[] = "hello world";
	puts(a);
	puts("hello world");
	const char* p = "hello world";
	puts(p);
	return 0;
}

Input and output of single character

These two functions are introduced separately here

putchar () character input function and getchar character output function

The getchar function can be written like this

int ch;

ch=getchar; Integers can store character types because here characters are considered ASCII values

The getchar() function does not take any parameters, that is, it does not add anything in parentheses. It returns the next character from the input queue.

For example, the following statement reads the next letter input and assigns the value of the character to the variable ch;

ch=getchar() is equivalent to scanf ('% C', & CH)

putchar prints its parameters. For example, the following statement prints the value previously assigned to ch as a character:

putchar(ch) is equivalent to printf("%c",ch);

Here is an example:

#include<stdio.h>
int main()
{
	char ch;
	printf("Please enter a character");
	ch = getchar();  //getchar is a character input function
	putchar(c);
}

Note that carriage return will also be treated as a character

String concatenation function

Format: strcat (character array 1, character array 2) needs to refer to the header file string,h

Function: connect the string in character array 2 to the string in character array 1, and delete the string flag "\ 0" after string 1

Note: 1. Character array 1 must be large enough;

2. Before connection, connect the string in character array 2 to the string in character array 1, and delete the string flag "\ 0" after string 1

3. The return value of this function is the first address of character array 1.

#include<stdio.h>
#include<string.h>
int main()
{
	char ch1[]="C and";
	char ch2[]="C++";
	printf("%s",strcat(ch1,ch2));
}

Character array 1 cannot load all connected strings because it defines sufficient length

String copy function

Returns the start function of the target space

Format: strcpuy (character array 1, character array 2) needs to reference the header file string h

Function: copy the string in character array 2 to character array 1, and the string end flag "\ 0" is also copied. The character number 2 can also be a string constant. This is equivalent to assigning a string constant to a character array.

Note: the character array 1 must be large enough, and the copy is copied together with '\ 0';

An array cannot be assigned with an assignment statement.

#include<stdio.h>
#include<string.h>
int main()
{
	char ch1[10],ch2[]="c++ language";   //The ch1 array must define enough space 
	strcpy(ch1,ch2);
	puts(ch1);
}

String comparison function strcmp

Format: strcmp (character array 1, character array 2) needs to refer to the header file string h>

Function: compare two strings one by one from left to right (ASCII code) until different characters or '\ 0' are encountered.

String 1 = string 2, the return value is 0;

String 1 > string 2, the return value is a positive number;

String 1 < string 2, the return value is a negative integer.

Description: string comparison cannot use "= =", but strcmp function must be used

This function can also be used to compare two string constants, or compare arrays and string constants.

#include<stdio.h>
#include<string.h>
int main()
{
	int k;
	char ch1[15],ch2[]="hello";
	gets(ch1);
	k=strcmp(ch1,ch2);
	if(k==0) printf("ch1=ch2");
	if(k>0)  printf("ch1>ch2");
	if(k<0)  printf("ch1<ch2");
}

Measure string length function strlen

Format: strlen (character array) needs to refer to the header file string h

Function: measure the actual length of the string (excluding the string end flag '\ 0') and return it as a function value. Spaces will also be calculated into the length

#include<stdio.h>
#include<string.h>
int main()
{
	char ch[]="hello world";
	printf("The length of this string is:%d\n",strlen(ch));
}

There are also some other string processing functions that are not commonly used. You can learn about these strings, including the above mentioned header file string H

Function format: strupr (character array)

Function: convert all characters of the string contained in the character array into uppercase letters

#include<stdio.h>
#include<string.h>
int main()
{
	char ch[]="hello,world";

	printf("%s",strupr(ch));
}

Function strlwr# format: strlwr (character array)

Function: convert all characters of the string contained in the character array into lowercase letters

#include<stdio.h>
#include<string.h>
int main()
{
	char ch[]="HELLO WORLD";
	printf("%s",strlwr(ch));
}

Function: strset format: strset (character array, character)

Function: convert all characters of the string contained in the character array to the specified characters

#include<stdio.h>
#include<string.h>
int main()
{
    char str[] = "http://see.xidian.edu.cn/cpp/u/xitong/";
    char c = 'a';
    strset(str, c);
   printf("%s", str);
   return 0;
}

Topics: C