0 like 0 dislike
3.3k views

In the following program, we want to stores two strings in two arrays, swaps them, and displays their new content. There some error. Please fix it and explain why and how in the comment after your answer.

#include <stdio.h>
int main(){
    char temp[100];
    char str1[100] = "Let see";
    char str2[100] = "Is everything OK?";
    temp = str1;
    str1 = str2;
    str2 = temp;
    printf("%s %s", str1, str2);
    return 0;
}

 

[Exercise] Coding (C) - asked in Chapter 11: Pointers
ID: 38255 - Available when: Unlimited - Due to: Unlimited
1 flag | 3.3k views

30 Answers

0 like 0 dislike
Hidden content!
#include ** ** * *
int main(){
* * * *** * * * * **** * *temp;
* **** * * *** *** **** *** * * * *str1 = ** ** * * ** * ******
* * ** * ** * *** * ** *str2 = * * * everything **
* ** *** * ** * * ** ** = str1;
* ** ** ***** * * **** = str2;
*** * ** * ** * * = temp;
* *** * * ** * * ** **** **** * ** *** ** ** str1, str2);
*** * ** * **** ****** * 0;
}
answered by (-258 points)
0 0
Case 0: Correct output
0 like 0 dislike
Hidden content!
* * ** * ** * ** *
int **
* * * * * **** *** **** **** * **
******* ** ***** **** ** * * *** *** * * * *
* * ** *** **** * * *** * * ** ***
* ** * *** * * * ** ** ***** is * to ** **** char[] need to be ***** string or ** * ** *
**** *** ** ** *** ** ** * * * *** * * assign to *** * *
* *** * *** * *** * ****** * is for each char * ** a ** **** is an array of ***
* *** * ** **** * ** an array has 6 ***** * the the value of str1 is the *** * of the first *
* * * * * * * * * * = str1;
* * ** ** *** = str2;
* *** * ** ** *** *** ** = temp;
**** * * * * ** *** * ** *** * ** **** str1, *
* * ** * * * ** * * * * ** 0;
}
answered by (-108 points)
0 0
Case 0: Correct output
0 like 0 dislike
Hidden content!
**** ** ***
int * *
* * * * **** ** * ** * * ****
** ** * * * * * **** * * *** * * ** * **** *
** * *** * * * * * * * * ** * ** ** ** * * ***
* ***** * ** ** * ***** **** is * to * * char[] need to be * * string or * ** * *
** * **** * * ** *** * ** * assign to ***
** ** ** * * * ** ** *** is *** for each char * * a * * * is an array of * * *
* * * * ** * **** **** * ** ** an array has 6 * * * * the * * the value of str1 is the ** of the first * *
* * * * * *** * ***** = str1;
* * * * * ** **** * ** * * = str2;
* * **** * * *** = temp;
** *** * * * ** *** *** * ** * ** str1, ** *
** * *** ** * * ** ** 0;
}
answered by (-281 points)
0 0
Case 0: Correct output
0 like 0 dislike
Hidden content!
#include * * * *** *
int main(){
* * *** * * **** * **** * * *temp;
*** ** **** * * ** *str1 = * *** ** ***
* * * * * * * ** * * *str2 = everything * ****
** * * *** * ** * = str1;
** ** ***** ** *** ***** = str2;
* * *** * ** * * * **** *** *** = temp;
* ** * * ****** * * *** * * str1, str2);
* * * **** * * * ** 0;
}
answered by (54 points)
0 0
Case 0: Correct output
0 0
因為arry不能改變位置
只有pointer 可以
所以把全部都改成指標就可以了
0 like 0 dislike
Hidden content!
#include ** * ** * *
int main(){
** * * ** * ** ** * * ** *temp;
* * ******* * * * * *str1 = * ** ** * **
*** * * **** * * ** ** *str2 = ** everything ****
*** ** ** ** **** ***** * * = str1;
* ***** * * *** * **** ** = str2;
**** ** * ** * * * * = temp;
* * * * * ** * * * * * * * ******* str1, str2);
******* * *** * * ***** 0;
}
answered by (-498 points)
0 0
Case 0: Correct output
0 0
Explain:
  First, the type of "temp=str1" is pointer but  the head of main is array, so it can't work.
