注册 登录
编程论坛 C++教室

想了很久还是不会,请学长帮忙一下看错在哪里?^_^

身不由己 发布于 2011-05-14 21:54, 543 次点击
以下程序完成将S2的字符串连接到S1中去,即将两个字符串合并的功能。

void main()

{char s1[80],s2[40];

 int i=0,j=0;

 printf("\nInput string1:");

 scanf("%s",s1);

 printf("\nInput string2:");

 scanf("%s",s2);

 while(s1[i]!='\0')

       s1=s1      ;

 while(s2[j]!='\0')

      s2=s2               ;

         s1=s1+s2        ;

 printf("The new string is:%s",s1);}


[ 本帖最后由 身不由己 于 2011-5-14 22:04 编辑 ]
7 回复
#2
寒风中的细雨2011-05-14 22:02
程序代码:
#include <iostream>
#include <string>

using namespace std;

int wmain(void)
{
    string s1;
    string s2;

    cout << "\nInput string1:";
    cin >> s1;

    cout << "\nInput string2:";
    cin >> s2;

    s1.append(s2);

    cout << "The new string is: " << s1 << endl;


    return 0;
}
#3
寒风中的细雨2011-05-14 22:03
程序代码:
#include <stdio.h>

int main(void)
{
    char s1[80],s2[40];
    int i=0,j=0;

    printf("\nInput string1:");
    scanf("%s",s1);

    printf("\nInput string2:");
    scanf("%s",s2);

    while(s1[i]!='\0')
    {
        ++i;
    }

    while(s2[j]!='\0')
    {
        s1[i++] = s2[j++];
    }
    s1[i] = 0;

    printf("The new string is:%s\n",s1);

    return 0;

}
#4
身不由己2011-05-14 22:07
谢谢斑竹
#5
寒风中的细雨2011-05-14 22:15
没事~~
#6
top_dada2011-05-14 22:49
最后那不是s[i+1] = 0;???
#7
心爱的远方2011-05-14 23:28
为什么非要用 int main(void)  最后来个 return o
#8
donggegege2011-05-15 12:17
这个用数据结构也能写
1