//Searching for specific characters      
   while (!stats.eof())
   {
      stats.getline(counter,200);
      for (int i=0; i<strlen(counter); i++)
      {
          if (isupper(counter[i]))
             upper++;
          if (islower(counter[i])) 
             lower++;
          if (isdigit(counter[i]))
             decimal++;
          if (isspace(counter[i]))
             blank++;
          if (counter[i] == '.')
             end++;
          if (counter[i] == '!')
             end++;          
          if (counter[i] == '?')
             end++;
          total++;  
      }    
   }

Hi guys, I just finished my assignment but I'm still confused about the strlen command.
Above is the section of code to search through a text file and check for specific characters.

I tried using the strlen command to get the total no. of characters but it doesn't work. Thus, I had to use total++.
However for

for (int i=0; i<strlen(counter); i++)

It does search through the whole file though.

When i tried using

total = strlen(counter)

The result is not what I want at all. Weird...
Can anyone point me in the right direction?
Thanks in advance =]

Recommended Answers

All 6 Replies

first problem: the while statement is incorrect. Here is how to do it. while( stats.getline(counter,200) ) Why do you think strlen() doesn't work? Its been working correctly for millions of programmers all over the globe for over 25 years, so I doubt it has a bug.

I tried using total = strlen(counter) to replace my total++ but I get 16 instead of 90.
I'm not saying that it's buggy, just that my C++ isn't really good so I don't understand many parts of it.

For stats.getline(counter,200); It works without the for loop too. If I understand this correctly, the while (!stats.eof()) will help loop and get the data from the text file.
So why do I need another while loop?

Appreciate the help :)

I think you misunderstood what strlen() does -- it returns the number of characters in the buffer up to, but not including, the first byte that contains 0. That is normally smaller than the number of characters that the buffer can hold.

Example: char buffer[255] = "Hello"; In the above, strlen() will return 5.

For stats.getline(counter,200); It works without the for loop too. If I understand this correctly, the while (!stats.eof()) will help loop and get the data from the text file.
So why do I need another while loop?

Appreciate the help :)

You don't need another while loop -- just correct the while loop you already have. eof() doesn't work the way you think it does and will cause the loop to execute one too many times. Replact that with what I posted and the loop will work correctly.

Aaaa I see, my lecture notes had a similar example and it used the while eof() loop.
I'll go read up more on the strlen command.
Thanks again. :)

your lecture notes are wrong and so is your instructor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.