字符串函数集

【1】字符数组的环形移动如何实现?

要求:函数原型如下:

void RightLoopMove(char *pstr, unsigned short steps)

参数1:char *pstr 表示需要移动的字符串

参数2:unsigned short steps 表示从第几个字符开始向右移动

返回值类型:void

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int mystrlen(char* str)
 5 {
 6     int Length = 0;
 7     if (NULL != str)
 8     {
 9         for (;*str++ != '\0';Length += 1);
10         return Length;
11     }
12     return Length;
13 }
14 
15 void RightLoopMove(char *pstr, unsigned short steps)
16 {
17     if (NULL == pstr)
18         return;
19 
20     char tmp;
21     int len = mystrlen(pstr);
22     steps %= len;
23     while (steps-- > 0)
24     {
25         tmp = *(pstr + len -1);
26         for (int i = len - 2;i >= 0;--i)
27         {
28             pstr[i + 1] = pstr[i];
29         }
30         pstr[0] = tmp;
31     }
32 }
33 
34 void main()
35 {
36     char str[] = "abcdefghijklmn";
37     RightLoopMove(str, 4);
38     cout << str << endl;         //klmnabcdefghij
39     system("pause");
40 }

【2】如何判断一个字符串是否是回文串?

要求:函数原型如下:

int  IsRevStr(char *str)

"回文数"是一种数字。如:98789, 这个数字正读是98789,倒读也是98789,正读倒读一样,所以这个数字就是回文数。

字符的以此类推.....

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int mystrlen(const char *str)
 5 {
 6     int Length = 0;
 7     if (NULL != str)
 8     {
 9         for (; *str++ != '\0'; Length++);
10         return Length;
11     }
12     return Length;
13 }
14 
15 int  IsRevStr(char *str)
16 {
17     int i, len;
18     int found = 1;
19     if (NULL == str)
20     {
21         return -1;
22     }
23 
24     len = mystrlen(str);
25     for (i = 0; i < len/2; i++)
26     {
27         if (*(str + i) != *(str + len - i - 1))
28         {
29             found = 0;
30             break;
31         }
32     }
33     return found;
34 }
35 
36 void main()
37 {
38     char *str1 = "abcdeedcba";
39     char *str2 = "abfba";
40     char *str3 = "liuyong";
41     cout << IsRevStr(str1) << endl;  // 1
42     cout << IsRevStr(str2) << endl;  // 1
43     cout << IsRevStr(str3) << endl;  // 0
44     system("pause");
45 }

【3】如何把数字字符串转换为整型数据?

要求:函数原型如下:

int   str2int (const   char   *str);

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int str2int(const char *str)
 5 {
 6     int temp = 0;
 7     const char *ptr = str;          // ptr保存str字符串开头
 8     if (*str == '-'|| *str == '+')     // 若第一个字符为符号位
 9     {
10         str++;   // 前移一位
11     }
12     //转换为数据
13     while (*str != 0)
14     {
15         if ((*str < '0') || (*str > '9'))
16         {
17             break;
18         }
19         temp = temp*10 + (*str - '0');
20         str++;
21     }
22     //若为负数
23     if (*ptr == '-')
24     {
25         temp = -temp;
26     }
27     return  temp;
28 }
29 
30 void  main()
31 {
32     char *str1 = "-1234";
33     char *str2 = "1234";
34     int data1 = str2int(str1);   
35     cout << data1 << endl;         // -1234
36     int data2 = str2int(str2);
37     cout << data2 << endl;         // 1234
38     system("pause");
39 }

【4】如何把整型数据转换为字符串?

要求:函数原型如下:

void  int2str(int data,char *str)

