資料結構:字串
阿新 • • 發佈:2019-02-03
二.字串
定義:若干字元組成的序列
c++/c與Java中字串的對比:c/c++中每個字串都是以‘\0‘作為結尾;Java中卻不是這樣的,Java中的一切都是物件,物件有長度,編譯器可以確定輸出的字元個數。
題目:請實現一個函式,把字串中的每一個空格替換成‘%20‘,例如,輸入為"we are happy",則輸出為:"we%20are%20happy"
思路:要弄清楚兩點:第一,在原串上面幹,字串的長度變長。第二,在新串上面幹,自己分配足夠地記憶體。從頭開始遍歷字串,在插入字元後,後面的字元會後移。因此我們採用從後面開始遍歷。
思路:同樣是定義兩個指標p1和p2,分別指向字陣列A1和A2尾端,sp1和sp2,分別指向陣列A1和A2的頭部。並且設定一個指標p3指向合併後的字串(即:A1陣列)的尾端。
定義:若干字元組成的序列
c++/c與Java中字串的對比:c/c++中每個字串都是以‘\0‘作為結尾;Java中卻不是這樣的,Java中的一切都是物件,物件有長度,編譯器可以確定輸出的字元個數。
題目:請實現一個函式,把字串中的每一個空格替換成‘%20‘,例如,輸入為"we are happy",則輸出為:"we%20are%20happy"
思路:要弄清楚兩點:第一,在原串上面幹,字串的長度變長。第二,在新串上面幹,自己分配足夠地記憶體。從頭開始遍歷字串,在插入字元後,後面的字元會後移。因此我們採用從後面開始遍歷。
先遍歷一次字串,統計字串中空格的總數,計算替換後字串的總長度。用指標p1和p2分別指向原字串的尾部和替換後字串的尾部。
#include <iostream> using namespace std; void ReplaceBlank(char string[],int length) { if(string==NULL||length<=0) return ; int originalLength=0; int numberOfBlank=0; int i=0; while(string[i]!=‘\0‘) { ++ originalLength; if(string[i]==‘ ‘) ++numberOfBlank; ++i; } int newLength=originalLength+numberOfBlank*2; if(newLength>length) { return; } int indexOfOriginal=originalLength; int indexOfNew=newLength; while(indexOfOriginal>=0&&indexOfOriginal<indexOfNew) { if(string[indexOfOriginal]==‘ ‘) { string[indexOfNew--]=‘0‘; string[indexOfNew--]=‘2‘; string[indexOfNew--]=‘%‘; } else string[indexOfNew--]=string[indexOfOriginal]; --indexOfOriginal; } } void Test(char* testName, char string[], int length, char expected[]) { if(testName != NULL) printf("%s begins: ", testName); ReplaceBlank(string, length); if(expected == NULL && string == NULL) printf("passed.\n"); else if(expected == NULL && string != NULL) printf("failed.\n"); else if(strcmp(string, expected) == 0) printf("passed.\n"); else printf("failed.\n"); } // 空格在句子中間 void Test1() { const int length = 100; char string[length] = "hello world"; Test("Test1", string, length, "hello%20world"); } // 空格在句子開頭 void Test2() { const int length = 100; char string[length] = " helloworld"; Test("Test2", string, length, "%20helloworld"); } // 空格在句子末尾 void Test3() { const int length = 100; char string[length] = "helloworld "; Test("Test3", string, length, "helloworld%20"); } // 連續有兩個空格 void Test4() { const int length = 100; char string[length] = "hello world"; Test("Test4", string, length, "hello%20%20world"); } // 傳入NULL void Test5() { Test("Test5", NULL, 0, NULL); } // 傳入內容為空的字串 void Test6() { const int length = 100; char string[length] = ""; Test("Test6", string, length, ""); } //傳入內容為一個空格的字串 void Test7() { const int length = 100; char string[length] = " "; Test("Test7", string, length, "%20"); } // 傳入的字串沒有空格 void Test8() { const int length = 100; char string[length] = "helloworld"; Test("Test8", string, length, "helloworld"); } // 傳入的字串全是空格 void Test9() { const int length = 100; char string[length] = " "; Test("Test9", string, length, "%20%20%20"); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7(); Test8(); Test9(); return 0; }
相關例題:有兩個排序的陣列A1和A2,記憶體在A1的末尾有足夠的多餘的空間來容納A2,請實現一個函式,把A2中地所有的數字插入到A1中並且所有的數字是排序的。
思路:同樣是定義兩個指標p1和p2,分別指向字陣列A1和A2尾端,sp1和sp2,分別指向陣列A1和A2的頭部。並且設定一個指標p3指向合併後的字串(即:A1陣列)的尾端。
當滿足p1>sp1和p2>sp2的同時。if(*p1>=*p2)*p3=*p1;else *p3=*p2;之後,當滿足p1>sp1,*p3=*p1;當滿足p2>sp2,*p3=*p2。
注意:C/C++把常量字串放到單獨的記憶體區域,幾個指標賦值給相同的常量字串時,指向相同的記憶體地址。當從頭到尾複製出現異動次數多時刻考慮從尾到頭複製。//合併陣列 #include <stdio.h> void mergaMatrix(int* matrix1,int* matrix2, int lenofmtrx1,int lenofmtrx2,int sizeofmatrix1) { if(sizeofmatrix1 != 0 && matrix1 != NULL && lenofmtrx1 !=0 && matrix2 != NULL && lenofmtrx2 != 0 ) { int* pNewMatrix1 = matrix1 + lenofmtrx1 + lenofmtrx2 -1; int* pMatrix1 = matrix1 + lenofmtrx1 - 1; int* pMatrix2 = matrix2 +lenofmtrx2 - 1; while(pMatrix1 >= matrix1 && pMatrix2 >= matrix2) { if(*pMatrix1 >= *pMatrix2) *pNewMatrix1-- = *pMatrix1--; else *pNewMatrix1-- = *pMatrix2--; } while(pMatrix1 >= matrix1) { *pNewMatrix1-- = *pMatrix1--; } while(pMatrix2 >= matrix2) { *pNewMatrix1-- = *pMatrix2--; } } return; } //單元測試 void test(int* matrix1,int* matrix2, int lenofmtrx1,int lenofmtrx2,int sizeofmatrix1) { if(matrix1 != NULL) { for( int i=0; i<lenofmtrx1;i++) { printf("%d ",*(matrix1+i)); } } printf("\n"); if(matrix2 != NULL){ for( int i=0; i<lenofmtrx2;i++) { printf("%d ",*(matrix2+i)); } } printf("\n"); mergaMatrix(matrix1,matrix2,lenofmtrx1,lenofmtrx2,sizeofmatrix1); for( int i=0; i<lenofmtrx1+lenofmtrx2;i++) { printf("%d ",*(matrix1+i)); } printf("\n"); } //一般情況 void test1() { const int sizeofmatrix1 = 100; int lenofmtrx1 = 3; int matrix1[sizeofmatrix1] = {1,3,5}; int lenofmtrx2 = 4; int matrix2[] = {2,4,6,8}; test(matrix1,matrix2,lenofmtrx1,lenofmtrx2,sizeofmatrix1); } //其中一個數組的書全部小於另外一個 void test2() { const int sizeofmatrix1 = 100; int lenofmtrx1 = 3; int matrix1[sizeofmatrix1] = {1,3,5}; int lenofmtrx2 = 4; int matrix2[] = {6,7,8,9}; test(matrix1,matrix2,lenofmtrx1,lenofmtrx2,sizeofmatrix1); } //其中一個為空 void test3() { const int sizeofmatrix1 = 100; int lenofmtrx1 = 3; int matrix1[sizeofmatrix1] = {1,3,5}; test(matrix1,NULL,lenofmtrx1,0,sizeofmatrix1); } //兩個都為空 void test4() { const int sizeofmatrix1 = 100; test(NULL,NULL,0,0,sizeofmatrix1); } int main() { test1(); test2(); test3(); test4(); return 0; }