Discussion
Which of the following changes can correct the program so that it prints “Geeks Quiz”?
#include <stdio.h>
void myStrcat(char *a, char *b)
{
int m = strlen(a);
int n = strlen(b);
int i;
for (i = 0; i <= n; i++)
a[m+i] = b[i];
}
int main()
{
char *str1 = "Geeks ";
char *str2 = "Quiz";
myStrcat(str1, str2);
printf("%s ", str1);
return 0;
}
Answer: A
Correct answer: char *str1 = “Geeks “; can be changed to char str1[100] = “Geeks “;
The fault is that str1 points at a string literal, which must not be modified, and there is no room after it in any case. Making it an array of 100 characters gives writable storage with space to spare. No terminator line is needed, because the copy loop runs to i <= n and so copies b’s own null byte along with its characters.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development