Qn: Write the C program to print letter and its number of times
Input: aaabcddgaapp
Output:
a3bcd2ga2p2
-By Srishti, Last Update On 12th June,2019 09:32 pm
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
  int i,p = 0,count = 1;
  char str[100];
  printf("Enter your string:");
  scanf("%s",str);
  for (i = 0; i < strlen(str); i++) {
      if (i + 1 < strlen(str)) {
          if (str[p] == str[i + 1]) {
              count++;
          } else {
              if (count == 1) {
                  printf("%c",str[p]);
              } else {
                  printf("%c%d",str[p] ,count);
              }
              p = i + 1;
              count = 1;
          }
      } else {
          if (count == 1) {
              printf("%c",str[p]);
          } else {
              printf("%c%d",str[p],count);
          }
          break;
      }
  }
return 0;
}
Input:
Enter your string: aaabcddgaapp
Output:
a3bcd2ga2p2

Pgcomments

Comments