0 like 0 dislike
3.4k 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.4k views

30 Answers

0 like 0 dislike
Hidden content!
#include * * *
int main(){
***** * ** * ** * * *temp;
****** * ** * * * * ** *str1 = ** * * * *** *
* * ***** **** * ** ** * *str2 = * ** everything *** * * *
* * ** * * * * *** **** = str1;
** * * * ** ** ***** = str2;
* * * *** ** *** *** = temp;
* * **** ** *** * * * ** * * str1, str2);
* ** * * **** **** 0;
}
answered by (-32 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 (-127 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 (-255 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 (-255 points)
0 0
Case 0: Correct output
0 0
If you declare as a char array, you must  use array in the code
0 like 0 dislike
Hidden content!
#include * ** * * * *
int main(){
* ** ** * ** ** ** ** *temp;
*** ** *** ***** **** ***** *str1 = ** * *** ** *
** * * ** * ** *str2 = * * * * everything ****** * *
****** * * * ** * * = str1;
* * * ***** * ** ** = str2;
********** ** * * * * *** = temp;
** * * * * **** ** ** ****** * ** str1, str2);
*** * **** ** ******* **** 0;
}
answered by (-301 points)
0 0
Case 0: Wrong output
0 0
-----------Re-judge-----------
Case 0: Correct output
0 0
We have to use point, instead of using array.
0 like 0 dislike
Hidden content!
#include * * ** **
int main(){
* ********** ** * * **** *temp;
** *** * * * * * ** * *str1 = * ***** **
**** ** **** * * * ***** * ** *str2 = *** ** everything * ** * *
**** * * * * * ** ** = str1;
* * *** * **** * ***** * = str2;
** *** **** * *** * *** *** ** * = temp;
* * * * **** ** * * * * * str1, str2);
* * *** ****** * * *** 0;
}
answered by (-196 points)
0 0
Case 0: Wrong output
0 0
-----------Re-judge-----------
Case 0: Correct output
0 like 0 dislike
Hidden content!
#include ** * * *
void *** *str1, char *str2)
** ** ** * *** ** * * * are attached to the swap function
{
* ** * * * ***** * ** ** *temp= str1;
* * *** ***** ***** * * *****
*** ** * * * * ** *
**** * ***** swap **** * a pointer *temp was declared * *** to *** * **
}



int main()
{
* * ** ***** *** * ** * ** ** * ****
*** ** **** *** *** * ******* *** * * * ** * *
* ** **** **** *** * ** *
** *** * **** * * i[7] = *** ** ********
* ** ** * **** char j[17] = * * **** * * ** *****
*** * *
** * * ***** ** * ******* and swap again in the main * **



***** *** ** * ** * * * * **
** ******** * *** ** * * * * * * ***
* ** ** * *** ** **** ** * *

** *** ** ***** * * ** **** ** *** * ** * str1);
** *** * * * * * * * * 0;
}
answered by (-167 points)
edited by
0 0
Case 0: Wrong output
0 0
-----------Re-judge-----------
Case 0: Correct output
0 0
Case 0: Correct 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 (-120 points)
edited by
0 0
Case 0: Wrong output
0 0
Case 0: Wrong output
0 0
-----------Re-judge-----------
Case 0: Correct output
0 0
"char str1[]" is to create a space to store the strings, and the "char *str1" is a pointer to point to the constant strings.
0 like 0 dislike
Hidden content!
#include *** ** ***
int main(){
* * * * * ** * * ** **** *temp;
** ***** * *********** * * *str1 = * ** * **** *
* * ***** ** * * * * *str2 = * *** everything * * * *
** * * * * *** = str1;
* ** * * * * ***** = str2;
*** *** ***** * * = temp;
* *** * * ** * * **** ** ** ******* ** * * str1, str2);
** * * ** **** * ** * *** * 0;
}
answered by (-116 points)
edited by
0 0
Case 0: Correct output
0 0
-----------Re-judge-----------
Case 0: Wrong output
0 0
Case 0: Correct output
0 like 0 dislike
Hidden content!
* * * *** *** *
int main()
{
*** * * * ** * **** * *temp;
** * ** * * * * *** *str1 = * ** * * *
*** *** * **** * ** *str2 = * **** * *** * **

*** * *** * * ** ** = str1;
* ** ** * ** *** *** ** * = str2;
** ****** ** ** * * ** ** = temp;
* * * ** *** * ** ** * ** * *** str1, str2);
** * * * * ** 0;
}


* * **** can not ** * to *** **** * ***
we need use *** which * ******* to * *** *
answered by (-336 points)
edited by
0 0
prog.c:1:1: error: expected '=', ',', ';', 'asm' or '__attribute__' at end of input
 ccc
 ^~~
0 0
prog.c:1:1: error: expected '=', ',', ';', 'asm' or '__attribute__' at end of input
 ccc
 ^~~
0 0
prog.c: In function 'main':
prog.c:7:10: error: assignment to expression with array type
     temp = str1;
          ^
prog.c:8:10: error: assignment to expression with array type
     str1 = str2;
          ^
prog.c:9:10: error: assignment to expression with array type
     str2 = temp;
          ^
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.70.179.153
©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.5k views
12,783 questions
183,443 answers
172,219 comments
4,824 users