第三天,指针相关的编程题
今天时间比较多,来做题啦!
编程题
1.用指针统计两个字符数组中相同字符的个数
1. 编写程序,实现:利用指向字符数组的指针变量,统计两个字符数组中相同的字符个数。
思路: 逐个取str1的字符与str2比较,相同则total++
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcde";
char str2[] = "defghijkla";
char *s1 = str1;
char *s2 = str2;
char *tmp = s2;
int len1 = strlen(str1);
int len2 = strlen(str2);
int total = 0;
for(int i=0; i<len1; i++)
{
for(int j=0; j<len2; j++)
{
if(*s1 == *s2)
total++;
s2++;
}
s1++;
s2 = tmp;
}
printf("same char count is %d\n", total);
return 0;
}
2. 输入 3 个整数,按从小到大的顺序输出,用 3 种不同方式实现。
这个好像跟指针不相关,不写了
3.用指针截取字符数组
3. 有一字符串,包含 n 个字符。 使用指针,将此字符串从第 m 个字符开始的全部字符复制成为另一个字符串。
思路: 这个太简单了,指针++到m位置就行了,复制用strcpy就行了
#include <stdio.h>
#include <string.h>
int main()
{
int m = 5;
char str1[] = {};
char str2[] = "defghijkla";
char *s = str2;
while(1 != m--)
s++;
printf("%s\n", s);
strcpy(str1, s);
printf("%s\n", str1);
return 0;
}
}
4.用指针提取字符串中的数字,如a123x456 17960? 302tab5876
4. 输入一个字符串,内有数字和非数字字符,如:a123x456 17960? 302tab5876
将其中连续的数字作为一个整数,依次存放到一数组 a 中。 例如,123 放在 a[0]中,456 放在 a[1]中……试统计共有多少个整数,并输出这些数。
思路: 直接说原理吧,思路不好讲,以"a123x456 17960? 302tab5876"
为例
刚开始s1
和s2
都指向该字符数组,
当检测到第一个字符时,n=0,m=1,q=n=0
第二个字符时n=1,m=1,q=n=1,n=1时,说明发现数字了,此时s1指向字符'1'
,使s2也指向该字符即s2 = s1;
第三个字符时n=2,m=1,q=n=2
第四个字符时n=3,m=1,q=n=3
第五个字符时n=3,m=2,q=n=3,比较第四次的q和第五次的n,发现两者相等,说明第五次检测到非数字了,那从s2开始的n个字符不就是数字吗,123
打印出来即可,放在数组中的操作太简单了不屑的做,哈哈!
看不懂的话画个图就懂了
…
吐个槽,这题还是很难的,关键的已经不是指针操作了,而是逻辑的处理。别看就十几行代码,我搞了一个半小时,我觉得难点在于用指针
如何取出来那一连串的数字,对于非数字开头的字符串、数字结尾的字符串都得能处理,改来改去花费了那么多时间。代码中的6个字符数组都测试过了,都ok的
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int n = 0;
int q = 0;
char tmp[20] = {};
char str1[] = "a123x456 17960? 302tab5876";
char str2[] = "a123x456 17960? 302tab5876a";
char str3[] = "abc123x456 17960? 302tab5876";
char str4[] = "abc123x456 17960? 302tab5876cbx";
char str5[] = "123x456 17960? 302tab5876c";
char str6[] = "123x456 17960? 302tab5876cbs";
char *s1 = str6;
char *s2 = str6;
int len = strlen(str6);
for(int i=0; i<len+1; i++)
{
/* 两种判断数字的方式都可以 */
//if(*s1 >= '0' && *s1 <= '9')
if(isdigit(*s1) != 0)
n++;
if(1 == n){
s2 = s1;
}
if(q == n && q != 0){
strncpy(tmp, s2, n);
printf("tmp = %s\n", tmp);
memset(tmp, 0, sizeof(tmp));
n = 0;
}
q = n;
s1++;
//printf("--------------------------------\n");
}
return 0;
}
5. 利用数组和指针,将一个 4 ×4 的矩阵转置,并输出矩阵中的最大值及其位置。
矩阵转置不是高数的东西嘛,哈哈,不会转置了。
6.二级指针对字符串排序
6. 利用指向指针的指针方法对字符串排序并输出。
思路: 字符串排序和普通数字排序没啥区别,唯一的区别在于二级指针操作的时候的写法,像(*q)[j]
,一定要加小括号
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void sort(int n, char **q)
{
char tmp;
for(int i=0; i<n-1; i++)
{
for(int j=0; j<n-1-i; j++)
{
if((*q)[j] > (*q)[j+1])
{
tmp = (*q)[j];
(*q)[j] = (*q)[j+1];
(*q)[j+1] = tmp;
}
}
}
}
int main()
{
char str[7] = "FEDCBA";
char *p = str;
char **q = &p;
printf("before string: %s\n", str);
sort(6, q);
printf("late string: %s\n", str);
return 0;
}
输出:
before string: FEDCBA
late string: ABCDEF
7. 写一程序,求任意一输入字符串的长度,将此输入的字符串按逆序的方式存入所在位置中,显示输入字符串和其逆字符串。
这题用指针太麻烦了,用数组最简单,放弃了!
8.删除英文字符串中的单词并打印出结果(难得一批啊这个)
8. 编写一个程序,先读入一段正文,然后删除其中的单词 from、in、at、an 和 on,最后显示该结果文本段。
思路: 想想都简单,整一个指针,用strncmp分别比较2个和4个字符,符合就删除掉
PS:这特么的思路是我昨天下午四点左右写的,写了个“简单”,MD一共搞了四五个小时才完全解决
用大白话解释一下:当strncmp
检测到字符串中有匹配的字符串str1/str2,用strncat把strA中非匹配字符串拼接到tmp中,使指针s++ n个长度,n是要匹配字符串的长度,这就跳过要删除的字符串了,然后把s拷贝到strA中,如此循环
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char strA[] = "one twon threeonfrom from fivefrom six front action fronm";
char strB[] = "one twon threeonfrom from fivefrom six front action fronm";
//只删除这两个就行了,知道原理即可
char str1[] = "from";
char str2[] = "on";
char *s = strB;
char tmp[100] = {};
int n = 0;
int m = 0;
int count = 0;
while(*s != '\0')
{
LOOP:
n = 4;
m = 2;
if(0 == strncmp(str1, s, n))
{
strncat(tmp, strA, count);
while(n-- && *s != '\0')
s++;
strcpy(strA, s);
count = 0;
goto LOOP;
}
if(0 == strncmp(str2, s, m))
{
strncat(tmp, strA, count);
while(m-- && *s != '\0')
s++;
strcpy(strA, s);
count = 0;
goto LOOP;
}
s++;
count++;
}
strcat(tmp, strA);
printf("%s\n", tmp);
return 0;
}
结果:
e tw three five six frt acti frm