解析:整数转化为字符串,可以采用加‘0’,再逆序的办法 整数加‘0’就会隐性转化成char类型的数。
1 #include
2
3 int main()
4 {
5 int num = 12345,i = 0,j = 0;
6 char a[7],b[7];
7 while(num)
8 {
9 a[i] = num%10 + '0';
10 num = num/10;
11 i++;
12 }
13 a[i] = 0;
14 i = i-1;
15 printf("%s\n",a);
16 while(i>=0)
17 {
18 b[j] = a[i];
19 j++;
20 i--;
21 }
22 b[j] = 0;
23 printf("%s\n",b);
24
25 return 0;
26 }
可以采用减‘0’再乘10累加的办法,字符串减‘0’就会隐性转化成int类型的数。
1 #include
2
3 int main(void) {
4
5 char a[] = "12345";
6 int i = 0,num = 0;
7 while(a[i])
8 {
9 num = num*10 + (a[i]-'0');
10 i++;
11 }
12 printf("%d\n",num);
13
14 return 0;
15 }
1 #include
2
3 int main(void) {
4
5 char a[] = "asdfg",b[7];
6 int len,i = 0;
7 len = 'a' - 'A';
8 printf("%c%c%c%c%c\n",a[0]-len,a[1]-len,a[2]-len,a[3]-len,a[4]-len);
9
10 return 0;
11 }
1 #include
2 #include
3
4 int main(void) {
5
6 char a[] = "asdfgh",b[10];
7 int i = 0,j = 0;
8 j = strlen(a)-1;
9 while(a[i])
10 {
11 b[i] = a[j];
12 i++;
13 j--;
14 }
15 b[i] = 0;
16 printf("%s\n",b);
17
18 return 0;
19 }
字符串逆序C++实现:https://blog.csdn.net/qq_44711190/article/details/118956333
1 #include
2 #include
3 using namespace std;
4
5 int main() {
6
7 string a = "asdfg";
8 string b(a.rbegin(),a.rend());
9 cout << b << endl;
10
11 return 0;
12 }
1 #include
2
3 int main()
4 {
5 int c = 0xcf,cout = 0,i = 0;
6 while(i<8)
7 {
8 if(c&&1)
9 {
10 cout++;
11 c = c>>1;
12 }
13 i++;
14 }
15 printf("%d\n",cout);
16
17 return 0;
18 }
1 #include
2 #include
3
4 #define N 6
5
6 int main(void) {
7
8 int i,j;
9 char a[] = "asdfgh";
10 for(i = 0;ia[j+1])
15 {
16 int temp = a[j];
17 a[j] = a[j+1];
18 a[j+1] = temp;
19 }
20 }
21 }
22 printf("%s\n",a);
23
24 return 0;
25 }
C++实现:https://blog.csdn.net/qq_23100787/article/details/50475510
1 #include
2 #include
3 #include
4 using namespace std;
5
6 int main() {
7
8 string a = "bcasd";
9 sort(a.begin(),a.end());
10 cout << a << endl;
11
12 return 0;
13 }
1 #include
2
3 int main()
4 {
5 char a[] = "asdfga";
6 char b = 'a';
7 for(int i = 0;a[i]!='\0';i++)
8 {
9 if(a[i]==b)
10 {
11 for(int j = i;a[j]!='\0';j++)
12 {
13 a[j] = a[j+1];
14 }
15 }
16 }
17 printf("%s\n",a);
18
19 return 0;
20 }
例如:第一个字符串是"ABACDEFGH" 第二个字符串是"BCD" 则第三个字符串是"AAEFGH"
1 #include
2
3 int main()
4 {
5 int i,j,temp,x = 0;
6 char a[] = "ABACDEFGH",b[] = "BCD",c[10];
7 for(i = 0;a[i]!='\0';i++)
8 {
9 temp = 1;
10 for(j = 0;b[j]!='\0'&&temp;j++)
11 if(a[i] == b[j]) temp = 0;
12 if(temp)
13 {
14 c[x] = a[i];
15 x++;
16 }
17 }
18 c[x] = 0;
19 printf("%s\n",c);
20 return 0;
21 }
1 #include
2
3 int main()
4 {
5 char a[] = "asdfgh",b[10];
6 int n = 3,j = 0,i;
7 for(i = n;a[i]!='\0';i++)
8 {
9 b[j] = a[i];
10 j++;
11 }
12 for(i = 0;i
原型:
1 #include
2 #include
3
4 int main()
5 {
6 char a[] = "********";
7 char b[] = "asdf";
8 strcpy(a,b);
9 printf("%s\n",a);
10
11 return 0;
12 }
strcpy的底层实现:
1 #include
2 #include
3
4 void Strcpy(char *a,char *b)
5 {
6 while(*b == '\0')
7 {
8 *a = *b;
9 *a++;
10 *b++;
11 }
12 *a = *b;
13 }
14
15 int main()
16 {
17 char a[] = "********";
18 char b[] = "asdf";
19 strcpy(a,b);
20 printf("%s\n",a);
21
22 return 0;
23 }
原型:
参数:str1
– 要进行比较的第一个字符串。str2
– 要进行比较的第二个字符串。
返回值:(比较指定的ASCII值) 如果 str1 < str2
。返回值<
0,如果 str2 > str1
。返回值>
0,如果 str1 = str2
,返回值 =
0
1 #include
2 #include
3
5 int main()
6 {
7 char a[] = "asdf";
8 char b[] = "asdf";
9 char c[] = "asd";
10 int i = strcmp(a,b);
11 printf("%d\n",i);
12
13 return 0;
14 }
底层实现:
1 #include
2 #include
3
4 int strcmp(char *a,char *b)
5 {
6 while(*a == *b)
7 {
8 if(*a == '\0')
9 {
10 return 0;
11 }
12 *a++;
13 *b++;
14 }
15 if(*a > *b)
16 {
17 return 1;
18
19 }else if(*a < *b){
20
21 return -1;
22 }
23
24 return 0;
25 }
26
27 int main()
28 {
29 char a[] = "asdf";
30 char b[] = "asdf";
31 char c[] = "asd";
32 int i = strcmp(a,b);
33 printf("%d\n",i);
34
35 return 0;
36 }
原型:
1 #include
2 #include
3
4 int main()
5 {
6 char a[] = "asdfg";
7 int i = strlen(a);
8 printf("%d\n",i);
9
10 return 0;
11 }
1 #include
2
3 int Strlen(char *a)
4 {
5 int i = 0;
6 while(*a != '\0')
7 {
8 *a++;
9 i++;
10 }
11
12 return i;
13 }
14
15 int main()
16 {
17 char a[] = "asdfg";
18 int i = Strlen(a);
19 printf("%d\n",i);
20
21 return 0;
22 }
原型:
1 #include
2 #include
3
4 int main()
5 {
6 char a[] = "asdfg";
7 char b[] = "zxcvb";
8 strcat(a,b);
9 printf("%s\n",a);
10
11 return 0;
12 }
1 #include
2 #include
3
4 void Strcat(char *a,char *b)
5 {
6 while(*a != '\0')
7 {
8 *a++;
9 }
10 while(*b != '\0')
11 {
12 *a = *b;
13 *a++;
14 *b++;
15 }
16 *a = '\0';
17 }
18
19 int main()
20 {
21 char a[100] = "asdfg";
22 char b[] = "zxcvb";
23 Strcat(a,b);
24 printf("%s\n",a);
25
26 return 0;
27 }
实现函数,把字符串中的空格替换为"%20"
例如:把字符串"We are like study.“中的空格全都替换为”%20",替换之后字符串就变为了"We%20are%20like%20study."
https://blog.csdn.net/dangzhangjing97/article/details/83956567
1 #include
2 #include
3
4 int main()
5 {
6 int i = 0,a = 0,b = 0,c = 0;
7 char arr[] = "We are like study";
8 while(arr[i] != '\0')
9 {
10 if(arr[i] == ' ')
11 {
12 a++;
13 }
14 else
15 {
16 b++;
17 }
18 i++;
19 }
20 c = b + (2 * a);
21
22 while(b>=0)
23 {
24 if(arr[b] == ' ')
25 {
26 arr[c--] = '0';
27 arr[c--] = '2';
28 arr[c--] = '%';
29 }
30 else
31 {
32 arr[c--] = arr[b];
33 }
34 b--;
35 }
36 printf("%s\n",arr);
37
38 return 0;
39 }
C++实现:
扫描字符串,遇见空格时将空格替换为0,并在该位置前面插入%2。本方法用到了字符串的insert函数。insert函数插入会将其余字符串后移。所以时间复杂度是O(n2)。
1 #include
2 #include
3 using namespace std;
4
5 class node{
6 public:
7 string replace(string s){
8 string str = "%2";
9 for(int i = 0;i
先扫描一遍字符串,记录下有多少个空格,然后对字符串扩容,长度为将空格替换为%20后的长度。然后定义两个指针i,j。i从旧字符串末尾开始左移,j从新字符串末尾开始左移。没有遇见空格,就执行s[j]=s[i];遇见空格将连着的三个位置分别赋予0,2,%。如果j=i了,说明已经没有空格,j前面的字符串不必再赋予,时间复杂度是O(n)。
1 #include
2 #include
3 using namespace std;
4
5 class node{
6 public:
7 string replace(string s){
8 int oldlen = s.size(),cout = 0;
9 for(int i = 0;i
1 #include
2 #include
3 using namespace std;
4
5 class node{
6 public:
7 string longest(vector& strs){
8 for(int i = 0;istrs;
24 strs.push_back("abcd");
25 strs.push_back("abc");
26 strs.push_back("ab");
27 cout << n.longest(strs) << endl;
28
29 return 0;
30 }
1 #include
2 using namespace std;
3
4 class node{
5 public:
6 string longgest(string s){
7 string ans = "";
8 for(int i = 0;i=0 && r=0 && y
1 #include
2 #include
3 using namespace std;
4
5 class node{
6 public:
7 string addbinary(string a,string b){
8 string ans;
9 int carry = 0;
10 int i = a.size() -1,j = b.size() -1;
11 while(i>=0 || j>=0 || carry){
12 if(i>=0) carry += a[i--] - '0';
13 if(j>=0) carry += b[j--] - '0';
14 ans = to_string(carry%2) + ans;
15 carry /= 2;
16 }
17 return ans;
18 }
19 };
20
21 int main(){
22
23 node n;
24 string a = "11";
25 string b = "11";
26 string c = n.addbinary(a,b);
27 cout << c << endl;
28
29 return 0;
30 }