• C/C++ 字符串问题总结


    1.怎样将整数转化为字符串,并且不用函数itoa.

    解析:整数转化为字符串,可以采用加‘0’,再逆序的办法 整数加‘0’就会隐性转化成char类型的数。

    1. 1 #include <stdio.h>
    2. 2
    3. 3 int main()
    4. 4 {
    5. 5 int num = 12345,i = 0,j = 0;
    6. 6 char a[7],b[7];
    7. 7 while(num)
    8. 8 {
    9. 9 a[i] = num%10 + '0';
    10. 10 num = num/10;
    11. 11 i++;
    12. 12 }
    13. 13 a[i] = 0;
    14. 14 i = i-1;
    15. 15 printf("%s\n",a);
    16. 16 while(i>=0)
    17. 17 {
    18. 18 b[j] = a[i];
    19. 19 j++;
    20. 20 i--;
    21. 21 }
    22. 22 b[j] = 0;
    23. 23 printf("%s\n",b);
    24. 24
    25. 25 return 0;
    26. 26 }

    2.字符串转化成整数

    可以采用减‘0’再乘10累加的办法,字符串减‘0’就会隐性转化成int类型的数。

    1. 1 #include <stdio.h>
    2. 2
    3. 3 int main(void) {
    4. 4
    5. 5 char a[] = "12345";
    6. 6 int i = 0,num = 0;
    7. 7 while(a[i])
    8. 8 {
    9. 9 num = num*10 + (a[i]-'0');
    10. 10 i++;
    11. 11 }
    12. 12 printf("%d\n",num);
    13. 13
    14. 14 return 0;
    15. 15 }

    3.将小写字母换算成大写字母

    1. 1 #include <stdio.h>
    2. 2
    3. 3 int main(void) {
    4. 4
    5. 5 char a[] = "asdfg",b[7];
    6. 6 int len,i = 0;
    7. 7 len = 'a' - 'A';
    8. 8 printf("%c%c%c%c%c\n",a[0]-len,a[1]-len,a[2]-len,a[3]-len,a[4]-len);
    9. 9
    10. 10 return 0;
    11. 11 }

    4.将字符串逆序

    1. 1 #include <stdio.h>
    2. 2 #include <string.h>
    3. 3
    4. 4 int main(void) {
    5. 5
    6. 6 char a[] = "asdfgh",b[10];
    7. 7 int i = 0,j = 0;
    8. 8 j = strlen(a)-1;
    9. 9 while(a[i])
    10. 10 {
    11. 11 b[i] = a[j];
    12. 12 i++;
    13. 13 j--;
    14. 14 }
    15. 15 b[i] = 0;
    16. 16 printf("%s\n",b);
    17. 17
    18. 18 return 0;
    19. 19 }

    字符串逆序C++实现:https://blog.csdn.net/qq_44711190/article/details/118956333

    1. 1 #include <iostream>
    2. 2 #include <string>
    3. 3 using namespace std;
    4. 4
    5. 5 int main() {
    6. 6
    7. 7 string a = "asdfg";
    8. 8 string b(a.rbegin(),a.rend());
    9. 9 cout << b << endl;
    10. 10
    11. 11 return 0;
    12. 12 }

    5.计算一个字节里(byte)里面有多少bit被置1

    1. 1 #include <stdio.h>
    2. 2
    3. 3 int main()
    4. 4 {
    5. 5 int c = 0xcf,cout = 0,i = 0;
    6. 6 while(i<8)
    7. 7 {
    8. 8 if(c&&1)
    9. 9 {
    10. 10 cout++;
    11. 11 c = c>>1;
    12. 12 }
    13. 13 i++;
    14. 14 }
    15. 15 printf("%d\n",cout);
    16. 16
    17. 17 return 0;
    18. 18 }

    6.编写函数形式参数为字符数组,对该字符数组存储的字符串中的字符从小到大排序并输出。

    1. 1 #include <stdio.h>
    2. 2 #include <string.h>
    3. 3
    4. 4 #define N 6
    5. 5
    6. 6 int main(void) {
    7. 7
    8. 8 int i,j;
    9. 9 char a[] = "asdfgh";
    10. 10 for(i = 0;i<N-1;i++)
    11. 11 {
    12. 12 for(j = 0;j<N-1-i;j++)
    13. 13 {
    14. 14 if(a[j]>a[j+1])
    15. 15 {
    16. 16 int temp = a[j];
    17. 17 a[j] = a[j+1];
    18. 18 a[j+1] = temp;
    19. 19 }
    20. 20 }
    21. 21 }
    22. 22 printf("%s\n",a);
    23. 23
    24. 24 return 0;
    25. 25 }

    C++实现:https://blog.csdn.net/qq_23100787/article/details/50475510

    1. 1 #include <iostream>
    2. 2 #include <string>
    3. 3 #include <algorithm>
    4. 4 using namespace std;
    5. 5
    6. 6 int main() {
    7. 7
    8. 8 string a = "bcasd";
    9. 9 sort(a.begin(),a.end());
    10. 10 cout << a << endl;
    11. 11
    12. 12 return 0;
    13. 13 }

    7.一个字符串和一个单个字符,在字符中删除所有的单个字符

    1. 1 #include <stdio.h>
    2. 2
    3. 3 int main()
    4. 4 {
    5. 5 char a[] = "asdfga";
    6. 6 char b = 'a';
    7. 7 for(int i = 0;a[i]!='\0';i++)
    8. 8 {
    9. 9 if(a[i]==b)
    10. 10 {
    11. 11 for(int j = i;a[j]!='\0';j++)
    12. 12 {
    13. 13 a[j] = a[j+1];
    14. 14 }
    15. 15 }
    16. 16 }
    17. 17 printf("%s\n",a);
    18. 18
    19. 19 return 0;
    20. 20 }

    8.编写函数,函数有两个字符串类型的形参,将在第一个字符串中出现的但在第二个字符串未出现的字符存放在第三个数组中并输出。

    例如:第一个字符串是"ABACDEFGH" 第二个字符串是"BCD" 则第三个字符串是"AAEFGH"

    1. 1 #include <stdio.h>
    2. 2
    3. 3 int main()
    4. 4 {
    5. 5 int i,j,temp,x = 0;
    6. 6 char a[] = "ABACDEFGH",b[] = "BCD",c[10];
    7. 7 for(i = 0;a[i]!='\0';i++)
    8. 8 {
    9. 9 temp = 1;
    10. 10 for(j = 0;b[j]!='\0'&&temp;j++)
    11. 11 if(a[i] == b[j]) temp = 0;
    12. 12 if(temp)
    13. 13 {
    14. 14 c[x] = a[i];
    15. 15 x++;
    16. 16 }
    17. 17 }
    18. 18 c[x] = 0;
    19. 19 printf("%s\n",c);
    20. 20 return 0;
    21. 21 }

    9.对于一个字符串,设计一个算法,将包括i位置在内的左侧部分移动到右边,将右侧部分移动到左边。给定字符串A和它的长度n以及特点位置p,请返回旋转后的结果。

    1. 1 #include <stdio.h>
    2. 2
    3. 3 int main()
    4. 4 {
    5. 5 char a[] = "asdfgh",b[10];
    6. 6 int n = 3,j = 0,i;
    7. 7 for(i = n;a[i]!='\0';i++)
    8. 8 {
    9. 9 b[j] = a[i];
    10. 10 j++;
    11. 11 }
    12. 12 for(i = 0;i<n;i++)
    13. 13 {
    14. 14 b[j] = a[i];
    15. 15 j++;
    16. 16 }
    17. 17 b[j] = 0;
    18. 18 printf("%s\n",b);
    19. 19
    20. 20 return 0;
    21. 21 }

     10.strcpy函数的实现

    原型:

    1. 1 #include <stdio.h>
    2. 2 #include <string.h>
    3. 3
    4. 4 int main()
    5. 5 {
    6. 6 char a[] = "********";
    7. 7 char b[] = "asdf";
    8. 8 strcpy(a,b);
    9. 9 printf("%s\n",a);
    10. 10
    11. 11 return 0;
    12. 12 }

    strcpy的底层实现:

    1. 1 #include <stdio.h>
    2. 2 #include <string.h>
    3. 3
    4. 4 void Strcpy(char *a,char *b)
    5. 5 {
    6. 6 while(*b == '\0')
    7. 7 {
    8. 8 *a = *b;
    9. 9 *a++;
    10. 10 *b++;
    11. 11 }
    12. 12 *a = *b;
    13. 13 }
    14. 14
    15. 15 int main()
    16. 16 {
    17. 17 char a[] = "********";
    18. 18 char b[] = "asdf";
    19. 19 strcpy(a,b);
    20. 20 printf("%s\n",a);
    21. 21
    22. 22 return 0;
    23. 23 }

    11.strcmp函数的实现

    原型:

    参数:str1 – 要进行比较的第一个字符串。str2 – 要进行比较的第二个字符串。

    返回值:(比较指定的ASCII值) 如果 str1 < str2。返回值< 0,如果 str2 > str1。返回值> 0,如果 str1 = str2,返回值 = 0

    1. 1 #include <stdio.h>
    2. 2 #include <string.h>
    3. 3
    4. 5 int main()
    5. 6 {
    6. 7 char a[] = "asdf";
    7. 8 char b[] = "asdf";
    8. 9 char c[] = "asd";
    9. 10 int i = strcmp(a,b);
    10. 11 printf("%d\n",i);
    11. 12
    12. 13 return 0;
    13. 14 }

    底层实现:

    1. 1 #include <stdio.h>
    2. 2 #include <string.h>
    3. 3
    4. 4 int strcmp(char *a,char *b)
    5. 5 {
    6. 6 while(*a == *b)
    7. 7 {
    8. 8 if(*a == '\0')
    9. 9 {
    10. 10 return 0;
    11. 11 }
    12. 12 *a++;
    13. 13 *b++;
    14. 14 }
    15. 15 if(*a > *b)
    16. 16 {
    17. 17 return 1;
    18. 18
    19. 19 }else if(*a < *b){
    20. 20
    21. 21 return -1;
    22. 22 }
    23. 23
    24. 24 return 0;
    25. 25 }
    26. 26
    27. 27 int main()
    28. 28 {
    29. 29 char a[] = "asdf";
    30. 30 char b[] = "asdf";
    31. 31 char c[] = "asd";
    32. 32 int i = strcmp(a,b);
    33. 33 printf("%d\n",i);
    34. 34
    35. 35 return 0;
    36. 36 }

    12.strlen函数的实现 

    原型:

    1. 1 #include <stdio.h>
    2. 2 #include <string.h>
    3. 3
    4. 4 int main()
    5. 5 {
    6. 6 char a[] = "asdfg";
    7. 7 int i = strlen(a);
    8. 8 printf("%d\n",i);
    9. 9
    10. 10 return 0;
    11. 11 }
    1. 1 #include <stdio.h>
    2. 2
    3. 3 int Strlen(char *a)
    4. 4 {
    5. 5 int i = 0;
    6. 6 while(*a != '\0')
    7. 7 {
    8. 8 *a++;
    9. 9 i++;
    10. 10 }
    11. 11
    12. 12 return i;
    13. 13 }
    14. 14
    15. 15 int main()
    16. 16 {
    17. 17 char a[] = "asdfg";
    18. 18 int i = Strlen(a);
    19. 19 printf("%d\n",i);
    20. 20
    21. 21 return 0;
    22. 22 }

    13.strcat函数的实现

    原型:

    1. 1 #include <stdio.h>
    2. 2 #include <string.h>
    3. 3
    4. 4 int main()
    5. 5 {
    6. 6 char a[] = "asdfg";
    7. 7 char b[] = "zxcvb";
    8. 8 strcat(a,b);
    9. 9 printf("%s\n",a);
    10. 10
    11. 11 return 0;
    12. 12 }
    1. 1 #include <stdio.h>
    2. 2 #include <string.h>
    3. 3
    4. 4 void Strcat(char *a,char *b)
    5. 5 {
    6. 6 while(*a != '\0')
    7. 7 {
    8. 8 *a++;
    9. 9 }
    10. 10 while(*b != '\0')
    11. 11 {
    12. 12 *a = *b;
    13. 13 *a++;
    14. 14 *b++;
    15. 15 }
    16. 16 *a = '\0';
    17. 17 }
    18. 18
    19. 19 int main()
    20. 20 {
    21. 21 char a[100] = "asdfg";
    22. 22 char b[] = "zxcvb";
    23. 23 Strcat(a,b);
    24. 24 printf("%s\n",a);
    25. 25
    26. 26 return 0;
    27. 27 }

    14.字符串替换空格

    实现函数,把字符串中的空格替换为"%20"

    例如:把字符串"We are like study.“中的空格全都替换为”%20",替换之后字符串就变为了"We%20are%20like%20study."

    https://blog.csdn.net/dangzhangjing97/article/details/83956567

    1. 1 #include <stdio.h>
    2. 2 #include <string.h>
    3. 3
    4. 4 int main()
    5. 5 {
    6. 6 int i = 0,a = 0,b = 0,c = 0;
    7. 7 char arr[] = "We are like study";
    8. 8 while(arr[i] != '\0')
    9. 9 {
    10. 10 if(arr[i] == ' ')
    11. 11 {
    12. 12 a++;
    13. 13 }
    14. 14 else
    15. 15 {
    16. 16 b++;
    17. 17 }
    18. 18 i++;
    19. 19 }
    20. 20 c = b + (2 * a);
    21. 21
    22. 22 while(b>=0)
    23. 23 {
    24. 24 if(arr[b] == ' ')
    25. 25 {
    26. 26 arr[c--] = '0';
    27. 27 arr[c--] = '2';
    28. 28 arr[c--] = '%';
    29. 29 }
    30. 30 else
    31. 31 {
    32. 32 arr[c--] = arr[b];
    33. 33 }
    34. 34 b--;
    35. 35 }
    36. 36 printf("%s\n",arr);
    37. 37
    38. 38 return 0;
    39. 39 }

    C++实现:

    扫描字符串,遇见空格时将空格替换为0,并在该位置前面插入%2。本方法用到了字符串的insert函数。insert函数插入会将其余字符串后移。所以时间复杂度是O(n2)。

    1. 1 #include <iostream>
    2. 2 #include <string>
    3. 3 using namespace std;
    4. 4
    5. 5 class node{
    6. 6 public:
    7. 7 string replace(string s){
    8. 8 string str = "%2";
    9. 9 for(int i = 0;i<s.size();i++){
    10. 10 if(s[i] == ' '){
    11. 11 s[i] = '0';
    12. 12 s.insert(i,str);
    13. 13 }
    14. 14 }
    15. 15 return s;
    16. 16 }
    17. 17 };
    18. 18
    19. 19 int main() {
    20. 20
    21. 21 node n;
    22. 22 string s = "We are happy";
    23. 23 cout << n.replace(s) << endl;
    24. 24
    25. 25 return 0;
    26. 26 }

    先扫描一遍字符串,记录下有多少个空格,然后对字符串扩容,长度为将空格替换为%20后的长度。然后定义两个指针i,j。i从旧字符串末尾开始左移,j从新字符串末尾开始左移。没有遇见空格,就执行s[j]=s[i];遇见空格将连着的三个位置分别赋予0,2,%。如果j=i了,说明已经没有空格,j前面的字符串不必再赋予,时间复杂度是O(n)。

    1. 1 #include <iostream>
    2. 2 #include <string>
    3. 3 using namespace std;
    4. 4
    5. 5 class node{
    6. 6 public:
    7. 7 string replace(string s){
    8. 8 int oldlen = s.size(),cout = 0;
    9. 9 for(int i = 0;i<s.size();i++){
    10. 10 if(s[i] == ' ') cout++;
    11. 11 }
    12. 12 s.resize(s.size()+2*cout);
    13. 13 int newlen = s.size();
    14. 14 for(int i = oldlen-1,j = newlen-1;i<j;i--,j--){
    15. 15 if(s[i] != ' ') s[j] = s[i];
    16. 16 else{
    17. 17 s[j] = '0';
    18. 18 s[--j] = '2';
    19. 19 s[--j] = '%';
    20. 20 }
    21. 21 }
    22. 22 return s;
    23. 23 }
    24. 24 };
    25. 25
    26. 26 int main() {
    27. 27
    28. 28 node n;
    29. 29 string s = "We are happy";
    30. 30 cout << n.replace(s) << endl;
    31. 31
    32. 32 return 0;
    33. 33 }

    15.最长公共前缀(力扣14题)

    1. 1 #include <iostream>
    2. 2 #include <vector>
    3. 3 using namespace std;
    4. 4
    5. 5 class node{
    6. 6 public:
    7. 7 string longest(vector<string>& strs){
    8. 8 for(int i = 0;i<strs[0].size();i++){
    9. 9 char c = strs[0][i];
    10. 10 for(int j = 1;j<strs.size();j++){
    11. 11 if(i == strs[j].size() || c != strs[j][i]){
    12. 12 return strs[0].substr(0,i);
    13. 13 }
    14. 14 }
    15. 15 }
    16. 16 return strs[0];
    17. 17 }
    18. 18 };
    19. 19
    20. 20 int main()
    21. 21 {
    22. 22 node n;
    23. 23 vector<string>strs;
    24. 24 strs.push_back("abcd");
    25. 25 strs.push_back("abc");
    26. 26 strs.push_back("ab");
    27. 27 cout << n.longest(strs) << endl;
    28. 28
    29. 29 return 0;
    30. 30 }

    16.最长回文子串(力扣第五题)

    1. 1 #include<iostream>
    2. 2 using namespace std;
    3. 3
    4. 4 class node{
    5. 5 public:
    6. 6 string longgest(string s){
    7. 7 string ans = "";
    8. 8 for(int i = 0;i<s.size();i++){
    9. 9 int l = i-1,r = i+1;
    10. 10 while(l>=0 && r<s.size() && s[l] == s[r]) l--,r++;
    11. 11 if(ans.size() < r-l-1) ans = s.substr(l+1,r-l-1);
    12. 12
    13. 13 int x = i,y = i+1;
    14. 14 while(x>=0 && y<s.size() && s[l] == s[r]) x--,y++;
    15. 15 if(ans.size() < y-x-1) ans = s.substr(x+1,y-l-1);
    16. 16 }
    17. 17 return ans;
    18. 18 }
    19. 19 };
    20. 20
    21. 21 int main()
    22. 22 {
    23. 23 node n;
    24. 24 string s = "asdfdsa";
    25. 25 cout << n.longgest(s) << endl;
    26. 26
    27. 27 return 0;
    28. 28 }

    17.二进制相加(力扣67题)

    1. 1 #include <iostream>
    2. 2 #include <vector>
    3. 3 using namespace std;
    4. 4
    5. 5 class node{
    6. 6 public:
    7. 7 string addbinary(string a,string b){
    8. 8 string ans;
    9. 9 int carry = 0;
    10. 10 int i = a.size() -1,j = b.size() -1;
    11. 11 while(i>=0 || j>=0 || carry){
    12. 12 if(i>=0) carry += a[i--] - '0';
    13. 13 if(j>=0) carry += b[j--] - '0';
    14. 14 ans = to_string(carry%2) + ans;
    15. 15 carry /= 2;
    16. 16 }
    17. 17 return ans;
    18. 18 }
    19. 19 };
    20. 20
    21. 21 int main(){
    22. 22
    23. 23 node n;
    24. 24 string a = "11";
    25. 25 string b = "11";
    26. 26 string c = n.addbinary(a,b);
    27. 27 cout << c << endl;
    28. 28
    29. 29 return 0;
    30. 30 }

  • 相关阅读:
    虚拟机配置完NAT模式之后可以和主机ping通但是ping 百度显示:网络不可达
    Ubuntu终端指令
    04.8. 数值稳定性和模型初始化
    Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis 电子招标采购系统源码
    【控制台】 \xce\xde\xb7\xa8\xb4\xf2\xbf\xaa
    tomcat服务安装步骤以及详细配置教程
    SpringBoot热部署
    Istio 入门(三):体验 Istio、微服务部署、可观测性
    基于Java+SpringBoot+Thymeleaf+Mysql餐厅座位预约预订网站系统设计与实现
    JavaWeb开发之Listener&Filter
  • 原文地址:https://blog.csdn.net/weixin_49303682/article/details/133522631