Sunday, November 15, 2015

Algorithms -> Strings -> Alternating Characters

Alternating Characters


The following is the solutions for alternating characters in C

For every character there should be an opposite character ,until the opposite character is found cancel it and modified the cancelled character to '*'.


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void AlternatingCharacters(char str[100000],int n)
{
 int i=0,j,del=0;
 while(i<n)
 {
  j=i+1;
while(j<n)
{
 if(str[i]==str[j])
 {
  str[j]='*';
  del++;
  j++;
 }
 else
  break;
}
 i=j;
 }
printf("%d\n",del);
}
int main() {
int testcases,i,n;
char string[100000];
scanf("%d",&testcases);
for(i=1;i<=testcases;i++)
{
scanf("%s",&string);
n=strlen(string);
AlternatingCharacters(string,n);
}
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    return 0;
}



No comments:

Post a Comment