Counting characters, words and lines in a string using a do-while loop | C++

  c++, visual-c++

I’m trying to convert a while loop that counts characters, words, and lines in a string, into a do-while loop. Here is my while loop:

#include <stdio.h>
#include <string>
#include <typeinfo>
using namespace std;

int main()
{
int c;
int characters = 0;
int words = 1;
int newlines = 0;
printf("Input a string.  Press enter, then ctrl+Z, then enter once more to end string.n");

while ((c = getchar()) != EOF)
{
    if (c >= 'a' && c <= 'z' || c>= 'A' && c<= 'Z')
        characters++;
    else if (c == ' ')
        words++;
    else if (c == 'n')
        newlines++;
      }

printf("The number of characters is %dn", characters);
printf("The number of words is %dn", words);
printf("The number of newlines is %dn", newlines);


return 0;
}

I’ve been trying for hours to repeat the above process while using a do-while loop, all to no avail. Here is what I have so far:

#include <stdio.h>
#include <string>
#include <typeinfo>
using namespace std;  

int main()
{
int c;
int characters = 0;
int words = 0;
int newlines = 0;
printf("Input a string.  Press enter, then ctrl+Z, then enter once more to end string.n");

do 
{
    c = getchar();
    if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
        characters++;
    else if (c == ' ')
        words++;
    else if (c == 'n')
        newlines++;
} while (c = getchar() != EOF);
    

printf("The number of characters is %dn", characters);
printf("The number of words is %dn", words);
printf("The number of newlines is %dn", newlines);

return 0;

}
I would appreciate any helpful tips.

Source: Windows Questions C++

LEAVE A COMMENT