How to remove continued repeated digits in a given string with C++

1 Answer

0 votes
#include <iostream> 

std::string removerepeatedDigits(std::string str) {
	int len = str.length();
	int i = 1, j = 1;

	while (i < len) {
		if (str[i] != str[i - 1]) {
			str[j] = str[i];
			j++;
			i++;	
		}
		
		// run to next not equal character
		while (str[i] == str[i - 1]) {
		    i++;
		}
	}
	
	str.resize(j);
	
	return str;
}


int main()
{
	std::string str = "951112722221333331";
	
	str = removerepeatedDigits(str);
	
	std::cout << str;
}
 
 
 
/*
run:
 
951272131
 
*/

 



answered Sep 23, 2023 by avibootz

Related questions

...