1
C++ program to identify the repeating letter in a sequence entered by user.
DavidShen2 (0)

https://repl.it/@DavidShen2/Google-Interview-Question-1

#include <iostream>
#include <string>
#include <algorithm>

std::string input;
int A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z;

void counter(std::string input, char character,int value){
	if(input.find(character) != std::string::npos){
		value++;
	}
}

void order_print(char want){
	int capLetters[] = {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};
		
	std::sort(capLetters, capLetters+26,std::greater<int>());
	
	if(capLetters[0]==want){
		std::cout<<want;
	}
}

int main(){
	std::cout << "\x1B[2J\x1B[H";
	std::cout<<"\n*****START PROGRAM******\nEnter a sequence of Capital letters. The program will identify which ones repeat.\n\n";
	std::cin>>input;

	counter(input,'A',A);
	counter(input,'B',B);
	counter(input,'C',C);
	counter(input,'D',D);
	counter(input,'E',E);
	counter(input,'F',F);
	counter(input,'G',G);
	counter(input,'H',H);
	counter(input,'I',I);
	counter(input,'J',J);
	counter(input,'K',K);
	counter(input,'L',L);
	counter(input,'M',M);
	counter(input,'N',N);
	counter(input,'O',O);
	counter(input,'P',P);
	counter(input,'Q',Q);
	counter(input,'R',R);
	counter(input,'S',S);
	counter(input,'T',T);
	counter(input,'U',U);
	counter(input,'V',V);
	counter(input,'W',W);
	counter(input,'X',X);
	counter(input,'Y',Y);
	counter(input,'Z',Z);

	order_print('A');
	order_print('B');
	order_print('C');
	order_print('D');
	order_print('E');
	order_print('F');
	order_print('G');
	order_print('H');
	order_print('I');
	order_print('J');
	order_print('K');
	order_print('L');
	order_print('M');
	order_print('N');
	order_print('O');
	order_print('P');
	order_print('Q');
	order_print('R');
	order_print('S');
	order_print('T');
	order_print('U');
	order_print('V');
	order_print('W');
	order_print('X');
	order_print('Y');
	order_print('Z');
	
	std::cout<<"\n\n*****END PROGRAM*****\n";
	return 0;
}

The program doesn't give an output. How can I make it work correctly?

You are viewing a single comment. View All
2
abc3354 (92)

Hi !

There are some problems in your code
The way you try to solve your problem is not clear

void counter(std::string input, char character,int value) -> value is modified you need an &
void counter(std::string input, char character,int &value)

counter doesn't work. std::find will find the first character. You need a loop

if(capLetters[0]==want) You compare a character (want) and its occurrence in the input
I think you should use a map rather than an array

I hope this will help you ;)


Replit helper here !
If you found my answer helpful, you can upvote it :)
If my post answers the question, mark it as the answer with the check mark !