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:25 pm
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main(){
  int i,p = 0,count = 1;
  char str[100];
  cout<<"Enter your string:";
  cin>>str;
  for (i = 0; i < strlen(str); i++) {
      if (i + 1 < strlen(str)) {
          if (str[p] == str[i + 1]) {
              count++;
          } else {
              if (count == 1) {
                  cout<<str[p];
              } else {
                  cout<<str[p]<<count;
              }
              p = i + 1;
              count = 1;
          }
      } else {
          if (count == 1) {
              cout<<str[p];
          } else {
              cout<<str[p]<<count;
          }
          break;
      }
  }
return 0;
}
Input:
Enter your string: aaabcddgaapp
Output:
a3bcd2ga2p2

Pgcomments

Comments