top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Compress String with character and its count. Example: "aaabbba" -> "a3b3a1”

0 votes
556 views
C: Compress String with character and its count. Example: "aaabbba" -> "a3b3a1”
posted Aug 1, 2017 by anonymous

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button
Try this solution and compress for string

    #include <stdio.h>
    
    int main(void) {
        // your code goes here
        char string[100], output[25];
        // initializing each index with zero
        for(int i=0;i<26;i++) {
            output[i] = 0;
        }
        scanf("%[^\n]s", string);
        // getting count of each char
        for(int i=0;string[i] !='\0'; i++) {
       
        output[(int)string[i]-97]++;
        }
        int counter =0;

        for(int i=0;i<26;i++) {
            if(output[i]!=0) {
           
                printf("%c%d", (char)(i+97), output[i]);
                counter = 0;
            }
        }
        return 0;
    }
use this function to get size of new string so you dont waste memory

int size_of_cmp_string(char s[])
{
int i=0;
int new_size_of_string=0;

while(i != strlen(s))
{
if(i < strlen(s))
{

if(s[i] != s[i+1])
{new_size_of_string++;
i++;}
else
{
new_size_of_string++;
while(s[i] == s[i+1])
{
if((i+1) == strlen(s))
{break;}
i++;}}

}
}
return new_size_of_string;}

Similar Questions
0 votes

Find the first non repeating character in a string.

For example
Input string: "abcdeabc"
Output: "e"

+1 vote

char *p="India";
char t[10]="USA";

after the swap t should contain India and p as USA.

+2 votes

Output
Enter the string: Welcome to C Class!
Enter the word to insert: programming
Enter the position you like to insert: 3
The string after modification is

Welcome to C programming Class!

...