C language FAQ - string

Keywords: C

catalogue

1, Common bug: "hot, hot, hot"

1. Solutions

2. Principle

2, String definition

1. String storage

2. String constant

3. String variable

3, How to manipulate strings

1. String variable assignment

2. Input

3. Output

  4. Calculate string length

5. String transfer between functions

6. User defined operation

4, string header file common functions

1. Calculate the string length strlen

2. Copy string strcpy

3. Copy the first n characters strncpy

4. String comparison strcmp

5. Compare the first n characters strncpy

6. String splicing strcat

1, Common bug: "hot, hot, hot"

         I think when programmers first contact strings or character arrays, they may encounter "hot" garbled bug s. In fact, this is caused by some bad programming habits. For example:

#include<stdio.h>
int main()
{
	char str[20],ch;
	int i=0;
	for(i=0;(ch=getchar())!='\n'&&i<20;i++)
		str[i]=ch;
	printf("%s",str);
	return 0;
}

1. Solutions

         There are two solutions. One is to add '\ 0' at the end of the string, for example:  

         The other is initialized when declaring a character array:

2. Principle

Why are these two methods feasible?

         First of all, this is related to the output mode. You used% s to format the output, indicating that str should store a string, and the string in C language should end with '\ 0', but you didn't. The first solution is to manually set the next character of the string to '\ 0', and the output function stops when it encounters \ 0. Naturally, it will not output garbled code.

         The second solution initializes the storage space of the character array in the stack area to make it (binary bits) all 0. When you output with printf("% s", str), the output function stops when it encounters 0000 0000 (note that \ 0 is represented by 0000 0000 in binary).

Why does it output "hot"?

         "Hot" is common in Windows compilation environment. Because the vs compiler will use 0xCC to fill the stack space that is not given an initial value when allocating memory. Because 0xCC is beyond the representation range of ASCII code, the compiler will consider it a wide character and connect the two bytes together for recognition. When two 0xccs are connected together, 0xCCCC is formed, which is just for the "hot" code in GBK.

We can write a piece of code to verify:

 

Array str takes up 10 bytes, each "hot" takes up two bytes, and just 5 "hot" are output

         To sum up, we should form the habit of setting initial values for variables in the process of writing programs. When it comes to string operations, pay attention to whether the next character of the string is \ 0.

         If you want to have a more comprehensive understanding of strings, you can see the following.

2, String definition

1. String storage

         A string is a string of characters that ends with \ 0\ 0 is 0x00 in hexadecimal and 0000 in binary. There are two ways to store strings in C program, one is string constant, the other is string variable.

2. String constant

         1) String constants are stored in the constant area and are usually of type char *. The value of a constant cannot be changed during program execution, but its value can be obtained through a pointer. For example:

#include <stdio.h>
int main() {
    char * str= "Hello World!\n";//Constant string
    printf("%s",str);
	return 0;
}

         Note that although we do not add \ 0 to the end of the string, when the compiler stores the string constant in the constant area, it will automatically set the binary position after the string constant to 0, which is equivalent to adding \ 0 to the end of the string.

         2) In addition, there is another way to implement string constants - define, for example:  

#include <stdio.h>
#define STRING "Hello!\n"
int main() {
printf("%s",STRING);
return 0;
}

         Note that #define is essentially a macro replacement. The code is replaced when the program is precompiled, and its value will not be stored in the constant area.

3. String variable

         C language does not define string type, so users need to implement string type themselves. Usually, we use char type array to store string variables, and use \ 0 as the end of the string.

//Here are two methods to assign initial values to Strings:
#include <stdio.h>
int main() {
	char str1[10] = "Hello!";	//{"Hello!"}
	char str2[10] = {'H','e','l','l','o','!','\0'};
	printf("str1=%s\nstr2=%s\n",str1,str2);
	return 0;
}

         If you use "" to assign an initial value to a string, such as char str1[10] = "Hello!", the compiler will automatically add \ 0 to the end of the string.

         If you assign an initial value to a string character by character, you need to manually add '\ 0' at the end of the string, for example, char str2[10] = {'H','e','l','l','o','!',' char STR2 [10] = {'H', 'e', 'l', 'l', 'o', '!', '\ 0''}, otherwise an error will occur when outputting in string format.

3, How to manipulate strings

1. String variable assignment

1) Assignment on array declaration

         When assigning an initial value to a string, the following two assignment methods can be used:

        char str1[10] = "Hello!"; //{"Hello!"}

        char str2[10] = {'H','e','l','l','o','!','\0'};

         However, the above two assignment methods cannot be used outside the array declaration statement, otherwise the compiler will report an error (as shown in the figure below). This is mainly because both "," and {} assignment methods can only be used when the array is defined.

  2) Assignment in other cases

         You can assign values to each element of the character array one by one and end with '\ 0':

#include <stdio.h>
int main() {
	char str1[10],str2[10];
	//str1 assignment:
	str1[0]='H';str1[1]='e';str1[2]='l';str1[3]='l';str1[4]='o';str1[5]='\0';
//str2 assignment:
	for(int i=0;i<10;i++)
		str2[i]=str1[i];
	printf("str1=%s\nstr2=%s",str1,str2);
	return 0;
}

          You can also use the strcpy function to assign a string. Remember to add the string.h header file:

#include <stdio.h>
#include <string.h>
int main() {
	char str1[10]="Hello!\n",str2[10]={0};
	strcpy(str2,str1);//Assign the string str1 to str2
	printf("%s",str2);
	return 0;
}

2. Input

1) Using the scanf function

#include <stdio.h>
int main() {
	char str[10];
	scanf("%s",str);
	printf("%s\n",str);
	return 0;
}

 

