C + + string manipulation collection

Source: Internet
Author: User

#include <iostream>using namespace STD;//Implement a function to find the length of a string. intMy_length (Const Char*s) {if(*s = =' + ')return 0;Else        return 1+my_length (S +1);}intMain () {Char*s ="123456";cout<< My_length (s) << Endl;return 0;}#include <iostream>#include <assert.h>using namespace STD;//implements the length of a string to be evaluated. intMy_length (Const Char*s) {if(s = = NULL)return 0;intCount =0; while(S[++count]! =' + ');returnCount;}intMain () {Char*s ="";cout<< My_length (s) << Endl;return 0;}#include <iostream>#include <string.h>#include <assert.h>using namespace STD;Char* MY_STRCPY (Char*dist,Const Char*scour) {assert (scour!=null);Char*p = dist; while(*dist++ = *scour++);returnP;}intMain () {Char*S1 ="123";Char*S2 =New Char[strlen(S1)];cout<< my_strcpy (S2, S1) << Endl;return 0;}#include <iostream>#include <assert.h>using namespace STD;//Implement library function strcat. Char* MY_STRCAT (Char*dist,Char Const*scour) {assert (Dist!=null | | scour!=null);if(*scour = =' + ')returnDistChar*p = dist; while(*dist! =' + ') dist++; while(*dist++ = *scour++);returnP;}intMain () {Char*S1 =New Char[Ten];strcpy(S1,"123");cout<< My_strcat (S1,"456") <<endl;return 0;}#include <iostream>#include <assert.h>#include <string.h>using namespace STD;//write a program to reverse the string. Char* My_invert (Char*s) {assert (s! = NULL);Char*head = s;Char*last = s+strlen(s)-1;CharTemp while(Head < last)        {temp = *head;        *head = *last;        *last = temp;        head++;    last--; }returns;}intMain () {Char*s =New Char[Ten];strcpy(S,"12345");cout<< My_invert (s) << Endl;return 0;}#include <iostream>#include <assert.h>using namespace STD;//simulation implements the STRCHR function, function: Finds the first occurrence of a character in a string and returns null if it does not appear. intMy_strchar (Const Char* Dist,CharCH) {assert (dist!=null);intCount =0; while(*dist! =' + '&& *dist!=ch) {count++;    dist++; }if(*dist = =' + ')return 0;returncount+1;}intMain () {cout<< My_strchar ("1234",' 1 ') << Endl;return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + string manipulation collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.