But if we use the array to solve this problem, we may have to use the for root to exchange every characters.
So we use the pointer, it's easy to use the pointer to exchange the string
So we set the array to pointer to solve the problem.
0 like 0 dislike
Hidden content!
#include * * * ** *
int main(){
* * **** * * ** ** ** *temp;
** **** * * ** * *str1 = * * *****
*** ***** ** *** ** *str2 = * * everything ** ** * *
** * * * * * ** * = str1;
* * ***** * * * = str2;
** *** * ** ** * * *** * = temp;
* ** **** *** ** * ** *** * * ** str1, str2);
** * ****** * **** ** * 0;
}
answered by (-284 points)
0 0
Case 0: Correct output
0 like 0 dislike
Hidden content!
* ** * *** ** * **
int *
* **** * * *** * * * *** * ****
** ** ** ** * ** * * **** *str1 = ** **** *
* * * ** *** * * *str2 = * ** ** * ***
* **** *** ** *** *** * = ***
* ** ** ** ***** ** * = str2;
* ** *** *** *** * * * ** =
*** * * * ** **** * * * * *** * ** * * *** *
***** **** * ** * ** ** * 0;
}

// The * * was in the **
//the ** can only ** one ** * * * and ** *
//but array is a * * of *** ** (or and etc)
answered by (-323 points)
0 0
Case 0: Correct output
0 like 0 dislike
Hidden content!
#include * * ** * * ****
int main(){
* * ***** * *** *** ** ** *temp;
* **** *** ** ****** * *str1 = ** ** * ******
* ** *** * * ** ** * * * ** *str2 = * *** everything * ****
** ** * * * **** * ** = str1;
* ** * * * * * *** = str2;
* * * **** * ** * * * = temp;
* **** * *** ** ** *** * str1, str2);
* ** * * * ** * * *** 0;
}
answered by (-229 points)
edited by
0 0
prog.c: In function 'main':
prog.c:6:5: error: 'temp' undeclared (first use in this function)
     temp = str1;
     ^~~~
prog.c:6:5: note: each undeclared identifier is reported only once for each function it appears in
prog.c:6:12: error: 'str1' undeclared (first use in this function)
     temp = str1;
            ^~~~
prog.c:7:12: error: 'str2' undeclared (first use in this function)
     str1 = str2;
            ^~~~
0 0
Case 0: Correct output
0 like 0 dislike
Hidden content!
#include *** * * **
int main(){
* *** *** * *** *** ** ** ** *
*** ***** * ** * * * str1[100] = ** ** * * * * *
* * ***** * * * str2[100] = ***** ** ** **

** * ** ** ** * *** ** * ** ** * str2, str1);
* * * ***** **** 0;
}
answered by (-329 points)
edited by
0 0
Case 0: Correct output
0 0
Case 0: Wrong output
0 0
Case 0: Correct output
0 like 0 dislike
Hidden content!
#include ** **** * * ******
int main(){
** ** *** * * * * *** * *temp;
* ** * * * * * * *str1 = *** * ***** *
**** * ** ** *** ** * * * *str2 = * everything ** ** ***
* * * *** ** * *** * * = str1;
** * * ********** * * = str2;
***** **** * ** ***** = temp;
* *** ** ** ******** *** ** str1, str2);
** *** * * **** ** ** 0;
}
answered by (-249 points)
0 0
Case 0: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.71.254.126
©2016-2024

Related questions

1 like 0 dislike
75 answers
[Exercise] Coding (C) - asked Oct 12, 2017 in Chapter 3: Formatted Input/Output by thopd (12.1k points)
ID: 26464 - Available when: Unlimited - Due to: Unlimited
| 5.7k views
3 like 0 dislike
87 answers
[Exercise] Coding (C) - asked Oct 12, 2017 in Chapter 3: Formatted Input/Output by thopd (12.1k points)
ID: 26463 - Available when: Unlimited - Due to: Unlimited
| 6.4k views
12,783 questions
183,443 answers
172,219 comments
4,824 users