多个字符从两端移动向中间汇聚

问题描述:写一段程序,就是一串字符,如:welcome to bit!!!!!!.然后由初始符###################开始变,变成w###############!,然后再是we###########!!,最后变成welcome to bit!!!!!!!;

问题分析:

首先是定义字符串

	char str[] = "welcome to bit!!!!!!";//{'w','e','l','c','o','m','e',' ','t','o',' ','b','i','t','!','!','!' ,'!' ,'!' ,'!' ,0};
	int a = 0;
	char str1[] = "####################";//{'#','#','#','#','#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,0};
	//scanf("%s",&str);
	int shuru = 0;

算出字符串str的长度

a = strlen(str) ;

然后利用一个循环,让str1中的字符从第一个和最后一个开始等于str中的相应字符

for (int i = 0; i <= a/2; i++)//循环
	{
		printf("%s\n", str1);//先打印一下初始符号#
		str1[i] = str[i];//str1的第一个字符等于str的第一个字符然后依次向后
		str1[19 - i] = str[19 - i];//str1的最后一个字符等于str的最后一个字符,然后依次向前
		//scanf("%d", &shuru);
		Sleep(1000);//每过一秒中执行一次
		system("cls");//执行系统命令-cls:清空屏幕,使显示效果更好
	}

附全代码:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
//#include <system.h>
int main()
{
	//welcome come to bit
	char str[] = "welcome to bit!!!!!!";//{'w','e','l','c','o','m','e',' ','t','o',' ','b','i','t','!','!','!' ,'!' ,'!' ,'!' ,0};
	int a = 0;
	char str1[] = "####################";//{'#','#','#','#','#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,'#' ,0};
	//scanf("%s",&str);
	int shuru = 0;
	a = strlen(str) ;
	//printf("%s\n", str);
	for (int i = 0; i <= a/2; i++)
	{
		printf("%s\n", str1);
		str1[i] = str[i];
		str1[19 - i] = str[19 - i];
		//scanf("%d", &shuru);
		Sleep(1000);
		system("cls");//执行系统命令-cls:清空屏幕
	}
	printf("%s\n", str);
	return 0;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值