数据交换

题目源地址

Description

给你2个长度相同的字符串数组,请交换2个数组的内容。
本题要求必须使用函数传参来实现!

Input

输入数据有多组,每组第1行为n( 2<=n<=10),接下来输入2行,每行都为n个字符串(串的长度小于20);

Output

请输出交换后的2组串。

Sample Input

3 
crise tear tire 
base bind dipaloma

Sample Output

base bind dipaloma
crise tear tire
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdio.h>
 4 #include <string.h>
 5 using namespace std;
 6 //形式参数和实际参数名称可以相同,因为C++支持方法的重载。
 7 void shuru(char a[][20],char b[][20],int n)
 8 {
 9     int i;
10     for (i=0;i<n;i++)
11         scanf ("%s",a[i]);
12         for (i=0;i<n;i++)
13         scanf ("%s",b[i]);
14 }
15 //不用加什么吗?每碰到一个空格,数组就进入下一行了
16 void shuchu(char a[][20],char b[][20],int n)
17 {
18     int i;
19     for (i=0;i<n;i++)
20         printf("%s ",a[i]);
21  printf ("\n");
22         for (i=0;i<n;i++)
23         printf("%s ",b[i]);
24 }
25 void haha(**char a[][20],**char b[][20],int n)
26 {
27     char  temp[20];
28     for (int i=0;i<n;i++)
29     {
30         strcpy(temp,a[i]);
31          strcpy(a[i],b[i]);
32           strcpy(b[i],temp);
33     }
34 
35 }
36 
37 int main()
38 {
39     char a[10][20],b[10][20];
40     int n;
41     while (cin>>n)
42     {
43         shuru(a,b,n);
44         haha(a,b,n);
45         shuchu(a,b,n);
46 
47     }
48 
49       return 0;
50 
51 }

猜你喜欢

转载自www.cnblogs.com/twomeng/p/9476042.html