mercredi 15 janvier 2020

Someone review my code and simplify it where possible

(sorry if this isn't an average post I'm new to StackOverflow) I am a beginner programmer with very basic knowledge of programming concepts. Maybe you can get a better understanding of how much I know by the code I wrote. Also my formatting sucks

This is a basic program that just deletes "stop words" from your string that you input. I feel like the code I wrote was very overdone and redundant. Just some tips on simplification would be appreciated :).

written in c++ using string

#include <iostream>
#include <string>
using namespace std;
int main() {
cout<<"enter stop words, seperate by comma with no spaces and put a comma at the end: ";
string stop_words;
string word1, word2, word3, word4, word5, word6;
int comma, comma2, comma3, comma4, comma5, comma6, Switch, words=0;
getline(cin, stop_words);
 int x = 0;
int numcommas = 0;
for(int i=0; i<stop_words.length(); i++){
  if(stop_words[i]==','){
  numcommas++;
  }
}

while(x<numcommas){

comma = stop_words.find(',');
comma2 = stop_words.find(',',comma+1);
comma3 = stop_words.find(',',comma2+1);
comma4 = stop_words.find(',',comma3+1);
comma5 = stop_words.find(',',comma4+1);
comma6 = stop_words.find(',',comma5+1);



word1 = stop_words.substr(0,comma);

if(comma2<stop_words.length())
{
word2 = stop_words.substr(comma+1,comma2-(comma+1));
}

if((comma3<stop_words.length())&&(comma2<stop_words.length()))
{
  word3 = stop_words.substr(comma2+1,comma3-(comma2+1));
}

if((comma4<stop_words.length())&&(comma3<stop_words.length())&&(comma2<stop_words.length()))
{
  word4 = stop_words.substr(comma3+1,comma4-(comma3+1));
}

if((comma5<stop_words.length())&&(comma4<stop_words.length())&&(comma3<stop_words.length())&& 
(comma2<stop_words.length()))
{
  word5 = stop_words.substr(comma4+1,comma5-(comma4+1));
}

if((comma6<stop_words.length())&&(comma5<stop_words.length())&&(comma4<stop_words.length())&& 
(comma3<stop_words.length())&&(comma2<stop_words.length()))
{
  word6 = stop_words.substr(comma5+1,comma6-(comma5+1));
}


x++;
}

cout<<word1<<endl<<word2<<endl<<word3<<endl;




cout<<endl<<"enter your sentence: ";
string sentence;
getline(cin, sentence);
int place = 0;
//I am aware there is no need for this switch statement
Switch=1;
switch(Switch){
case 1:
place=sentence.find(word1);
if(place<sentence.length()&&(word1!="")){
sentence.erase(place,word1.length()+1);
}
case 2:
place=sentence.find(word2);
if(place<sentence.length()&&(word2!="")){
sentence.erase(place,word2.length()+1);
}
case 3:
place=sentence.find(word3);
if(place<sentence.length()&&(word3!="")){
sentence.erase(place,word3.length()+1);
}
case 4:
place=sentence.find(word4);
if(place<sentence.length()&&(word4!="")){
sentence.erase(place,word4.length()+1);
}
case 5:
place=sentence.find(word5);
if(place<sentence.length()&&(word5!="")){
sentence.erase(place,word5.length()+1);
}
case 6:
place=sentence.find(word6);
if(place<sentence.length()&&(word6!="")){
sentence.erase(place,word6.length()+1);
}

}


cout << sentence << "\n";
}

Aucun commentaire:

Enregistrer un commentaire