<C> 字符串简单习题

1.查找一个字符在另一个字符串中第一次出现的下标

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int T1(const char* str,char c);
 5 
 6 int main()
 7 {
 8     printf("%d\n",T1("abcd",'c'));
 9     printf("%d\n",T1("abcd",'e'));
10 
11     return 0;
12 }
13 
14 int T1(const char* str,char c)
15 {
16     int Index = 0;
17 
18     while(*str != '\0')
19     {
20         if(*str == c)
21         {
22             return Index;
23         }
24 
25         Index ++;
26         str ++;
27     }
28 
29     return -1;
30 }

2.查找一个字符在另外一个字符串中第一次出现的地址

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 char* T2(char* str,char c);
 5 
 6 int main()
 7 {
 8     printf("%p\n",T2("abcd",'c'));
 9     printf("%p\n",T2("abcd",'e'));
10 
11     return 0;
12 }
13 
14 char* T2(char* str,char c)
15 {
16     while(*str != '\0')
17     {
18         if(*str == c)
19         {
20             return str;
21         }
22         str ++;
23     }
24 
25     return NULL;
26 }

3.查找一个字符串在另外一个字符串中第一次出现的地址 例如:abcccccde ccd

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 char* T3(char* str1,char* str2);
 5 
 6 int main()
 7 {
 8     printf("%s\n",T3("abcccccde","ccd"));
 9 
10     return 0;
11 }
12 
13 char* T3(char* str1,char* str2)
14 {
15     if(strlen(str1) < strlen(str2))
16     {
17         return NULL;
18     }
19     
20     while(*str1 != '\0')
21     {
22         if(*str2 == *str1)
23         {
24             if( 0 == strncmp(str1,str2,strlen(str2)))
25             {
26                 return str1;
27             }
28         }
29         str1 ++;
30     }
31 
32     return NULL;
33 }

4.统计一个字符串中单词的个数(单词与单词之间是空格分割 并且在理想状态下)

 1 #include<stdio.h>
 2 
 3 int T4(char* str);
 4 
 5 int main()
 6 {
 7     char* str = "a abc make 89 12 a ma make 12 abc 89";
 8     printf("%d\n",T4(str));
 9 
10     return 0;
11 }
12 
13 int T4(char* str)
14 {
15     int count = 0;
16     while(*str != '\0')
17     {
18         if(*str == ' ')
19         {
20             count ++;
21         }
22         str ++;
23     }
24     count ++;
25 
26     return count;
27 }

5.将一个字符串插入到另外一个字符串中 例如:“abcd” “12345” 插入位置是第3个元素之后

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 char* T5(char* str1,char* str2,int n);
 5 
 6 int main()
 7 {
 8     char str1[100] = "abcd";
 9     char* str2 = "12345";
10     int n = 3;
11     printf("%s\n",T5(str1,str2,n));
12 
13     return 0;
14 }
15 
16 char* T5(char* str1,char* str2,int n)
17 {
18     char* pInsert = str1 + n;
19     char* pMove = str1 + strlen(str1) + 1;
20 
21     while(pMove >= pInsert)
22     {
23         *(pMove + strlen(str2))= *pMove;
24         pMove --;
25     }
26 
27     while(*str2 != '\0')
28     {
29         *pInsert++ = *str2++;
30     }
31     
32     return str1;
33 }

6.翻转字符串

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 void T6(char* str);
 5 
 6 int main()
 7 {
 8     char str[6] = "12345";
 9     T6(str);
10 
11     printf("%s\n",str);
12 }
13 
14 void T6(char* str)
15 {
16     char* str1 = str;
17     char* str2 = str + strlen(str) - 1;
18 
19     while(*str1 < *str2)
20     {
21         *str1 = *str1 ^ *str2;
22         *str2 = *str1 ^ *str2;
23         *str1 = *str1 ^ *str2;
24     
25         str1 ++;
26         str2 --;
27     }
28 }

7.判断是否回文(回文指的是顺读和逆读都一样的字符串) 例如:“abcdcba”

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int T7(char* str);
 5 
 6 int main()
 7 {
 8     char str[] = "abcdcba";
 9     printf("%d\n",T7(str));
10 
11     return 0;
12 }
13 
14 int T7(char* str)
15 {
16     char* str1 = str;
17     char* str2 = str + strlen(str) -1;
18 
19     while(*str < *str2)
20     {
21         if(*str1 != *str2)
22         {
23             return 0;
24         }
25 
26         *str1 ++;
27         *str2 --;
28     }
29 
30     return 1;
31 }

 

posted @ 2018-04-14 18:44  Aaaaaalei  阅读(183)  评论(0编辑  收藏  举报