+3

Why the space is being printed too many times?

#include <iostream> #include <string> using namespace std; int main(){ string str; getline(cin,str); string alpha="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"; string all="zzyyxxwwvvuuttssrrqqppoonnmmllkkjjiihhggffeeddccbbaa"; for(int n=0;n<str.length();n++){ for(int a=0;a<52;a++){ if(alpha[a]==str[n]) cout<<all[a]; if(str[n]==' ') cout<<' '; } } return 0; } For example: Input is: Doing Work Output comes: wlrmt dlip

3/30/2020 1:29:49 PM

Hadia

2 Answers

New Answer

+3

CeePlusPlus Thanks it worked

+2

It's because when it checks if str[n] is a space, it checks it 52 times and therefore printing 52 spaces. You should put a break statement after the cout<<' '; statement.