您的位置 电脑知识爱好者 >> 编程知识 >> 一道C程序设计题关于字符串连接

一道C程序设计题关于字符串连接

电脑知识爱好者互联网本站整理2007-8-26 14:06:14
知识重点:字符串连接 编一程序,将两个字符串连接起来。结果取代第一个字符串。 要求 1、用字符数组,不用strcat函数; 2、用C标准中的strcat函数; 1、采用字符数组的方法。算法:定义的两个字符数组,第..

字符串连接 编一程序,将两个字符串连接起来。结果取代第一个字符串。 要求 1、用字符数组,不用strcat函数; 2、用C标准中的strcat函数; [实验提示] 1、采用字符数组的方法。算法:定义的两个字符数组,第一个数组的大小应大于两个字符数组的有效大小。在串接时要注意字符处结束标志:如下图所示: Str1[20] ILOVE\0 [实验提示] 1、采用字符数组的方法。算法:定义的两个字符数组,第一个数组的大小应大于两个字符数组的有效大小。在串接时要注意字符处结束标志:如下图所示: Str1[20] ILOVE\0 Str2[10] C++\0 串接后:str1[20] ILOVEC+ +\0 串接过程: 1)首先找到第一个字符串的结束标志即末尾str1[5]。 2)执行str1[5+i] = str[i] 直到str[i] != ‘\0’ 3)给字符串str1末尾添加‘\0’ 2、函数原形 strcat(char[],const char[])使用时实参只需给出数组名,如 strcat(str1,str2)使用时要确保str1数组不会溢出。 有输入的吗?

======================

#include<stdio.h>

#include<string.h>

#define SIZE1 20

#define SIZE2 10

void main()

{

char Str1[SIZE1],Str2[SIZE2];

int Len1,Len2;

printf("Please Input String1(Length<%d):",SIZE1);

gets(Str1);

Len1=0;

do

{

if(Str1[Len1]=='\0')break;

Len1++;

}

while(Len1<SIZE1);

if(Len1==SIZE1)

{

printf("Error: String1 Overflow.");

return;

}

printf("please input String2(length<%d):",SIZE2);

gets(Str2);

Len2=0;

do

{

if(Str2[Len2]=='\0')break;

Len2++;

}

while(Len2<SIZE2);

if(Len2==SIZE2)

{

printf("Error: String2 Overflow.");

return;

}

if(Len1+Len2>=SIZE1)

{

printf("Error: New String1 Overflow.");

return;

参与评论
相关内容
关于我们 | 隐私政策 | 站点地图 | 站长博客|京ICP备07025396号
添加到百度搜藏 添加到百度搜藏 电脑知识爱好者Copyright ?2006-2008版权所有 我要啦免费统计