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 %d\n", characters);
printf("The number of words is %d\n", words);
printf("The number of newlines is %d\n", newlines);
return 0;
}
I've been trying for hours to repeat the above process using a do-while
loop, but 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 %d\n", characters);
printf("The number of words is %d\n", words);
printf("The number of newlines is %d\n", newlines);
return 0;
}