面试宝典:字符串基础

内容来自:面试宝典 字符串章节

  • 字符串与数字的相互转换
  • 实现strcopy
  • 求一个字符串中连续出现次数最多的子串
  • 求一个字符串中相同且最长的子串
  • 实现strstr
  • 将一句话中的单词进行倒置,标点符号不变。比如一句话:i come from tiamjin. 倒换后称为 tianjin. from come i
  • 计算从1-n之间的中包含1个个数
  • 转变字符串格式为原来字符串里的字符+该字符出现的次数的个数
  1 /*
  2  * mystring.h
  3  *
  4  *  Created on: Mar 10, 2017
  5  *      Author: wxquare
  6  */
  7 
  8 #ifndef MYSTRING_H_
  9 #define MYSTRING_H_
 10 
 11 #include <cassert>
 12 #include <string>
 13 #include <iostream>
 14 
 15 namespace MYSTRING {
 16 
 17 void itoa(int val, char* str) {
 18 
 19     char* tmp = new char[10];
 20 
 21     int i = 0;
 22     while (val) {
 23         tmp[i++] = (val % 10) + '0';
 24         val = val / 10;
 25     }
 26     i = i - 1;
 27     int j = 0;
 28     while (i >= 0) {
 29         str[j++] = tmp[i--];
 30     }
 31     delete[] tmp;
 32 }
 33 
 34 int atoi(char* str) {
 35 
 36     int i = 0, res = 0;
 37     while (str[i] != '') {
 38         res = res * 10 + str[i++] - '0';
 39     }
 40     return res;
 41 }
 42 
 43 char* strcopy(char* strDest, const char* strSrc) {
 44 
 45     assert(strDest != nullptr && strSrc != nullptr);
 46 
 47     char* p = strDest;
 48     char* q = const_cast<char*>(strSrc);
 49     while (*q != '') {
 50         *p++ = *q++;
 51     }
 52     *p = '';  //notice
 53     return strDest;
 54 }
 55 
 56 /*
 57  * 求一个字符串中连续出现次数最多的字串
 58  * maximum number of consecutive strings in a string.
 59  */
 60 std::string maxCounSubStr(const std::string &str) {
 61     int len = str.length();
 62     std::string res = str.substr(0, 1);
 63     int maxCount = 1, count;
 64     for (int i = 0; i < len; i++) {
 65         for (int j = i + 1; j < len; j++) {
 66             int offset = j - i;
 67             if (str.substr(i, offset) == str.substr(j, offset)) {
 68                 count = 2;
 69                 for (int k = j + offset; k < len; k += offset) {
 70                     if (str.substr(i, offset) == str.substr(k, offset)) {
 71                         count++;
 72                     } else {
 73                         break;
 74                     }
 75                 }
 76                 if (count > maxCount) {
 77                     maxCount = count;
 78                     res = str.substr(i, offset);
 79                 }
 80             }
 81         }
 82     }
 83     return res;
 84 }
 85 
 86 /*
 87  * 求一个字符串中相同且最长的字串
 88  */
 89 using string = std::string;
 90 string sameAndLongestSubstr(const string& str) {
 91     string res;
 92     int len = str.length();
 93     bool isfound = false;
 94     for (int l = len; l >= 1; l--) {
 95         if (isfound)
 96             break;
 97         for (int i = 0; i < len; i++) {
 98             if (i + l < len) {
 99                 string subStr = str.substr(i, l);
100                 if (str.find(subStr) != str.rfind(subStr)) {
101                     res = subStr;
102 //                    std::cout << subStr << '	' << i << std::endl;
103                     isfound = true;
104                     break;
105                 }
106             }
107         }
108     }
109     return res;
110 }
111 
112 /*
113  * implement strstr
114  * char * strstr ( const char * str1, const char * str2 );
115  */
116 char* strstr(const char* str1, const char* str2) {
117     char *cur = const_cast<char*>(str1);
118     char *p1, *p2;
119     while (cur != '') {
120         p1 = cur;
121         p2 = const_cast<char*>(str2);
122         if (*p1 == *p2) {
123             while (*p2 != '' && *p1 != '' && *p1++ == *p2++)
124                 ;
125             if (*p2 == '')
126                 return cur;
127             else if (*p1 == '')
128                 return NULL;
129             else {
130                 cur++;
131             }
132         } else {
133             cur++;
134         }
135     }
136     return NULL;
137 }
138 
139 /*
140  * 把一句话里面的单词进行倒置
141  * eg. "i come from tianjin."
142  *        "tianjin. from come i"
143  */
144 void reverseWords(string& sentence) {
145     int len = sentence.length();
146     char* tmp = new char[len];
147 
148     //整体倒置
149     int i = len - 1, j = 0;
150     while (i >= 0) {
151         tmp[j++] = sentence[i--];
152     }
153 
154     //每个单词的逆序
155     int k = 0;
156     i = 0, j = 0;
157     while (i < len) {
158         for (int j = i; j < len; j++) {
159             if (tmp[j] == ' ' || j == len - 1) {
160                 if (j != len - 1) {
161                     int n = j - i;
162                     i = j + 1;
163                     while (n--) {
164                         sentence[k++] = tmp[--j];
165                     }
166                     sentence[k++] = ' ';
167                 } else {
168                     int n = j - i + 1;
169                     i = j + 1;
170                     while (n--) {
171                         sentence[k++] = tmp[j--];
172                     }
173                 }
174                 break;
175             }
176         }
177     }
178     delete [] tmp;
179 }
180 
181 
182 /*
183  * 统计1的个数
184  * ege: f(13) = 6  {1,2,3,4,5,6,7,8,9,10,11,12,13}
185  *
186  */
187 int countOne(int n){
188     if(n == 1) return 1;
189     int r = n;
190     int count = 0;
191     while(r){
192         if(r % 10 == 1){
193             count++;
194         }
195         r = r / 10;
196     }
197     return countOne(n-1) + count;
198 }
199 
200 
201 /*
202  * 把字符串转为字符加上数字的表述方式
203  * ege: 1233444  转变为 11213243
204  *
205  */
206 string CharNumber(const string& str){
207     string res;
208     int len = str.length();
209     int count = 1;
210     for(int i=0;i<len;i++){
211         if(i+1 < len && str[i+1] == str[i]){
212             count++;
213         }else{
214             res.push_back(str[i]);
215             res.push_back(count+'0');
216             count = 1;
217         }
218     }
219     return res;
220 }
221 
222 }
223 ;
224 
225 #endif /* MYSTRING_H_ */
原文地址:https://www.cnblogs.com/wxquare/p/6531303.html