A small test of a string

Source: Internet
Author: User

The string is a sequence of several characters, each ending with a ' \ s ', because of this feature, each string has an additional character overhead, so be aware of the problem of string cross-border.

Such as:

Char str[10];strcpy (str, "0123456789");//cross-border

At the same time, many functions such as strcpy, strlen, strcmp, strstr and other string functions are implemented with this feature.

To save memory, C and C + + put a constant string in the static zone, and when several pointers are assigned to the same string constants, they point to the same address. But initializing arrays with constants is different

char a1[]= "Hello word"; char a2[]= "Hello word";//a1 and A2 address are different char *p1= "Hello word"; char *p2= "Hello word";//p1 and P2 address are the same, Both P1 and P2 point to the "Hello word" space.

PS: Constants have read-only properties

char a1[]= "Hello word"; a1[5]= ', ';//correct, can change element char *p= "Hello word";p [5]= ', ';//error, *p points to a constant, cannot be changed


Practice:

Implement a function that replaces each space in a string with "%20".

Example: Enter "We are happy"

Output "We%20are%20happy"


Implementation Method 2.0

Consider moving backwards from one tail to the next, traversing the string, counting the number of spaces, because of the calculation of the new string length (each replacing a space, length +2), encountering a space to replace, time complexity O (n), using the array subscript can be used to implement the character movement

#include <iostream> #include <assert.h> #include <string.h>using namespace Std;void replaceblank (  Char *str) {assert (str);//assert that STR is not null int blank = 0;//space number char *p = str;size_t length = strlen (str); while (*p! = ')} {if (*p = = ")//traverse to find the number of spaces blank++;p + +;} size_t Newlen = blank * 2 +length;//replace space for%20 new length while (newlen>length) {if (Str[length]! = ") {Str[newlen] = Str[length] ; newlen--;length--;} else{str[newlen--] = ' 0 '; str[newlen--] = ' 2 '; str[newlen--] = '% ';//new length minus 3length--;//original length minus one (Space)}}}


Implementation Method 2.1:

The above uses an array implementation, and the pointer can do the same. Point to the end of the string with one pointer P1, and the other P2 to the end of the replaced string. Move the pointer forward P1, move to the space that P2 points to, until you hit the space, move the P1 forward one grid, insert "%20" before P2, and when P1 and P2 point to the same position, replace

void Replaceblank (char str[]) {assert (str); size_t length = strlen (str); size_t blank = 0;//Space number size_t i = 0;while (str[i]! = ' + ') {if (str[i] = = ') blank++;i++;} if (blank = = 0) return;size_t Newlen = blank * 2 + length;char*p1 = str + LENGTH;CHAR*P2 = str + newlen;while (P1!=P2) {if ( *p1! = ") {*p2 = *p1;p1--;p 2--;} else{p1--;*p2--= ' 0 '; *p2--= ' 2 '; *p2--= '% ';}}}

Test method

1. Space between the middle of the string and the last

2. No spaces

3. Empty string

Test void Test () {char str[20] = "We are happy"; cout << str << Endl; Replaceblank (str); cout << str << Endl;} int main () {test (); GetChar (); return 0;}


This article is from the "incomparable Warm yang" blog, please be sure to keep this source http://10797127.blog.51cto.com/10787127/1771076

A small test of a string

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.