Discussion
Output of following program
#include <stdio.h>
int fun(char *p)
{
if (p == NULL || *p == '\0') return 0;
int current = 1, i = 1;
while (*(p+current))
{
if (p[current] != p[current-1])
{
p[i] = p[current];
i++;
}
current++;
}
*(p+i)='\0';
return i;
}
int main()
{
char str[] = "geeksskeeg";
fun(str);
puts(str);
return 0;
}
Answer: A
Correct answer: gekskeg
The function removes adjacent duplicates: current scans the string while i marks where to write, and a character is kept only when it differs from the one before it. On geeksskeeg the second e, the second s and the second e of ee are dropped, leaving gekskeg. The result is written back in place and terminated with a null.
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