How to implement several common string functions
How to implement several common string functions, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Implement several common string functions and practice writing code. Always thank the code so that you don't forget how to write it.
Character comparison function
String assignment function
Find the length of the string
The inversion of the string
String comparison
String concatenation
/ / string.cpp: Defines the entry point for the console application.//#include "stdafx.h" # include # include "string.h" / / character exchange function void charswap (char& ch2, char& ch3) {char ch = ch2; ch2 = ch3; ch3 = ch;} / / find the string length int stringlength (const char* sourstr) {const char* pstr = sourstr; int length = 0 While (* pstr++! ='\ 0') {length++;} return length;} / / string that inverted char* stringconvert (char* sourstr) {int length = stringlength (sourstr); int loopnumber = length / 2; int index = 0 While (index < loopnumber) {charswap (* (sourstr + index), * (sourstr + length-index-1)); index++;} return sourstr;} / / string copy char* stringcopy (char* deststr, const char* sourstr) {const char* pstr = sourstr; int index = 0 While (* pstr! ='\ 0') {* (deststr + index) = * pstr++; index++;} * (deststr + index) ='\ 0strings concatenate char* stringcontact (char* deststr, const char* sourstr) {const char* pstr = sourstr; int length = stringlength (deststr); int index = 0 While (* pstr! ='\ 0') {* (deststr + length + index) = * pstr++; index++;} * (deststr + length + index) ='\ 0strings; return deststr;} / / string comparison function int stringcompare (const char* deststr, const char* sourstr) {const char* pdest = deststr; const char* psour = sourstr While (* pdest = = * psour & & * pdest! ='\ 0') {pdest++; psour++;} return * pdest-* psour;} int main (int argc, char* argv []) {char buff1 [100]; char buff2 [100]; stringcopy (buff1, "reqre12345"); stringcopy (buff2, "reqre1") Printf ("% d\ n", stringcompare (buff1, buff2)); getchar (); return 0;} after reading the above, do you know how to implement several common string functions? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!