串结构练习——字符串连接

来源:互联网 发布:如何创建java项目 编辑:程序博客网 时间:2024/06/06 07:07

题目描述

 给定两个字符串string1和string2,将字符串string2连接在string1的后面,并将连接后的字符串输出。
连接后字符串长度不超过110。 

输入

 输入包含多组数据,每组测试数据包含两行,第一行代表string1,第二行代表string2。
 

输出

 对于每组输入数据,对应输出连接后的字符串,每组输出占一行。
 

示例输入

123654abssfg

示例输出

123654abssfg


网上搜了好久才找到的串操作模板,累觉不爱

源代码C++环境下可正常运行

#include <stdio.h>#include <stdlib.h>#include <string.h>#define Stringmax 100#define Stringsize 10char str1[1000],str2[1000];typedef char ET;typedef struct{    ET *ch;    int length;    int stringsize;} String;/*初始化串*/char StrInti(String &S){    S.ch = (ET *)malloc(Stringmax*sizeof(ET));    if(!S.ch)exit(-1);    S.length = 0;    S.stringsize=Stringmax;    return 1;}/*串赋值*/int Strstar(String &S,char str[]){    int i;//    if(S.length>=S.stringsize)//    {//        S.ch=(ET *)realloc(S.ch,(S.stringsize+Stringsize)*sizeof(ET));//        if(!S.ch)exit(-1);//        S.stringsize+=Stringsize;//    }    for(i=0; str[i]!='\0'; i++)    {        S.ch[i]=str[i];    }    S.ch[i]='\0';    S.length=i;    return 1;}/*链接串*/char Concat(String &T,String &S1,String &S2){    T.length=S1.length+S2.length;//    T.ch=(ET *)realloc(T.ch,(S1.length+S2.length)*sizeof(ET));    int j=0,k=0;    while(S1.ch[j]!='\0')T.ch[k++]=S1.ch[j++];    j=0;    while(S2.ch[j]!='\0')T.ch[k++]=S2.ch[j++];    T.ch[k]='\0';    return 1;}/*输出串*/void Stroutput(String &S){    int i=0;    while(i<S.length)    {        printf("%c",S.ch[i]);        i++;    }}/*清空串*/void Strclear(String &S){    if(S.ch)    {        delete S.ch;        S.ch=NULL;    }    S.length=0;}int main(){    String S1,S2,T;    while(~scanf("%s",str1))    {        StrInti(S1);        StrInti(S2);        StrInti(T);        Strstar(S1,str1);        scanf("%s",str2);        Strstar(S2,str2);        Concat(T,S1,S2);        Stroutput(T);        printf("\n");    }}



0 0
原创粉丝点击