编写一个函数实现两个字符串的连接(不使用库函数strcat).这个用C语言怎么写程序啊求详细解释~急! C语言编写一个程序,将两个字符串连接起来,不要使用strca...
\u7528c\u8bed\u8a00\u7f16\u5199\u7a0b\u5e8f,\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765,\u4e0d\u8981\u7528strcat\u51fd\u6570#include #include void strc(char c1[],char c2[]);void main(){char s1[30]="abc";char s2[30]="def";strc(s1,s2); //\u8bf7\u5728\u540e\u9762\u8865\u5145strc\u51fd\u6570\u7684\u529f\u80fd\uff0c\u5b8c\u6210\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u8fde\u63a5puts(s1);}void strc(char c1[],char c2[]){ //\u8bf7\u586b\u7a7a\uff0c\u5b8c\u6210\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u8fde\u63a5 int i,j; for(i = 0; c1[i]; i ++); for(j = 0; c2[j]; j ++) c1[i+j] = c2[j]; c1[i+j] = 0;}
#include "stdio.h"void main(){ char a[50],b[50],c[50]; int i=0,j=0,k=0;printf("\u8f93\u5165\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32");gets(a);printf("\u8f93\u5165\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32");gets(b); printf("a=%s\n",a); printf("a=%s\n",b); while(a[i])c[k++]=a[i++]; while(b[j])c[k++]=b[j++]; c[k]=b[j]; printf("c=%s",c);}\u5df2\u7ecf\u8fd0\u884c\u8fc7\uff0c\u6709\u4ec0\u4e48\u95ee\u9898\u8bf7\u7559\u8a00\uff01
void fun (char s1[],char s2[]){
int i,j;
for (i=0;s1[i] !=’\0’; i++); /*求出的i为pA字符的总长度,包括结束标记位*/
for (j=0;s2[j] !=’\0’; j++)
s1[i++]=s2[j]; /*将pB字符串连在pA字符串的后面*/
s1[i]='\0’; /*在字符串最后加上结束标记符*/
}
扩展资料:
最常用的字符串函数:
字符串输出函数puts格式:puts (字符数组名) 功能:把字符数组中的字符串输出到显示器。
2.字符串输入函数gets格式:gets (字符数组名) 功能:从标准输入设备键盘上输入一个字符串。本函数得到一个函数值,即为该字符数组的首地址。
3.字符串连接函数strcat格式:strcat (字符数组名1,字符数组名2) 功能:把字符数组2中的字符串连接到字符数组1 中字符串的后面,并删去字符串1后的串标志“”。
4.字符串拷贝函数strcpy格式:strcpy (字符数组名1,字符数组名2) 功能:把字符数组2中的字符串拷贝到字符数组1中。串结束标志“”也一同拷贝。
5.字符串比较函数strcmp格式:strcmp(字符数组名1,字符数组名2) 功能:按照ASCII码顺序比较两个数组中的字符串,并由函数返回值返回比较结果。
6.测字符串长度函数strlen格式:strlen(字符数组名) 功能:测字符串的实际长度(不含字符串结束标志‘’) 并作为函数返回值。
参考资料:
百度百科-数组
#include"stdio.h"
#include"conio.h"
#include"math.h"
main()
{char c,str1[100],str2[100],str3[300];
int i,j;
printf("Please input the first string
Confirm by Enter
");
gets(str1);
printf("Please input the second string
Confirm by Enter
");
gets(str2);
for(i=0;(c=str1<i>)!='\0';i++)
{str3<i>=str1<i>;
};
j=i;
for(i=0;(c=str1[i])!='\0';i++)
for(i=0;(c=str2<i>)!='\0';i++,j++)
{str3[j]=str2<i>;
};
printf("The string which has been connected is:
%s",str3);
getch();
}
扩展资料:
include用法:
#include命令预处理命令的一种,预处理命令可以将别的源代码内容插入到所指定的位置;可以标识出只有在特定条件下才会被编译的某一段程序代码;可以定义类似标识符功能的宏,在编译时,预处理器会用别的文本取代该宏。
插入头文件的内容
#include命令告诉预处理器将指定头文件的内容插入到预处理器命令的相应位置。有两种方式可以指定插入头文件:
1、#include<文件名>
2、#include"文件名"
如果需要包含标准库头文件或者实现版本所提供的头文件,应该使用第一种格式。如下例所示:
#include<math.h>//一些数学函数的原型,以及相关的类型和宏
如果需要包含针对程序所开发的源文件,则应该使用第二种格式。
采用#include命令所插入的文件,通常文件扩展名是.h,文件包括函数原型、宏定义和类型定义。只要使用#include命令,这些定义就可被任何源文件使用。如下例所示:
#include"myproject.h"//用在当前项目中的函数原型、类型定义和宏
你可以在#include命令中使用宏。如果使用宏,该宏的取代结果必须确保生成正确的#include命令。例1展示了这样的#include命令。
【例1】在#include命令中的宏
#ifdef _DEBUG_
#define MY_HEADER"myProject_dbg.h"
#else
#define MY_HEADER"myProject.h"
#endif
#include MY_HEADER
当上述程序代码进入预处理时,如果_DEBUG_宏已被定义,那么预处理器会插入myProject_dbg.h的内容;如果还没定义,则插入myProject.h的内容。
参考资料:
百度百科——include
编写一个函数实现两个字符串的连接(不使用库函数strcat).这个用C语言怎么写程序啊求详细解释,你这个应该是一个电脑应用的程序吧,不过具体的程序从你这个字面意思也解释不了,所以这个问题帮你解答不了,希望你了解。
#include <stdio.h>
void main()
{
char s1[20],s2[10];
int i=0,j=0;
gets (s1),gets (s2);
while (s1[i] !='\0')
{
i++;
}
while (s2[j] != '\0')
{s1[i++]=s2[j++];}
puts (s1);
}
strcat源码
char * strcat(char * dest, const char * src)
{
char *tmp = dest;
while (*dest)
dest++;
while ((*dest++ = *src++) != '\0')
;
return tmp;
}
参考一下吧。
绛旓細j=0;for(;*(a+i);i++)*(c+i)=*(a+i);for(;*(b+j);i++,j++)*(c+i)=*(b+j);*(c+i)=*(b+j);return 0;}int lenstr(char *a){int i=0;for(;*(a+i);i++);return i;}void main(){ char str1[]="Every one is ",str2[]="leaning C.",str12[...
绛旓細include<stdio.h> void lianjie(char *s1,char *s2){while(*s1)s1++;while(*s1++=*s2++);} int main(){ char s1[200],s2[200];gets(s1);gets(s2);lianjie(s1,s2);puts(s1);return 0;}
绛旓細1銆佹柊寤轰竴涓伐绋嬪拰.c鏂囦欢锛岃緭鍏ュご鏂囦欢鍜屼富鍑芥暟銆2銆佸畾涔夊彉閲忕被鍨嬨3銆佽皟鐢╟py鍑芥暟銆4銆佸畾涔涓涓嚱鏁锛屽苟瀹氫箟鍙橀噺绫诲瀷銆5銆佺敤涓涓狥or 璇彞鍜宨f璇彞鍒ゆ柇鏄惁涓哄厓闊炽6銆佹渶鍚庡姞涓涓瓧绗︿覆缁撴潫绗︼紝骞跺湪涓诲嚱鏁颁腑杈撳嚭銆7銆佺紪璇戙傝繍琛屻
绛旓細include"stdio.h"include"string.h"char *fun(char *str1,char *str2){ char *str=str1;while(*str!='\0')str++;while(*str2!='\0')str++=*str2++;str='\0';return(str);} main(){ char str1[50],str2[50];char *str;printf("浠庨敭鐩樿緭鍏涓や釜瀛楃涓锛灏瀹冧滑鍚堟垚涓涓鏂板瓧绗...
绛旓細void strcat(char *p1,char *p2){ while(*p1++);p1--;while(*p1++=*p2++);}
绛旓細1銆佸疄闄呬笂灏辨槸瀹炵幇strcat杩欎釜瀛楃涓搴鍑芥暟锛屽湪vc鑷甫鐨刢rt婧愮爜鎴栬卨inux骞冲彴鐨刧libc搴撲腑閮芥湁strcat鐨勬簮鐮侊紝鑷繁鍙互鏌ラ槄鍙傝冿紝鐪嬬湅搴撳紑鍙戣呮槸濡備綍鍐浠g爜鐨勶紝瀵逛簬瀛︿範C璇█闈炲父鏈夌敤銆2銆佺ず渚 include <stdio.h> char *strcat(char *str1, char *str2){ if((str1==NULL)||(str2==NULL)) throw "...
绛旓細include <string.h> include <stdio.h> main(){ char str1[80],str2[80];int i=0,j=0;puts("puts two string:");gets(str1);gets(str2);while(str1[i]!='\0')i++;while(str2[j]!='\0')str1[i++]=str2[j++];printf ("result is:%s\n",str1);} ...
绛旓細fun鍑芥暟缂栧啓鏈変簺闂锛屽仛濡備笅淇敼锛屼慨鏀硅鏄庡湪浠g爜涓粰鍑猴細void fun(char p1[], char p2[]){ int i=0,M=0,N=0; while(p1[i++]) { M++; } i=0;//閲嶆柊鍒濆鍖杋涓0锛岀粺璁2闀垮害 while(p2[i++]) { N++; } for(i=0;i<N;i++)//鎶妏2鎷兼帴...
绛旓細include <stdio.h>Join(char *a, char *b){int i=0,j=0;while(a[i]!='\0')i++;while(b[j]!='\0'){a[i++]=b[j++];}}main(){char a[100],b[100];//娉ㄦ剰:涓や釜瀛楃涓闀垮害鍊煎拰涓嶈瓒呰繃100 scanf("%s",a);scanf("%s",b);Join(a,b);printf("%s",a);} ...
绛旓細锛 娉ㄦ剰瑕佺暀1浣嶄繚瀛樼粨鏉熺'\0' strcpy(str1,"abcd");strcpy(str2,"efgh"); printf("鍘瀛楃涓鍒嗗埆涓猴細%s %s\n\n",str1,str2); strcat(str1,str2); printf("鐢╯trcat鎷兼帴鍚庡瓧绗︿覆锛%s\n\n",str1); strcpy(str1,"abcd");strcpy(str2,"efgh"); p=&str1[...