Note that the scanf function stops reading the string when it encounters spaces or carriage returns, so spaces cannot be read:

2) Using the gets function

#include <stdio.h>
int main() {
	char str[10];
	gets(str);
	printf("%s\n",str);
	return 0;
}

Note that the gets function stops reading when it encounters carriage return, and does not stop when it encounters spaces, so you can read spaces:

  3) Read in character by character

#include <stdio.h>
int main() {
	char str[20];
	int i=0;
	for(i=0;(str[i]=getchar())!='\n'&&i<20;i++);
	str[i]='\0';
	printf("%s",str);
	return 0;
}

3. Output

1)printf is output in% s format, for example:

        printf("%s",str);

2) Put function, for example:

        puts(str);

  Note that the put function automatically wraps the line after outputting the string, while printf does not

3) Character by character output

#include<stdio.h>
int main()
{
	char str[20]="Hello World!\n";
	for(int i=0;i<20&&str[i]!='\0';i++)
		printf("%c",str[i]);
}

  4. Calculate string length

#include<stdio.h>
int main()
{
	char str[20]="0123456789!\n";
	int length=0;
	for(length=0;length<20&&str[length]!='\0';length++);//Calculate string str length
	printf("length=%d",length);
}
//The end character of the string \ 0 is generally not included in the string length

5. String transfer between functions

         C language strings are usually passed between functions in the form of pointers, that is, the first address of the array (character array name), for example:

#include<stdio.h>
#define N 20
int len(char s[]){//Calculates the length of the string s
	int length=0;
	for(length=0;length<N&&s[length]!='\0';length++);
	return length;
}
int main()
{
	char str[N]="Hello World!\n";
	printf("%d",len(str));
	return 0;
}

The following expressions for the declaration of function parameters are equivalent:
//int len(char s[]);    
//int len(char s[N]);
//int len(char * s);

         Note that because the character array is passed as a pointer, if the string is modified in the sub function, these modifications will be retained when returning to the main function. For example:

#include<stdio.h>
#define N 20
void reverse(char s[N]){//Flip string s
	int i=0,j=0;
	char m;
	for(j=0;j<N&&s[j]!='\0';j++);//Calculates the length of the string s
	for(j=j-1;i<j;i++,j--){
		m=s[i];
		s[i]=s[j];
		s[j]=m;
	}
	printf("reverse() s:%s\n",s);//After flipping, the value of the string s
	return;
}
int main()
{
	char str[N]="0";
	scanf("%s",str);	//Read in string str
	reverse(str);		//Flip string str
	printf("main() str:%s\n",str);//After the reverse function is executed, the value of str in the main function
	return 0;
}

Here, str of the main function and s of the sub function reverse are the same character array, which occupy the same storage space.  

6. User defined operation

         In the final analysis, the operation of string is the operation of character array. Therefore, we can realize the operation of string through the operation of each element of character array. In addition, we should pay attention to whether the character array is out of bounds and whether the next character of the string is \ 0.

4, string header file common functions

         The string header file defines some functions that manipulate strings and arrays. Here are some common:

1. Calculate the string length strlen

        size_t strlen(char *s);

         Find the length of the string from the first address of the string to the first '\ 0' stop counting, for example:

#include<stdio.h>
#include<string.h>
#define N 30
int main(){
	char str1[N]="There is an apple!";
	printf("%d\n",strlen(str1));
	return 0;
}

2. Copy string strcpy

        char * strcpy ( char * destination, char * source );

         Copy the value of the string source to destination, for example:

#include<stdio.h>
#include<string.h>
#define N 30
int main(){
	char str1[N]="There is an apple!";
	char str2[N]="0";
	strcpy(str2,str1);//Copy the value of the string str1 to str2
	printf("%s\n",str2);
	return 0;
}

 

3. Copy the first n characters strncpy

          char * strncpy ( char * destination, char * source, size_t num );

         Copy the maximum first num characters of the string source into the character array destination. When num is not greater than the length of the string source, you need to initialize destination or append \ 0 to the end of the destination string. For example:

#include<stdio.h>
#include<string.h>
int main(){
	char str1[19]="There is an apple!";//The length of array str1 is 19, and its stored string length is 18
	char str2[18]="0";
	strncpy(str2,str1,10);//Copy the first 10 characters of the string str1 to str2
	printf("%s\n",str2);
	return 0;
}

 

When num is not greater than the length of the string source, the '\ 0' of the string source will be copied to destination.

4. String comparison strcmp

        int strcmp ( char *str1, char *str2);

         Compare ASCII code, STR1 > STR2, return value > 0; If two strings are equal, return 0; STR1 < STR2, return value < 0. For example:

#include<stdio.h>
#include<string.h>
int main(){
	char str1[20]="apple";
	char str2[20]="bpple";
	if(strcmp(str1,str2)>=0)
		printf("str1>=str2\n");
	else
		printf("str1<str2\n");
	return 0;
}

 

5. Compare the first n characters strncpy

        char * strncpy ( char *str1, char *str2, size_t num);

         Compare the ASCII codes of the first num characters of str1 and str2, for example:

#include<stdio.h>
#include<string.h>
int main(){
	char str1[20]="apple b";
	char str2[20]="apple a";
	if(strncmp(str1,str2,5)==0)
		printf("str1==str2\n");
	else
		printf("str1<>str2\n");
	return 0;
}

6. String splicing strcat

        char * strcat ( char *destination, char *source);

         Append the contents of source to the end of destination, for example:

#include<stdio.h>
#include<string.h>
int main(){
	char str1[20]="an ";
	char str2[20]="apple";
	strcat(str1,str2);
	printf("%s\n",str1);
	return 0;
}

 

Posted by Permutant on Thu, 11 Nov 2021 15:41:00 -0800