标题:把两个字符串同时放进空数组里
取消只看楼主
小马不过河
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2016-7-24
结帖率:100%
 问题点数:0 回复次数:2 
把两个字符串同时放进空数组里
#include <stdio.h>
int main(){
    char *s1="hello";
    char *s2="world";
    char s3[20];
    int i,j=0;
    for(i=0;i<20;i++){
        s3[i]=*(s1+i);
    if(s3[i]=='\0')
        s3[i+1]=*(s2+j++);}
        printf("%s\n",s3);

    return 0;
}
搜索更多相关主题的帖子: include 字符串 hello world 
2016-07-27 11:55
小马不过河
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2016-7-24
得分:0 
回复 楼主 小马不过河
#include <stdio.h>
int main(){
    char *s1="hello";
    char *s2="world";
    char s3[20];
    int i;
    for(i=0;i<20;i++){
        s3[i]=*(s1+i);//继续循环的话,s1一直加会越界;结果对,但是程序错误;
    if(s3[i]=='\0')
        s3[i]=*(s2++);}
        printf("%s\n",s3);

    return 0;
}
2016-07-27 15:24
小马不过河
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2016-7-24
得分:0 
回复 楼主 小马不过河
#include <stdio.h>
int main(){
    char *s1="hello";
    char *s2="world";
    char s3[20];
    char *p=s1,*q=s2,*r=s3;
    while((*r++=*p++)!='\0');
    r--;
    while((*r++=*q++)!='\0');
    printf("%s\n",s3);

    return 0;
}
#include <stdio.h>
#include <string.h>

int main(){
    char *s1="hello";
    char *s2="world";
    char s3[20];
    strcpy(s3,s1);
    strcat(s3,s2);
        printf("%s\n",s3);

    return 0;
}
#include <stdio.h>
int lens(char *p){
    int len=0;
    while(*p++!='\0')
        len++;
    return len;
}
int main(){
    char *s1="hello";
    char *s2="world";
    char s3[20];
    int len1=lens(s1);
    int len2=lens(s2);
    int i=0;
    for(i=0;i<len1;i++)
        s3[i]=*(s1+i);
    for(i=len1;i<len1+len2;i++)
        s3[i]=*(s2+i-len1);
    s3[len1+len2]='\0';
        printf("%s\n",s3);

    return 0;
}
2016-07-28 11:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-467278-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.809899 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved