实验八 字符数组以及串

1121 定义存贮字符串的数组

Description
在下面程序中填充定义字符数组的语句,使程序完整。

#include “stdio.h”
#include “string.h”
int main()
{ _______________________/define a array named s to store string/
strcpy(s, “abcdefghijklmn”);
printf(“%s”, s);
return 0;
}

分析:直接定义数组

#include "stdio.h"
#include "string.h"
int main()
{  char s[10005];/*define a array named s to store string*/
    strcpy(s, "abcdefghijklmn");
    printf("%s", s);
    return 0;
}

1122 字符串的合并

Description
从键盘输入3个字符串(每个字符串以回车符做为结束标志),将3个字符串以输入先后顺序合并到字符串s中,
请填空使用程序完整。


#include "stdio.h"
#include "string.h"
main()
{    
    char s[100]="";
    char a[30];
    _______________________               
    printf("%s", s);
}

输入样例
123
abc
456

输出样例
123abc456

分析:使用string库中的strcat函数可以实现字符的连接

#include "stdio.h"
#include "string.h"
main()
{
    char s[100]="";//将数组中的字符赋值为空:‘\0’
    char a[30];
    for(int i=0;i<3;i++)
        {
     	gets(a);
        strcat(s,a);//将a数组中的字符连接到s的字符后面;
        }
    printf("%s", s);
}

1123 字符串的输入与输出

Description
下面程序实现从键盘读入字符串,然后输出到屏幕,请填充必要的语句。

#include “stdio.h”
main()
{ char s[50];
printf(“What’s your name?\n”);
_______________________ /iput your name from the keyboard/
printf(“Your name is “);
printf(”_______________________”, s); /output your name/
}

输入样例
Wang

输出样例
What’s your name?
Your name is Wang

#include "stdio.h"
main()
{   char  s[50];
    printf("What's your name?\n");
    gets(s);/*iput your name from the keyboard*/
    printf("Your name is ");
    printf("%s", s);  /*output your name*/
}

1145 回文串

Description
读入一行字符串(不多于80个字符,以回车结束),判断该字符串是否为回文串(即从左向右拼写与从
右向左拼写是一样的),是则输出Y,不是则输出N。

输入格式
一行字符
输出格式
是则输出Y,不是则输出N
输入样例
abba
输出样例
Y

提示


input:
abcba
output:
Y

input:
abc
output:
N

分析:循环n/2次(n为数组字符长度),判断头尾是否相等

#include "stdio.h"
#include<string.h>
main()
{
   char s[85];
   int n,count=0,i;
    gets(s);//输入字符串
    n=strlen(s);//获取长度
 for(i=0;i<n/2;i++)
 {
		if(s[i]==s[n-1-i])//循环判断是否头尾相等
		{
			count++;
		}
}
	if(count==n/2)//此处无论n为奇偶都一样,由整型运算的特征决定,读者可以自己推理;
	{
		printf("Y\n");
	}
	else
	{
		printf("N\n");
	}
   return 0;
}

1050 寻找字符串

Description
由键盘输入两个字符串(假设第一个字符串必包含第二个字符串,如第一个字符串为ABCDEF,第二个为CDE,
则CDE包含在ABCDEF中),现要求编程输出第二字符串在第一行字符串中出现的位置。
(如果第二个字符串在第一个字符串中出现多次,则以最前出现的为准)

输入样例
ABCDEFG
DE

输出样例
4

提示
因为DE在ABCDEFG中的第4个字符处出现
分析:两个循环进行遍历


#include<stdio.h>
#include<string.h>
char a[105], b[55];
int main()
{
	
	int i,j, n,m;
	gets(a);
	gets(b);
	m = strlen(a);
	n = strlen(b);
	for (i = 0; i < m; i++)
	{
		for (j = 0; j < n; j++)
		{
			if (a[i + j] != b[j])
			/*外循环在数组a中遍历,内循环在b中遍历;检查当前数组a的字符是否等于b数组的字符*/
			{
				break;
			}
		}
		if (j == n) //如果内循环完全执行完毕,即j=n,说明完全匹配;
		{
			printf("%d", i + 1);//注意下标与位置数是不同的;
			break;
		}
	}
	return 0;
}


考试题 多少个Bubble

描述
读入一行字符串(不多于800个字符,以回车结束),统计其中Bubble出现了多少次

输入样例
Bubble if only Bubble.
输出样例
2

#include<stdio.h>
#include<string.h>
int main()
{
	char a[805];
	int i, n,count=0;
	gets(a);
	n = strlen(a);
	for (i = 0; i < n; i++)
	{
		if (a[i] == 'B' && a[i + 1] == 'u' && a[i + 2] == 'b' && a[i + 3] == 'b' && a[i + 4] == 'l' && a[i + 5] == 'e')
			count++;
	}
	printf("%d", count);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值