Search This Blog

Wednesday, October 28, 2015

Write a program which count all consecutive characters in a given string and write display string as in given example. e.g str = "aaabbbcc"; O/P string is "3a3b2c"

1:  #include <stdio.h>  
2:  #include <string.h>  
3:  main()  
4:  {  
5:    char *str = "aabbcc***^^^^&&&&ffff";  
6:    char opstr[100] = {0};  
7:    char c,tb[5];  
8:    int count = 1;  
9:    while(*str)  
10:    {  
11:      if ( *str == *(str +1) )  
12:      {  
13:        c = *str;  
14:        str++;  
15:        count++;  
16:      } else {  
17:        sprintf(tb,"%d",count);  
18:        strcat(opstr,tb);  
19:        sprintf(tb,"%c",c);  
20:        strcat(opstr,tb);  
21:        str++;  
22:        count=1;  
23:      }  
24:    }  
25:    printf("%s\n",opstr);  
26:  }  

1 comment: