字符串指针

2021.6.7程序设计课上机:

学习内容:

函数,字符串指针
wlacm第十五周题目(6.7周一)
1、自定义字符串长度函数 my_strlen
2、自定义复制字符串的函数 my_strcpy
3、自定义字符串函数 ispalindrome
4、统计元音字母的个数
5、统计子串出现的次数


学习产出:

1、程序设计经典例题5道
2、CSDN 技术博客 1 篇

题目

1、问题 A

自定义字符串长度函数 my_strlen

题目描述

编写自定义函数int my_strlen(char s[]); 检测字符串s的长度

输入

一行字符串(长度不超过100)

输出

字符串的长度

样例输入 Copy

abc
how are you?

样例输出

3
12

提示

main函数的定义如下
#include<stdio.h>
#include<string.h>
int main()
{
char s[100];
gets(s);
printf("%d\n",my_strlen(s) );
}

int my_strlen(char s[])
{
	int i,n=0;
	for(i=0; ;i=i+1)
	{
		if(s[i]=='\0') break;  //检测完所有字符,自动终止循环
			n=n+1;
	}
		return n;
}
#include<stdio.h>
#include<string.h>
int main() 
{ 
    char s[100];
    gets(s);
    printf("%d\n",my_strlen(s) );
} 

#include<stdio.h>
#include<string.h>
int my_strlen(char s[])
{
	int n;
	n=strlen(s)-1;
	return n;
}
int main() 
{ 
    char s[100];
    gets(s);
    printf("%d\n",my_strlen(s) );
} 

gets()头文件记得加上。

2、问题B

自定义复制字符串的函数 my_strcpy

题目描述

编写自定义函数char *my_strcpy(char s1[],char s2[]); 将字符串s2复制到s1,返回值为s1

输入

一行字符串(长度不超过100)

输出

复制后的字符串内容

样例输入

abc
how are you?

样例输出

abc
how are you?

提示

main函数的定义如下:
#include<stdio.h>
#include<string.h>
int main()
{
char s[100] , t[100];
gets(s);
printf("%s\n",my_strcpy(t,s) );
}

char *my_strcpy(char s1[],char s2[])        //注意主函数my_strcpy(t,s)
{
	int i;
	for(i=0;  ;i=i+1)
	{
	    s1[i]=s2[i];             //对s1[]中每个元素逐一复制
		if(s2[i]=='\0') break;   //复制完所有字符,自动终止循环
	}
		return s1;
}
#include<stdio.h>
#include<string.h>
int main() 
{ 
    char s[100] , t[100];
    gets(s);                    
    printf("%s\n",my_strcpy(t,s) );
} 

#include<stdio.h>
#include<string.h>
char *my_strcpy(char s1[],char s2[])
{
	char *p=strcpy(s1,s2);
	return p;
}
int main() 
{ 
    char s[100] , t[100];
    gets(s);                    
    printf("%s\n",my_strcpy(t,s) );
} 

3、问题C

自定义字符串函数 ispalindrome

题目描述

编写自定义函数int ispalindrome(char s[]); 检测字符串s是否为回文
如果s为回文,函数返回值为1,否则返回值为0

输入

一行字符串(长度不超过100)

输出

yes or no

样例输入

abc
how are you?
rotator

样例输出

no
no
yes

提示

main函数的定义如下:
#include<stdio.h>
int main()
{
char s[100];
gets(s);
if ( ispalindrome(s) ) printf(“yes”);
else printf(“no”);
}

#include<string.h>
int ispalindrome(char s[])       
{
	int i , j , k=0;
	for(i=0 , j=strlen(s)-2 ; i<j ; i++ , j--)  //比较形象见下文具体分析
	{
	   if( s[i] != s[j] )  //在字符串左右对照中遇到不相同情况
	   {
	    k=k+1;              //使k不同于初值,即可终止循环
	    break;
	   }
	}
	if(k==0) return 1;      //注意if()语句中等于是==
	else return 0;	
}
#include<stdio.h> 
int main() 
{ 
    char s[100]; 
    gets(s); 
    if ( ispalindrome(s) ) printf("yes"); 
    else printf("no"); 
} 

代码暂时还有问题,但判断回文序列的方法非常巧妙。
i——>> 字符串左右都有循环<<——j
a a a a a b b b d d b b b a a a a a

4、问题D

在英语中,A、E、I、O、U是元音字母(汉语拼音方案借用了英语的元音字母,以元音字母开始的音节叫做韵母)
要求:统计一段短文中元音字母的个数

题目描述

编写自定义函数int ispalindrome(char s[]); 检测字符串s是否为回文
如果s为回文,函数返回值为1,否则返回值为0

输入

一段文字,包括各种字母、数字、标点等符号,以\n结尾,总长度不超过100

输出

元音字母的个数

样例输入

one apple a day keep the doctor away.
On July 20, 1969, Neil Armstrong became the first human to step on the moon.

样例输出

13
19

提示

string.h中声明的库函数 strchr(char *s , char c)可用于检测字符c是否在字符串s中出现

第一次复盘的错误版本


太扯淡了

#include <stdio.h>
int main() 
{
   char m,s[520];
   int i,k=0;
   for(i=0 ; i<strlen(s) ; i++)
   {
       if(m=='A'||m=='E'||m=='I'||m=='O'||m=='U'||m=='a'||m=='e'||m=='i'||m=='o'||m=='u')
       {
        k=k+1;
       }
   }
   return k;
}

正解

#include <stdio.h>
int main() 
{
   char s[520];         //定义字符串
   gets(s);             //输入字符串
   int i,k=0;
   for(i=0 ; i<strlen(s) ; i++)  //遍历数组中每个元素如果碰到目标元素就增值
   {
       if( s[i] == 'A' || s[i]=='E'|| s[i]=='I'|| s[i]=='O'|| s[i]=='U'|| s[i]=='a'|| s[i]=='e'|| s[i]=='i'|| s[i]=='o'|| s[i]=='u')
       {
        k=k+1;              //不要犯混,s[i]代表元素
       }
   }
   printf("%d",k);
}

更好的方法求大佬赐教/(ㄒoㄒ)/~~

5、问题E

统计子串出现的次数

题目描述

对于两个字符串S1和S2,如果S1中含有S2的内容,则S2为S1的子串。
例如:字符串"league of legend"包含了"end",所以"end"是"league of legend"的子串。
同理,"leg"也是"league of legend"的子串。
但"lend"不是"league of legend"的子串,虽然字母l、e、n、d均出现在"league of legend"中。

要求:输入两个字符串S1和S2,S1和S2的长度大于0且小于100,统计S2在S1中出现的次数。

输入

两个字符串S1和S2,每个字符串占一行

输出

S2在S1中出现的次数

样例输入

main(){char *s=“main(){char *s=%c%s%c;printf(s,34,s,34);}”;printf(s,34,s,34);}
34
样例输出
4

提示

string.h中声明的库函数 strchr(char *s , char c)可用于检测字符c是否在字符串s中出现

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值