Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • C++ longest word compiler differences
    When I finished the longest word challenge it said I failed in 2 test cases. I looked and oddly enough I failed at "hello world". I also failed at the really complicated special characters sentence, which I expected, because I wasn't aware of the isalpha function at the time. Here is my code, it is crazy because I didn't know about the isalpha function.
    #include <iostream>
    using namespace std;
    
    string LongestWord(string sen) { 
    
      // code goes here   
      string longest;
      string buffer;
      for (int a = 0; a<sen.size();++a)
      {
      if(sen.at(a)!=' '&& sen.at(a)!= '!' && sen.at(a)!= '.'&& sen.at(a)!= '@'
        && sen.at(a)!= '#'&& sen.at(a)!= '$' && sen.at(a)!= '%'&& sen.at(a)!= '^'&&
        sen.at(a)!= '&'&& sen.at(a)!= '*'&& sen.at(a)!= '('&& sen.at(a)!= ')'&&
        sen.at(a)!= '-'&& sen.at(a)!= '='&& sen.at(a)!= '?')
      {buffer += sen.at(a);}else{buffer = ' ';}
        if(buffer.size()>longest.size())
        {longest = buffer;}
      }
      sen = longest;
      return sen;           
    
    }
    
    I failed at "hello world" because it returns "world" instead of "hello" even though they are equal and I have my if statement set up to only change the "longest" string if it is actually larger, not equal to. I went ahead and pasted the code into VS C++ compiler and ran the code and it returns "hello" as it should. Why did the coderbyte compiler and the VS compiler return different results?
    laowai asked this question on 6/10/14 | cpp, longest, word
    Answers
  • +
  • 2
  • -
  • Have you tried in this line
    {buffer += sen.at(a);}else{buffer = ' ';}
    place"buffer = "" instead of "space".
  • +
  • 2
  • -
  • It works on my pc but not on your compiler
    #include <iostream>
    #include <string>
    #include <regex>
    
    using namespace std;
    
    string LongestWord(string sen) { 
    
    	std::string s = "";
    	
    	std::regex re ("[a-z0-9]+",std::regex_constants::icase);
    
    	std::sregex_iterator iter(sen.begin(), sen.end(), re);
    	std::sregex_iterator end;
    
        while(iter != end)
        {
    
            for(unsigned i = 0; i < iter->size(); ++i)
            {	
    		std::string temp = (*iter)[i];	
    		if(temp.length() > s.length())
    			s = temp;
            }
            ++iter;
        }
      return s; 
                
    }
    
    int main() { 
      
      // keep this function call here
      //cout << LongestWord(gets(stdin));
    	std::cout << LongestWord("aaa!bbbb") << "\n";
    
      return 0;
        
    } 
    
    
  • +
  • 0
  • -
  • I agree with Avanciak. You start out with the default string constructor which creates an empty string (""), but when you reset the string, you reset it to (' ') - a single whitespace character. The behaviour of the coderbyte compiler is therefore as you would expect. "hello"(size: 5) is shorter than " world"(size: 6). I have no idea why the VS compiler would produce a different result, unless it initializes the string differently or resets the string incorrectly to an empty string. Much more importantly: look at the <cctype> (ctype.h) header from the c/c++ stl: http://www.cplusplus.com/reference/cctype/ It provides functions, which let you check things, like whether a character is an alphabetic character. Interestingly few people on this site seem to know these functions even though they exist in practically every language in some way. Your way of checking is extremely inefficient and - more importantly - faulty. If you would read a control character such as a tab or newline you would not reset the buffer, even though both would undoubtably end the current word. If you want to ignore the stdlib functions, the usual way to check this, would be to compare, if the character is in the range of values that represent alphabatic characters in ASCII. Refer to an ascii-table to find the ranges you want, e.g.: http://www.asciitable.com/ . For alphabetical characters the ranges would be 65-90 (upper case) and 97-122 (lower case). The cctype functions typically do just that.
    Login to write an answer