c语言:编写一个字符串连接程序,它的功能是把两个不知道 长度的字符串 s1、s2 的内容连接成为一个字符

作者&投稿:徭厚 (若有异议请与网页底部的电邮联系)
用C语言编写一个程序 求一个字符串S1的内容复制给另一个字符串S2~

#include

char * mstrcpy(char *des, char *src)
{
int i = 0;
while( *(src+i) != '\0')
{
*(des+i) = *(src+i);
i++;
}

*(des+i) = '\0';
}

int main(void)
{
char *str1="abcdefg12345";
char str2[80];

mstrcpy(str2, str1);

printf("%s
", str2);

return 0;
}

你这种写法逻辑上有错,即不能正确处理两字符串不等长的情况。应当这样写才符合要求:
void main(void){
char str1[80],str2[80];
int i=0,j=0,a=0,b=0;
gets(str1);
gets(str2);
while(str1[i]!='\0') a+=str1[i++];
while(str2[j]!='\0') b+=str2[j++];
printf("resu=%d",a-b);
}
.

#include<iostream>
#include<string>
using namespace std;
void main()
{string s1,s2,s;
cin>>s1>>s2;
s=s1+s2;
cout<<s<<endl;
}
本例是在Visual C++ 6.0上运行的其他标准C++软件运行的程序与上述程序略有不同,如:
#include<iostream>
#include<string>
using namespace std;
int main()
{string s1,s2,s;
cin>>s1>>s2;
s=s1+s2;
cout<<s<<endl;
return0;
}

#include<stdio.h>
char * fun(char *p,char *q)
{
if(p == NULL || q==NULL)return NULL;
int n=strlen(p);
int m=strlen(q);
int *str=new char[m+n+1];
assert(str != NULL);
strcpy(str,p);
char *temp=&str[n];
strcpy(temp,q);
str[m+n]='\0';
return str;
}

#include"iostream.h"
#include"string.h"
void main()
{
char s1[20],s2[20],s[50];
cin>>s1>>s2;
strcpy(s,s1);
strcat(s,s2);
cout<<s<<endl;
}

t

说具体点,是不知道字符串的有效字符长度还是什么


求如何编写一个C语言程序,输入一个字符,若是小写字母,转换为大写字母输 ...
char trans(char str){ if(str <= 'z' && str >='a'){ return str - ('a'-'A');} return str;}

用C语言编写一个程序(输入一段字符串,并设定一个字母,如果字符串中有这...
include "stdio.h"define MaxLen 256 main(){ char ch;char str[MaxLen];memset(str,0,sizeof(str));printf("输入字符串\\n");scanf("%s",str);printf("输入删除字符\\n");scanf("%c",&ch);int len = strlen(str);int cur = 0;for( int i=0;i<len;i++){ if( ch==str[i]...

入门c语言:从键盘输入一个字符,判断它是字母、数字或其它字符。
主要部分如下:char c;cin>>c;if(c>='0'||c<='9')cout<<"digital character";if(c>='a"||c<='z')cout<<"alphabetic character";else cout<<"other character";大致就这样吧

用C语言编写:输入一行字符以@作结束标志,分别统计其中英文字母、空格...
include <stdio.h> include <stdlib.h> void main(){ int w=0,k=0,n=0,z=0; \/\/w为字母个数,k为空格个数,n为数字个数,z为其他字符个数 char ch;scanf("%c",&ch);while(ch!='@'){ if ( ch>='A'&&ch<='z' )w++;else if ( ch==' ' ) \/\/这里你少写个‘=’...

C语言编程:从键盘中输入一个英文字符串
include<stdio.h> include<stdlib.h> int main(){ int strSize = 100;char *str = (char *)malloc(sizeof(char) * strSize);int charNum = 0;char input;\/\/逐个字符输入字符串,可以输入int可以表示的最大值个字符 printf("请输入任意个字符:\\n");while(true){ scanf("%c",&input);...

用c++语言编写一个程序:如何在字符串里查找第一个不重复的字母,即只出...
{ cout<< a << " 第一个不重复的字母: "<< a[i] << endl; break; } }} int main(){\/\/addbccadfeg字符串则输出b;\/\/addbccabfeg字符串则输出f;string a="addbccadfeg";string b="addbccabfeg";find(a);find(b);system("pause");return 0;} ...

用c++语言编写一个程序:如何在字符串里查找第一个不重...
include <iostream>using namespace std;void f(char* s){ int cnt,i,l; char* s1; l=strlen(s); for(i=0;i<l;i++) { s1=s; cnt=0; while(*s1) { if(*s1==s[i]) { cnt++; if (cnt>1) break; } s1++; } if(cnt==1) { cout...

用c语言编写:从键盘输入两个字符,要求输出这两个字符,并输出它们的ASCII...
1 用getchar输入两个字符。2 用%d的格式,同过printf输出ascii码值。3 计算平方和,并利用sqrt求平方根。4 输出平方根。代码:include <stdio.h>#include <math.h>int main(){ char a,b; int c; double r; a = getchar(); b = getchar(); printf("%d %d ", a,...

编写程序实现在一个字符串中查找指定的字符(请用c语言作答)_百度知 ...
include<stdio.h> int main(){ int i,index,count;char a,ch,str[80];scanf("%c\\n",&a);i=0;index=-1;count=0;ch=getchar();for(i=0;ch!='\\n';i++){ str=ch;count++;ch=getchar();} for(i=0;i<count;i++)if(a==str)index=i;if(index!=-1)printf("index=%d"...

编写c语言程序,从键盘输入一行字符,将第一个英文字符设为大写,其它英文...
include<stdio.h> int main(){ char c[100];int i=0;int a=0;while((c[i]=getchar())!='\\n'){ if(c[i]>='A'&&c[i]<='Z'){ c[i]=c[i]+32;} if(!a && c[i]>='a'&&c[i]<='z'){ c[i]=c[i]-32;a=1;} i++;} int j;for(j=0;j...

赣榆县13326884551: 用c语言编写一个将两个字符串连接起来函数两个字符串由主函数输入, 连接后的字符串也由主函数输出. -
赞蒋硫酸: #include<stdio.h> void main() {void con(char sting1[],char sting2[],char sting3[]);char s1[20],s2[20],s3[40]; printf("Input sting1: ");scanf("%s",s1);printf("Input sting2: ");scanf("%s",s2);con(s1,s2,s3);printf("%s\n",s3); } void con(...

赣榆县13326884551: 用c语言编程编写函数,实现把一个字符串连接到后面一个字符串.并编写程序,在程序中自定义一个字符串数组 -
赞蒋硫酸: #include <stdio.h> #include<conio.h> #include <string.h> char *my_strcat(char *s1,char *s2) { while(*(++s1)); while(*s1++=*s2++); return s1; } int main() { char s1[50]={"my name is Bob"},s2[50]={"2011119465"}; my_strcat(s1,s2); printf("%s",s1); getch(); }我写的一个字符连接函数,-物联网校企联盟技术部

赣榆县13326884551: 用C语言写出一函数,将两个字符串连接,该怎么写 -
赞蒋硫酸: void concat(char *s1, int size1, char *s2, int size2, char *sout) {int i;for (i = 0; i < size1; ++i)sout[i] = s1[i];for (i = 0; i < size2; ++i)sout[i + size1] = s2[i];sout[i] = '\0'; } 示例使用方法: char *s1 = "ABCD"; char *s2 = "12345"; char sout[100];...

赣榆县13326884551: C语言编程:字符串的连接 -
赞蒋硫酸: scanf("%s %s",a[100],b[100]);改为scanf("%s %s",a,b);strcat(char a[],const char b[]);改为strcat( a, b); 完整程序:#include<stdio.h>#include<string.h> main() { char a[100],b[100]; scanf("%s%s",a,b); strcat(a,b); printf("%s",a); }

赣榆县13326884551: C语言程序设计 字符串连接 -
赞蒋硫酸: #include int main(void) { unsigned int i,j; char soustr[80],desstr[80];//定义两个字符型数组,长度都为80gets(soustr);//读取第一行输入,即敲下回车键之前的输入,存到sourtr中gets(desstr);//读取另一行输入,并存到desstr数组中i = strlen(...

赣榆县13326884551: c语言编写一个程序:实现两个字符串的连接谢谢了,大神帮忙啊 -
赞蒋硫酸: #includevoid main() { char *p1,*p2,s1[100],s2[100]; printf("输入字源符zhidao串1:"); scanf("%s",s1); printf("输入字符串2:"); scanf("%s",s2); for(p1=s1;*p1;p1++); for(p2=s2;*p2;*p1++=*p2++); *p1='\0'; printf("连接后的字符串:%s",s1); }

赣榆县13326884551: C语言问题 编写一程序将两个字符串连起来 -
赞蒋硫酸: 一、用strcat函数: #include "stdio.h" #include "string.h" int main(int argc,char *argv[]){char a[100]="abcdefg",b[]="1234567";printf("%s\n",strcat(a,b));return 0; }二、不用strcat函数: #include "stdio.h" int main(int argc,char *...

赣榆县13326884551: C语言编写字符串连接.谢谢! -
赞蒋硫酸: int i=0,j; char ch; char str1[100],str2[100]; printf("请输入2个字符串:\n"); scanf("%s %s",str1,str2); printf("合并前str1:%s,str2:%s\n",str1,str2); strcat(str1,str2); while(str1[i]!='\0') { for(j=i;j<strlen(str1);j++) { if(str1[i]>str1[j]) { ch=str1[i]; str1[i]=str1[j]; str1[j]=ch; } } i++; } printf("合并后%s\n",str1);

赣榆县13326884551: C语言编程:5、编一个程序,将两个字符串连接起来,不要用strcat函数. -
赞蒋硫酸: 思路:字符串连接先需要找到第一字符串的结束位置,接着把第二字符串元素放到第一字符串后面,最后加上结束标志即可.参考代码:拼接123和456#include<stdio.h> void mystrcat(char a[],char b[]){//字符串连接函数int i=0,j=0; while(a[i++]!='\0...

赣榆县13326884551: 用C语言写一个函数,将两个字符串连接. -
赞蒋硫酸: 1、实际上就是实现抄strcat这个字符串库函数,在vc自带的crt源码或者linux平台的glibc库中都有strcat的源码,自己可以查阅参考,看看库开发者是如何zd写代码的,对于学习C语言非常有用. 2、示例 #include <stdio.h>char *strcat(char *str1, ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网