Write a program that displays a string of characters on the screen but replaces all successive occurrences of the same character with a single character occurrence. For example, the string "aaabcbbccddd" is displayed "abcbcd".
Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
#define SIZE 100
void main()
{
char str[SIZE];
int i, j;
printf("Enter a string : ");
gets(str);
j=0;
i=1;
while(str[i]!='\0')
{
if(str[i]==str[j])
i++;
else
str[++j]=str[i++];
}
str[++j]='\0';
printf("Output: %s\n",str);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Rearranging values in an array