I'm kinda new to c++ and can only do very simple applications.
Could someone explain why this code works:
#include <iostream>
using namespace std;
int main()
{
bool inword = 0;
unsigned int numwords = 0;
char *str = "this is a test string for my project";
char letter;
while (letter = *str++)
{
if(letter == ' ')
{
inword = 0;
}
else if(inword == 0)
{
inword = 1;
numwords++;
}
}
cout << "Found " << numwords << " words!\n";
system("PAUSE");
return 0;
}
And this doesn't:
#include <iostream>
using namespace std;
int main()
{
bool inword = 0;
unsigned int numletters = 0;
char *str = "this is a test string for my project";
char letter;
while (letter = *str++)
{
if(letter >= 'a' && letter <= 'z')
{
inword = 0;
}
else if(inword == 0)
{
inword = 1;
numletters++;
}
}
cout << "Found " << numletters << " letters!\n";
system("PAUSE");
return 0;
}
Please help if you know any solution.