注册 登录
编程论坛 C语言论坛

字符串输入回车是怎么样的

蒟蒻 发布于 2019-11-15 20:58, 3012 次点击
输入两个已经按从小到大顺序排列好的字符串,编写一个合并两个字符串的函数,使合并后的字符串,仍然是从小到 大排列。

输入:
            两个已经排好顺序(升序)的两个字符串

输出:
            一个合并在一起的有序(升序)的字符串
要求:
示例:123456
    45789
输出:1236


我的代码
    #include <stdio.h>  
    #include <stdlib.h>  
    #include <string.h>  
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */  
      
    int main(int argc, char *argv[]) {  
        char a[100],b[100],h[100];  
        int i1,i2,i3;  
        gets(a);  
        gets(b);  
    // puts(a);  
    //  puts(b);  
        i1=0;  
        i2=0;  
        i3=0;  
        int s1,s2,k1;  
        s1=strlen(a);  
        s2=strlen(b);  
    if(a[0]!='\n'&&b[0]!='\n'){  
         
    do{  
            if(a[i1]<=b[i2]){  
                h[i3]=a[i1];  
                i3++;  
                i1++;  
            }  
            else{  
                h[i3]=b[i2];  
                i3++;  
                i2++;  
            }  
        } while(a[i1]!='\0'&&b[i2]!='\0');  
         
         
        if(i1==s1&&i2==s2){  
            for(k1=0;k1<i3;k1++){  
                printf("%c",h[k1]);  
            }  
            printf("\n");  
        }  
        if(i1==s1&&i2!=s2){  
            for(k1=0;k1<i3;k1++){  
                printf("%c",h[k1]);  
            }  
            for(i2=i2;i2<s2;i2++){  
                printf("%c",b[i2]);  
            }  
            printf("\n");  
        }  
        if(i2==s2&&i1!=s1){  
            for(k1=0;k1<i3;k1++){  
                printf("%c",h[k1]);  
            }  
            for(i1=i1;i1<s1;i1++){  
                printf("%c",a[i1]);  
            }  
            printf("\n");  
        }  
    }  
    else if(a[0]=='\n'){  
        for(i1=0;i1<s2;i1++){  
            printf("%c",b[i1]);  
            printf("\n");  
        }  
    }else{  
        for(i1=0;i1<s1;i1++){  
            printf("%c",a[i1]);  
            printf("\n");  
        }  
    }  
            return 0;  
    }




但是有示例是
回车与一个数字

我的代码只是输出无结果
要求是输出那个数字
5 回复
#2
自学的数学2019-11-15 22:13
示例:123456
    45789
输出:1236
示例里面,两个输入的字符串第一个为123456,第二个为45789.你的合并字符串是不是将第一个字符串(123456)中包含了第二个字符串相同的字符(45)去掉后得到的字符就是123456减去45等于1236,不知是不是这么理解。
但是你程序代码输出的结果却是12344556789,请问:你到底需要的是那种合并规则后得到的字符串结果?
#3
forever742019-11-15 22:44
仅仅就标题问题而言,gets的一般作风是接收了输入的字符串以后把结尾的换行符\n替换为字符串结束符\0
所以gets得到的字符串原则上不应包含换行符
#4
蒟蒻2019-11-17 14:25
回复 2楼 自学的数学
我的示例写错了
应该是输入12345
      23467
  输出1223344567
#5
蒟蒻2019-11-17 14:28
回复 2楼 自学的数学
当输入为12345
     第二个数不输入,只是回车
  要求输出12345
但是我的程序不会输出12345
应该怎么改?

#6
蒟蒻2019-11-17 14:31
回复 3楼 forever74
我会改了

1