示例代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 void int2str(int data, char *str)
 5 {
 6     if (NULL == str)
 7     {
 8         return;
 9     }
10 
11     char buf[10];
12     int i = 0, len = 0;
13     int temp = data < 0 ? -data : data;   // temp为n的绝对值
14     while (temp)
15     {
16         buf[i++] = (temp % 10) + '0';        // 把temp每一位上的数存入buf
17         temp /= 10;
18     }
19     // data的位数加一
20     buf[i] = '\0';
21     len = data < 0 ? ++i : i;
22     str[i] = '\0';  // 最末位置为‘0’
23     while (1)
24     {
25         --i;
26         if (buf[len - i - 1] == '\0')
27         {
28             break;
29         }
30         str[i] = buf[len - i - 1];    // 把buf内的字符拷到字符串
31     }
32     
33     if (0 == i)
34     {
35         str[i] = '-';   // 如果是负数,添加一个负号
36     }
37 }
38 
39 void  main()
40 {
41     int a = 1234;
42     int b = -1234;
43     char str1[10], str2[10];
44     int2str(a, str1);
45     int2str(b, str2);
46     cout << str1 << endl;  // 1234
47     cout << str2 << endl;  // -1234
48     system("pause");
49 }

【5】如何对字符串进行排序?

示例代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int mystrlen(const char * str)
 5 {
 6     int Length = 0;
 7     if (NULL != str)
 8     {
 9         for (; *str++ != '\0'; Length += 1);
10         return Length;
11     }
12     return Length;
13 }
14 
15 void strsort(char *str)
16 {
17     int i,j;
18     int temp = 0;
19     int num = mystrlen(str);
20     for (i = 0; i < num; i++)
21     {
22         for (j = i + 1; j < num; j++)
23         {
24             if (str[i] > str[j])
25             {
26                 temp = str[i];
27                 str[i] = str[j];
28                 str[j] = temp;
29             }
30         }
31     }
32 }
33 
34 void  main()
35 {
36     char ch[] = "lkjhgfdsa";
37     strsort(ch);
38     cout << ch << endl;  // adfghjkls
39     system("pause");
40 }

【6】如何把字符串中某个指定的字符删除?

要求:函数原型如下:

char * DeleChar(char *str,char c)

示例代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 char * DeleChar(char *str, char c)
 5 {
 6     char *head = NULL;
 7     char *p = NULL;
 8     if (NULL == str)
 9         return NULL;
10     head = p = str;   // 指向字符串头,准备遍历
11     while (*p)
12     {
13         if (*p != c)
14         {
15             *str++ = *p++;
16         }
17         else
18         {
19             ++p;
20         }
21     }
22     *str = '\0';
23     return head;
24 }
25 
26 void  main()
27 {
28     char ch[] = "agggbcabcsbddbf";
29     DeleChar(ch, 'b');
30     cout << ch << endl;   // agggcacsddf
31     system("pause");
32 }

【7】如何找出01字符串中0与1出现的最大次数?

要求:函数原型如下:

void  Calculate(const char *str,int *max0,int *max1)

示例代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 void  Calculate(const char *str, int *max0, int *max1)
 5 {
 6     int temp0 = 0;
 7     int temp1 = 0;
 8     while (*str)
 9     {
10         if (*str == '0')
11         {
12             (*max0)++;
13             if (*(++str) == '1')
14             {
15                 if (temp0 < *max0)
16                 {
17                     temp0 = *max0;
18                 }
19                 *max0 = 0;
20             }
21         }
22         else if (*str == '1')
23         {
24             (*max1)++;
25             if (*(++str) == '0')
26             {
27                 if (temp1 < *max1)
28                 {
29                     temp1 = *max1;
30                 }
31                 *max1 = 0;
32             }
33         }
34     }
35 
36     *max0 = temp0;
37     *max1 = temp1;  
38 }
39 
40 void  main()
41 {
42     char string[] = "0101111111100000111";
43     int max0 = 0;
44     int max1 = 0;
45     Calculate(string, &max0, &max1);
46     cout << "max0:" << max0 << "max1:" << max1 << endl;   // max0:5   max1:8
47     system("pause");
48 }

【8】如何从字符串的某一个位置删除指定个数的字符?

函数原型如下:

