Commonly used character char judgment and string processing functions in C++: isalnum, reverse, compare, +=

1. Commonly used character char judgment function:
Insert picture description here
1.1. The isalnum function is equivalent to:

if(s[i]<'0' || (s[i]>'9'&& s[i]<'a') || s[i]>'z'){
    
    
    return false;
}else{
    
    
	return true;

2. Commonly used string processing functions:
2.1, compare comparison function: the
same returns 0, different returns <0 or >0.

std::string str1 ("green apple");
std::string str2 ("red apple");
if (str1.compare(str2) != 0)
  std::cout << str1 << " is not " << str2 << '\n'; //green apple is not red apple
if (str1.compare(6,5,"apple") == 0) // green apple is an apple
  std::cout << str1 << " is an apple\n";

2.2. Reverse string function: reverse

string s = "abcde";
std::reverse(s.begin(),s.end());    // edcba

2.3. String splicing: +=

std::string name ("John");
std::string family ("Smith");
name += " K. ";         // c-string
name += family;         // string
name += 'A';           // character

No need to call the insert function, insert a single character at the end:

name.insert(name.end(),'A');

to sum up:

1. No need to call the insertfunction to insert a single character at the end, just use it directly +=.
2. The isalnumfunction determines whether the character is a character or a number.

Guess you like

Origin blog.csdn.net/qq_33726635/article/details/106863491