字符串面试准备
1.整数转化成字符串,且不使用itoa函数,这里主要要记住正数、负数。
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<queue> 5 #include<math.h> 6 using namespace std; 7 8 int main() 9 { 10 int temp = 1212123; 11 char str[20]; 12 if(temp<0) 13 { 14 str[0]='-'; 15 temp=-temp; 16 } 17 else if(temp>0) 18 str[0]='+'; 19 else 20 str[0] = '0'; 21 int num = 1; 22 while(temp) 23 { 24 str[num] = temp % 10 + '0'; 25 num++; 26 temp = temp/10; 27 } 28 str[num] = '\0'; 29 for(int i=num-1;i>=0.5*num;i--) 30 { 31 int temp = str[i]; 32 str[i] = str[num-i]; 33 str[num-i] = temp; 34 } 35 cout<<str<<endl; 36 }
2.字符串转化成整数,且不使用atoi函数,这里不考虑负字符串了。
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<queue> 5 #include<math.h> 6 using namespace std; 7 8 int main() 9 { 10 char str[7] = {'1','2','3','4','5','6','\0'}; 11 int p,num; 12 p = num =0; 13 while(str[num]) 14 { 15 p = 10*p+str[num++]-'0'; 16 } 17 cout<<p<<endl; 18 return 0; 19 }
3.字符串拷贝,使用自己的mystrcpy函数,格式为char *mystrcpy(char *strDest,char *strSrc);
注意strDest strSrc为空时和相等时的特殊情况。C++时可以用异常的方式处理,这里还没研究
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<queue> 5 #include<math.h> 6 using namespace std; 7 8 char *mystrcpy(char *strDest,const char *strSrc) 9 { 10 if ( strDest == NULL || strSrc == NULL) 11 return NULL ; 12 if ( strDest == strSrc) 13 return strDest ; 14 char *tempptr = strDest ; 15 while( (*strDest++ = *strSrc++) !='\0') 16 ; 17 return tempptr ; 18 19 } 20 21 int main() 22 { 23 char str[7]="abcdef"; 24 char des[7]; 25 mystrcpy(des,str); 26 cout<<des<<endl; 27 return 0; 28 }
4.字符串循环右移n位,比如原来是"abcdefghi",如果n=2,移位后应该是"hiabcdefgh"
这里主要采用库函数,第一种使用strcpy和strncpy的结合;第二种使用memcpy
strcpy和memcpy主要有以下3方面的区别:
1、复制到内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组,结构体、类等。
2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符‘\0‘才结束,所以容易溢出。
3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy。
下面是两种情况的代码:
1.
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<queue> 5 #include<math.h> 6 using namespace std; 7 8 9 int main() 10 { 11 char str[] = "abcdefghi"; 12 int n = 2; 13 char temp[100]; 14 int l = strlen(str)-n; 15 strcpy(temp,str+l); 16 strncpy(temp+n,str,l); 17 temp[strlen(str)] = '\0'; 18 cout<<temp<<endl; 19 return 0; 20 }
2.
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<queue> 5 #include<math.h> 6 using namespace std; 7 8 9 int main() 10 { 11 char str[] = "abcdefghi"; 12 int n = 2; 13 char temp[100]; 14 int l = strlen(str)-n; 15 memcpy(temp,str+l,n); 16 memcpy(temp+n,str,l); 17 temp[strlen(str)] = '\0'; 18 cout<<temp<<endl; 19 return 0; 20 }
8.转换字符串格式为原来字符串里的字符+该字符连续出现的个数,例如字符串1233422222,转化为1121324125(1出现1次,2出现1次,3出现2次……)
这里主要注意个sprintf的用法,代码如下:
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<queue> 5 #include<math.h> 6 using namespace std; 7 8 9 int main() 10 { 11 cout<<"Enter the umbers"<<endl; 12 string str; 13 char reschar[50]; 14 reschar[0] = '\0'; 15 getline(cin, str); 16 int len = str.length(); 17 int count = 1; 18 int k ; 19 for(k=0;k<=len-1;k++) 20 { 21 if(str[k+1]==str[k]) 22 count++; 23 else 24 { 25 sprintf(reschar+strlen(reschar),"%c%d",str[k],count); 26 count=1; 27 } 28 } 29 cout<<reschar<<endl; 30 return 0; 31 }
浙公网安备 33010602011771号