Hi This is programing in C not C find the error in each of t
Hi
This is programing in C not C++
find the error in each of the following program segments and explain how to correct it:
a) char s [ 10 ] ;
strncpy( s, \"hello\", 5 );
printf( \"%s\ \", s );
b) printf( \"%s\", \'a\' );
c) char s [ 12 ] ;
strcpy( s, \"Welcome Home\" ) ;
d) if ( strcmp( string1, string 2 ) ) {
printf( \"The strings are equal\ \" );
}
Solution
Qiestion a:
a) char s [ 10 ] ;
strncpy( s, \"hello\", 5 );
printf( \"%s\ \", s );
Answr: Issue here is with method name strncpy and parameters
it should be strcpy and also parameters count should be in this method is 2 but in our case 3 which is wrong. Please find the below corrct code.
char s [ 10 ] ;
strcpy( s, \"hello\" );
printf( \"%s\ \", s ,5 );
Question b:
Answer: Here is the issue with control string. we said control string string but define character in value.
Correct code should be either printf( \"%s\", \"a\" );or printf( \"%c\", \'a\' );
Question c:
Answer: We define character length as 12 and also assigned the string with length of 12. Generally we should define a char array length with 1 extra location pointer character \'\\0\'.
Code should be
char s [ 13 ] ;
strcpy( s, \"Welcome Home\" ) ;
Question d:
Answer:
Corrected should be
if ( strcmp( string1, string 2 ) ==0 ) {
printf( \"The strings are equal\ \" );
}
![Hi This is programing in C not C++ find the error in each of the following program segments and explain how to correct it: a) char s [ 10 ] ; strncpy( s, \ Hi This is programing in C not C++ find the error in each of the following program segments and explain how to correct it: a) char s [ 10 ] ; strncpy( s, \](/WebImages/4/hi-this-is-programing-in-c-not-c-find-the-error-in-each-of-t-977229-1761501358-0.webp)
![Hi This is programing in C not C++ find the error in each of the following program segments and explain how to correct it: a) char s [ 10 ] ; strncpy( s, \ Hi This is programing in C not C++ find the error in each of the following program segments and explain how to correct it: a) char s [ 10 ] ; strncpy( s, \](/WebImages/4/hi-this-is-programing-in-c-not-c-find-the-error-in-each-of-t-977229-1761501358-1.webp)