char  *deleteChar(char *str,int pos,int len)

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int mystrlen(const char * str)
 5 {
 6     int Length = 0;
 7     if (NULL != str)
 8     {
 9         for(; *str++ != '\0'; Length++);
10         return Length;
11     }
12     return Length;
13 }
14 
15 char  *deleteChar(char *str,int pos,int len)
16 {
17     char *p = str + pos - 1;
18     int tt = mystrlen(str);
19     if ((pos < 1) || (p - str) > tt)  // pos不大于1 或者 pos不超出字符串的长度
20         return str;
21     if ((p + len - str) > tt)   // len大于pos后剩余的个数
22     {
23         *p = '\0';
24         return str;
25     }
26     while (*p && *(p + len))   // len小于或者等于剩余的个数
27     {
28         *p = *(p + len);
29         p++;
30     }
31     *p = '\0';
32     return str;
33 }
34 
35 void main()
36 {
37     char delstr[] = "afdfjgkglkgdg";
38     cout << "删除前字符串:" << delstr << endl;
39     char *dest = deleteChar(delstr, 3, 4);
40     cout << "从第三个字符位置,删除4个字符:" << endl;
41     cout << "删除后字符串:" << dest << endl;
42     system("pause");
43 }
44 
45 // The result of this
46 /*
47 删除前字符串: afdfjgkglkgdg
48 从第三个字符位置,删除4个字符:
49 删除后字符串: afkglkgdg
50 */

【9】写一个函数把字符串反转。

函数实现及测试程序代码如下:

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4  
 5 // 字符串中每个字符的反转
 6 char* strRev(const char *str)
 7 {
 8     char *pTmp = new char[strlen(str) + 1];
 9     strcpy(pTmp, str);
10     char *ret = pTmp;
11     char *p = pTmp + strlen(str) - 1;
12     while (p > pTmp)
13     {
14         *p ^= *pTmp;
15         *pTmp ^= *p;
16         *p ^= *pTmp;
17         --p;
18         ++pTmp;
19     }
20 
21     return ret;
22 }
23 
24 void  main()
25 {
26     char * str = "abcdef";
27     cout << strRev(str) << endl;  // fedcba
28     system("pause");
29 }

【10】写一个函数查找两个字符串中的第一个公共字符串

函数实现及测试程序代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int strLen(const char *str)
 5 {
 6     int len = 0;
 7     if (NULL != str)
 8     {
 9         while (*str++ != '\0')
10         {
11             len++;
12         }
13         return len;
14     }
15     return len;
16 }
17 
18 // 两个字符串中的第一个公共子串
19 char *commonstr(char *str1, char *str2)
20 {
21     int i, j, k;
22     char *shortstr, *longstr;
23     char *substr;
24     if ((NULL == str1)||(NULL == str2))
25     {
26         return NULL;
27     }
28 
29     if (strLen(str1) <= strLen(str2))
30     {
31         shortstr = str1;
32         longstr = str2;
33     }
34     else
35     {
36         shortstr = str2;
37         longstr = str1;
38     }
39 
40     if (strstr(longstr, shortstr) != NULL)
41     {
42         return shortstr;
43     }
44     // 申请堆内存存放返回结果
45     substr = (char *)malloc(sizeof(char)*(strLen(shortstr) + 1));
46     i = strLen(shortstr) - 1;  // i = 4 = 5 - 1
47     for (; i > 0;i--)
48     {
49         k = strLen(shortstr) - i; // k = 5 - i
50         for (j = 0;j <= k;j++)
51         {
52             memcpy(substr, &shortstr[j], i);
53             substr[i] = '\0';  
54             if (strstr(longstr, substr) != NULL)             
55             {
56                 return substr;
57             }
58         }
59     }
60 
61     return NULL;
62 }
63 
64 void main()
65 {
66     char *pStr1 = "abcdefghijklmnopqrstuvwxyz";
67     char *pStr2 = "asdghihgksdkmnohklukjlgsdjk";
68     cout << commonstr(pStr1, pStr2) << endl; // ghi(第二个公共字符串:mno)
69     system("pause");
70 }

