2015-08-26 3 views
0

J'ai un problème à résoudre. Je dois créer une fonction qui recive 2 paramètres, l'un est de type chaîne et l'autre est int. Je dois entrer une phrase, puis un numéro. Mon programme doit supprimer le mot qui est égal à ce nombre. J'ai écrit beaucoup de mon code, mais j'ai un problème, par exemple si je saisis le numéro un, ou si ma phrase contient beaucoup d'espaces vides entre les mots, cela ne les supprime pas. Je ne dois pas utiliser d'itérateurs. Ceci est mon code.Suppression de la sous-chaîne de votre chaîne

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

int Numberofwords(std::string s) 
{ 
    int trigg (0),suff(0); 

    int x(0),y(s.length()-1); 

    while(s[x] == ' ') 
     x++; 

    for(int x(y);x>=0;x--) 
    { 
     if(s[x]==' ') suff++; 
     if(s[x] != ' ') break; 
    } 

    for(;x<s.length()-suff;x++) 
    { 
     if(s[x] == ' ') trigg++; 
     if((s[x] == s[x+1]) && s[x] == ' ') trigg--; 
     if((s[x] == ' ' && s[x+1]==' \n')) trigg--; 
    } 
    trigg++; 
    return trigg; 
} 

std::string Funkcija(std::string recenica, int n) 
{ 
    int br(Numberofwords(recenica)),x(0),y(recenica.length()-1),suff(0),counter(0),trig(0); 

    std::string s; 

    if(n<1 || n>br) throw "Inputed number must be bigger than 0 and smaller then number of words in sentence."; 

    while(recenica[x] == ' ') 
     x++; 

    for(int i(y);i>=0;i--) 
    { 
     if(recenica[i] == ' ') suff++; 
     if(recenica[i] != ' ') break; 
    } 

    int a(x); 
    for(;a<recenica.length()-suff;a++) 
    { 

     if(recenica[a] == ' ') trig++; 

     if(trig == (n-1)) 
     { 
      int e(a); 
      while(recenica[e+1] != ' ') 
      { 
       counter++; 
       e++; 
      } 

      recenica.erase(a+1,counter); 
     } 

    } 
    return recenica; 
} 

Répondre

0

Vous pouvez réaliser cela en itérant votre chaîne.

chaîne itération peut être fait avec différentes façons: (vous voulez que la troisième voie, mais si vous pouviez, je vous recommande la première ou la seconde)

// Way #1: Iterators 
std::string remove_word_by_number(std::string source, unsigned int word_number) 
{ 
    if (word_number == 0) 
     throw runtime_error("word_number cannot be 0"); 

    std::ostringstream out; 

    bool word_removed = false; 
    bool inside_word = false; 
    unsigned int words_parsed = 0; 
    for (std::string::iterator it = source.begin(); it != source.end(); ++it) 
    { 
     if (isalnum(*it)) 
     { 
      if (!inside_word) 
      { 
       inside_word = true; 
       words_parsed++; 
      } 
      if (words_parsed != word_number) 
       out << *it; 
      else 
       word_removed = true; 
     } 
     else 
     { 
      inside_word = false; 
      out << *it; 

      if (word_removed) 
      { 
       out << std::string(it, source.end()); 
       break; 
      } 
     } 
    } 

    return out.str(); 
} 

// Way #2: C-style iterators (pointers, not sure if you can use this kind of iterators) 
std::string remove_word_by_number(std::string source, unsigned int word_number) 
{ 
    if (word_number == 0) 
     throw runtime_error("word_number cannot be 0"); 

    std::ostringstream out; 

    bool word_removed = false; 
    bool inside_word = false; 
    unsigned int words_parsed = 0; 
    for (const char* it = source.data(); it != source.data()+source.size(); it++) 
    { 
     if (isalnum(*it)) 
     { 
      if (!inside_word) 
      { 
       inside_word = true; 
       words_parsed++; 
      } 
      if (words_parsed != word_number) 
       out << *it; 
      else 
       word_removed = true; 
     } 
     else 
     { 
      inside_word = false; 
      out << *it; 

      if (word_removed) 
      { 
       out << source.substr(it-source.data()); 
       break; 
      } 
     } 
    } 

    if (!word_removed) 
     throw runtime_error("word_number out of range"); 

    return out.str(); 
} 

// Way #3: Using string's operator[] 
std::string remove_word_by_number(std::string source, unsigned int word_number) 
{ 
    if (word_number == 0) 
     throw runtime_error("word_number cannot be 0"); 

    std::ostringstream out; 

    bool word_removed = false; 
    bool inside_word = false; 
    unsigned int words_parsed = 0; 
    for (std::size_t it = 0; it < source.size(); it++) 
    { 
     if (isalnum(source[it])) 
     { 
      if (!inside_word) 
      { 
       inside_word = true; 
       words_parsed++; 
      } 
      if (words_parsed != word_number) 
       out << source[it]; 
      else 
       word_removed = true; 
     } 
     else 
     { 
      inside_word = false; 
      out << source[it]; 

      if (word_removed) 
      { 
       out << source.substr(it); 
       break; 
      } 
     } 
    } 

    return out.str(); 
} 

Hope it helps

+0

grâce un beaucoup d'homme ... (y), ça m'a vraiment aidé ... – Ahmo