一道C程序设计题关于字符串连接
字符串连接 编一程序,将两个字符串连接起来。结果取代第一个字符串。 要求 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;
参与评论- 相关内容
- 最近更新
- ·我要C++编译器的下载地址~~~
- ·C#语言中的,继承里的get,set访问..
- ·请问C/C++有那些存储区,各自功能..
- ·T-flash卡的问题
- ·又是诺基亚QD啊!
- ·K750有什么优.缺点?
- ·求F-Secure Mobile Anti-Virus S..
- ·3250怎么这样啊??
- ·哪种手机能接收股市的行情?如何收..
- ·摩托A1200内存卡锁住了怎么办啊?
添加到百度搜藏