【11】写一个函数在字符串N中查找第一次出现子串M的位置。

函数实现以及测试程序代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 // 在字符串n中查找第一次出现子串m的位置
 5 int StrStr(const char *src, const char *sub)
 6 {  
 7     const char *bp;
 8     const char *sp;
 9     int nIndex = -1;
10     if ((NULL == src)||(NULL == sub))
11     {
12         return nIndex;
13     }
14 
15     nIndex = 0;
16     while (*src)
17     {
18         bp = src;
19         sp = sub;
20         do
21         {
22             if (!*sp)
23             {
24                 return nIndex;
25             }
26         } while (*bp++ == *sp++);
27 
28         ++src;
29         ++nIndex;
30     }
31 
32     return -1;
33 }
34 
35 void main()
36 {
37     char *pStr = "abcdefghijklmn";
38     char *pDes = "ghi";
39     char *pSec = "sec";
40     cout << StrStr(pStr, pDes) << endl;  // 5
41     cout << StrStr(pStr, pSec) << endl;  // -1
42     cout << StrStr(pStr, NULL) << endl;  // -1
43     cout << StrStr(NULL, pSec) << endl;  // -1
44     system("pause");
45 }
46 // run out:
47 /*
48 6
49 -1
50 -1
51 -1
52 请按任意键继续. . .
53 */

【12】写一个函数把字符串A中的B字符子串用字符串C进行替换。

函数实现以及测试程序代码如下:

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 // 在字符串n中查找第一次出现子串m的位置
 6 const char * StrStr(const char *src, const char *sub)
 7 {  
 8     const char *bp;
 9     const char *sp;
10     int nIndex = -1;
11     if ((NULL == src)||(NULL == sub))
12     {
13         return src;
14     }
15 
16     while (*src)
17     {
18         bp = src;
19         sp = sub;
20         do
21         {
22             if (!*sp)
23             {
24                 return src;
25             }
26         } while (*bp++ == *sp++);
27 
28         ++src;
29     }
30 
31     return NULL;
32 }
33 
34 //把字符串b中的所有字符串a用字符串c进行替换
35 char* replace(const char *str, const char *sub1, const char *sub2, char *output)
36 {
37     char *pOutput = NULL;
38     const char *pStr = NULL;
39     int lenSub1 = strlen(sub1);    // 子串sub1的长度
40     int lenSub2 = strlen(sub2);    // 子串sub2的长度
41     pOutput = output;
42     pStr = str; // 用于寻找子串
43     while (*pStr != 0)
44     {
45         pStr = StrStr(pStr, sub1);
46         if (NULL != pStr)
47         {
48             memcpy(pOutput, str, pStr-str); // 复制str的前一部分至pOutput
49             pOutput += pStr - str;
50             memcpy(pOutput, sub2, lenSub2);  // 复制sub2子串到output
51             pOutput += lenSub2;
52             pStr += lenSub1;   // 为下一次做准备
53             str = pStr;
54         }
55         else
56         {
57             break;
58         }
59     }
60     *pOutput = '\0';
61     if (*str != '\0')
62     {
63         strcpy(pOutput, str);
64     }
65 
66     return output;
67 }
68 
69 void  main()
70 {
71     char* str = "abcdefghijklabcdefstuvwxyz";
72     char* str1 = "abcdef";
73     char* str2 = "fffbb";
74     char* outPut = (char *)malloc(sizeof(char) * 50);
75     cout << replace(str, str1, str2, outPut) << endl;
76     free(outPut);
77     system("pause");
78 }
79 // run out:
80 /*
81 fffbbghijklfffbbstuvwxyz
82 请按任意键继续. . .
83 */

【13】待续...

 

Good  Good  Study, Day  Day  Up.

顺序  选择  循环  总结

 

posted @ 2012-12-31 15:39  kaizenly  阅读(1233)  评论(0编辑  收藏  举报
打赏