Hier zwei Files, beide bis auf Zeile 19 identisch (und meiner Meinung nach logisch identisch...). Jedoch geben sie mir andere Resultate zurück - hat jemand ne Erklärung? (Nein, ich will nicht wissen, wie ich das Problem umgehen kann, sondern wie es entsteht...)
Code:
#include<iostream>
#include<cctype>
// wordcount
// PRE: Einen String
// PRE: gibt durch whitespaces getrennte W??rter zur??ck
unsigned int wordcount(std::string& s)
{
unsigned int anzahl = 0;
if(!std::isspace(s[0])){
anzahl=1;
}
for(int i=0; i<s.length()-2;i++){
if(std::isspace(s[i]) && std::isspace(s[i+1])==false){
anzahl++ ;
}
}
return anzahl;
}
int main()
{
std::string Satz= "hallo Welt wie gehts?";
std::cout << wordcount(Satz) << std::endl;
return 0;
}
Code:
#include<iostream>
#include<cctype>
// wordcount
// PRE: Einen String
// PRE: gibt durch whitespaces getrennte Wörter zurück
unsigned int wordcount(std::string& s)
{
unsigned int anzahl = 0;
if(!std::isspace(s[0])){
anzahl=1;
}
for(int i=0; i<s.length()-2;i++){
if(std::isspace(s[i])==true && std::isspace(s[i+1])==false){
anzahl++ ;
}
}
return anzahl;
}
int main()
{
std::string Satz= "hallo Welt wie gehts?";
std::cout << wordcount(Satz) << std::endl;
return 0;
}
Mfg Bischi
Lesezeichen