The difference between C and C++ (6) String

Table of contents

1. Characters

2. C language string

1. Representation of string

2. Input and output

3. Commonly used functions

Three, string class

1. Definition, initialization, input and output


1. Characters

Type: char

Input: getchar

Output:putchar

Example:

#include <stdio.h>

void main()
{
	char ch;
	while (ch = getchar())
	{
		if (ch == EOF)break;
		printf("%d", ch);
		putchar(ch);
	}
	return;
}

Running results: (8 lines)

a^Zb
97a
26
a
97a
10

^Z

explain:

Enter a^Zb and press Enter, enter the two characters a and ^Z, then enter a and press Enter, enter the two characters a and the newline character, and then enter ^Z. The program ends.

Note 1:

The ASCII code of '\0' is 0, the ASCII code of '^A' - '^Z' is 1-26, of which the newline character '\n' is 10, and the '^Z' is 26, but when the input buffer When '^Z' appears after '\n' when the area is empty, the ASCII code of '^Z' is -1, indicating the end of file.

Note 2:

EOF is a constant -1, and OJ input and output are often controlled by EOF.

Note 3:

No. 10 '\n' and No. 26 '^Z' are both truncation of getchar. Enter a line of string, getchar can only read the first '\n' or '^Z', and discard any remaining ones.

2. C language string

1. Representation of string

In C language, strings are represented by char arrays, and '\0' is added to the end of the string to indicate the end.

2. Input and output

Input: gets

Output:puts

Example:

#include <stdio.h>
#include<string.h>

void main()
{
	char str[]="abcde";
	puts(str);
	gets(str);
	printf("%d\n", strlen(str));
	puts(str);
	return;
}

Running result 1: (4 lines)

abcde
^Za
5
abcde

Running result 2: (5 lines)
 

abcde
a^Zb

2
a?

It can be seen that the newline character and ^Z are both truncation of the gets function. The difference is that

The newline character will be read and discarded by gets, and gets handles ^Z in the same way as getchar. When the buffer is empty, it is the -1 character, and when the buffer is not empty, it is the 26th character.

3. Commonly used functions

(1) strcat function-string connection

string catenate string concatenation

Example:

#include <stdio.h>
#include<string.h>

void main()
{
	char str1[10] = "ab";
	char str2[] = "cd";
	printf("%d %d\n", strlen(str1), strlen(str2));
	strcat(str1, str2);
	printf("%d %d\n", strlen(str1), strlen(str2));
	printf("%s %s", str1, str2);
	return;
}

operation result:

2 2
4 2
abcd cd

The strcat(str1, str2) function copies str2 to the back of str1.

(2) strcpy and strncpy functions - string copy

strcpy(str1,str2) copies str2 to the location of str1

strncpy(str1,str2,n) copies the first n characters of str2 to the position of str1

(3) strcmp function - string comparison

strcmp(str1,str2) is compared in lexicographic order, and the return values ​​>0, =0, and <0 respectively represent str1>str2, str1==str2, str1<str2

(4) strlen and sizeof - string length

sizeof is a keyword and calculates the length of the array

strlen is a function that calculates the number of characters in a string, excluding '\0'

(5) strlwr and strupr functions - string case

strlwr converts uppercase letters into lowercase letters

strupr converts lowercase letters into uppercase letters

Summary: C-style string input - cin or getline or get, cin ends with a space or newline, getline and get both read one line.

The getline function will read and discard the newline characters, while the get function will keep the newline characters in the input buffer if it takes parameters. The get function can read and discard the newline characters if it takes no parameters.

Three, string class

 

1. Definition, initialization, input and output

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str="abc";
    cin>>str;
    cout<<str;
    return 0;
}

2. String assignment, length calculation, and flipping

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str1="abc";
    string str2=str1;
    reverse(str2.begin(),str2.end());
    cout<<str1<<" "<<str1.length()<<" "<<str2;
    return 0;
}

3. Get characters from string

It can be in array form or iterator form.

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str="abcde";
    cout<<str[2]; 
    string::iterator it=str.begin();
    cout<<*it;
    it=str.end()-1;
    cout<<*it;
    return 0;
}

Output:

cae

4. Comparison and connection of strings

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str1="abc",str2="def",str3="def";
    if(str1<str2)cout<<"str1<str2\n";
    if(str2==str3)cout<<"str2=str3\n";
    cout<<str1+str2;
    return 0;
}

Output:

str1<str2
str2=str3
abcdef

5. Convert string to character array

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str="abc";
    const char *p1=str.c_str();
    char *p2=new char[20];
    strcpy(p2,p1);
    cout<<p2;
    return 0;
}

6. Find characters in string

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str="abca";
    cout<<str.find('a')<<" "<<str.find('a',2);
    return 0;
}

Output:

0 3

Attached are the common string functions in the C++ textbook:

Guess you like

Origin blog.csdn.net/nameofcsdn/article/details/132225117