-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathCompress_the_String.cpp
More file actions
64 lines (49 loc) · 1.32 KB
/
Compress_the_String.cpp
File metadata and controls
64 lines (49 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Write a program to do basic string compression. For a character which is consecutively repeated more than once, replace consecutive duplicate occurrences with the count of repetitions.
For e.g. if a String has 'x' repeated 5 times, replace this "xxxxx" with "x5".
Note : Consecutive count of every character in input string is less than equal to 9.
Input Format :
Input string S
Output Format :
Compressed string
Sample Input:
aaabbccdsa
Sample Output:
a3b2c2dsa
*/
// input - given string
// You need to update in the given string itself i.e. in input. No need to return or print.
#include <bits/stdc++.h>
using namespace std;
void stringCompression(char input[]) {
// Write your code here
string ans="";
for(int i=0;input[i]!=0;){
int j=i+1;
while(input[i]==input[j]){
j++;
}
int c=j-i;
//cout<<input[i]<<endl;
//cout<<char(48+c)<<endl;
//cout<<ans<<endl;
char cd='0'+c;
if (c!=0 && c!=1){
ans+=input[i];
ans+=(char)(cd);
}
else
ans+=input[i];
i=j;
}
strcpy(input,ans.c_str());
}
#include <iostream>
#include "solution.h"
using namespace std;
int main() {
char input[1000];
cin.getline(input, 1000);
stringCompression(input);
cout << input << endl;
}