do while loop that doesn't end in C - c

In this program I am attempting to save each character that the user inputs into a do while loop that should count the number of spaces, new lines, and tabs.
When I run my program it doesn't end when '.', '!', or '?'
Why?
int characters, spaces, new_lines, tabs;
int user_input;
printf("Enter a sentence (end by '.' or '?' or '!'):");
do{
user_input = getchar();
if (user_input == ' ')
spaces++;
if (user_input == '\t')
tabs++;
if (user_input == '\n')
new_lines++;
} while((user_input != '.') || (user_input != '?') || (user_input != '!'));
printf("Number of space characters: %d", spaces);
printf("Number of new line characters: %d", new_lines);
printf("Number of tabs: %d", tabs);
return 0;

(user_input != '.') || (user_input != '?') || (user_input != '!')
The above doesn't evaluate the way you think it does. For the condition to be false (and the loop to stop) all three clauses must be false. That means that all respective inverses must be true, i.e:
(user_input == '.') && (user_input == '?') && (user_input == '!')
And that is of course impossible. A single character variable cannot contain three different values at once.
I assume you want the loop to terminate if the program receives either of those characters as input, so you need to check that the input is neither of those, meaning:
(user_input != '.') && (user_input != '?') && (user_input != '!')

Related

While loop in C prints the same line more than once

char ch;
int nr=0;
printf("\n: ");
ch = getchar();
while(ch != 'q' && ch != 'Q'){
ch = tolower(ch);
if(ch == 'a' || ch == 'e' || ch == 'o' || ch == 'i' || ch == 'u')
nr++;
printf("something");
ch = getchar();
}
printf("vocale: %d", nr);
its supposed to count the number of vowels until the user presses q or Q. it's such a silly program and yet i cant get past it.
Instead of using getchar
ch = getchar();
that also reads white space characters use scanf like
scanf( " %c", &ch );
Pay attention to the leading space in the format string. It allows to skip white space characters.
For example
while ( scanf( " %c", &ch ) == 1 && ch != 'q' && ch != 'Q'){
Also it will be more safer to write
ch = tolower( ( unsigned char )ch );
The problem is, that the input only gets flushed to your program whenever the user presses enter. Another reason why it seems not to work is, because you don't have a newline at the end of you output (printf("vocale: %d", nr); ), which causes the output not to be flushed to the terminal when the program ends. Fix this and your program works, but maybe not as you expect it to, because you still have to press enter. It will still only count to the first 'q' found.
int main() {
char ch;
int nr = 0;
printf(": ");
while(tolower(ch = getchar()) != 'q'){
ch = tolower(ch);
if(ch == 'a' || ch == 'e' || ch == 'o' || ch == 'i' || ch == 'u')
nr++;
}
printf("vocale: %d\n", nr);
}
The program:
: aeu q oi (here I pressed enter)
vocale: 3

Switch Case to count amount of words and characters

the output window commandI am trying to use switch case in C to figure out the amount of characters, words, newlines in a user input. The code seems legit, no errors raised, however, the output does not work as expected. Please take a look and tell me what I did wrong. Thanks in advance! Here is the code:
#include <stdio.h>
int main()
{
char a, words = 1, characters = 0, newlines = 0;
printf("What do you have in mind? ");
a = getchar();
while ((a=getchar()) && a != EOF)
{
switch (a)
{
case '1':
if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z')
characters++;
printf("The amount of character is %c ", characters);
case '2':
if (a == ' ' || a == '\t')
words++;
printf("The amount of word is %c ", words);
case '3':
if (a == '\t')
newlines++;
printf("The amount of newlines is %c ", newlines);
default:
if (a == EOF)
break;
}
}
return 0;
}
you misunderstand what switch /case means. It does not means 'first case','second case' .. of some conditions, it means (in your specific situation), if the user just typed '1' then do this, it the users just typed '2' then do this,... well you are not typing 1 or 2 or 3.
Simply do this
while ((a=getchar()) && a != EOF)
{
if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z')
characters++;
printf("The amount of character is %c ", characters);
if (a == ' ' || a == '\t')
words++;
printf("The amount of word is %c ", words);
if (a == '\t')
newlines++;
printf("The amount of newlines is %c ", newlines);
}
Also you must change a to be an int
#include<stdio.h>
int
main ()
{
int ch_count = 0, line_count = 0, word_count = 0, choice;
char ch;
FILE *fp;
fp = fopen ("wordcount.txt", "r"); //make a seperate file "wordcount.txt" and Read the Test content from it.
if (fp == NULL) // Show error if previous step is not done i.e wordcount.txt file is not made.
{
perror ("FILE NOT FOUND.");
return (-1);
}
else // If file is opened properly then ask for NO. of counts.
{
printf ("Select the Following Option-------\n"
"1 - Number of Characters\n"
"2 - Number of Words\n"
"3 - Number of Lines\n");
scanf ("%d", &choice);
}
{
switch (choice) // switch to desired case as per choice of user.
{
case 1: // CASE 1 - To count the characters in the file.
{
while ((ch = fgetc (fp)) && ch != EOF)
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
ch_count++;
}
printf ("Number of Characters : %d\n", ch_count);
break;
case 2: // CASE 2: To count total number of words
while ((ch = fgetc (fp)) && ch != EOF)
if ((ch == ' ') || (ch == '\t') || (ch == '\n') || (ch == '\0'))
{
word_count++;
}
printf ("Numbers of Words : %d\n", word_count);
break;
case 3: // CASE 3: To count total number of lines
while ((ch = fgetc (fp)) && ch != EOF)
if ((ch == '\n') || (ch == '\0'))
{
line_count++;
}
printf ("Numbers of lines : %d\n", line_count);
break;
} //switch closed
}
fclose (fp); //file closed
return 0;
}

C How to Capitalize character after punctuation mark without Arrays?

I am very new to the C programming language and was curious how would you capitalize a letter in a program following a punctuation mark without using an array. I tried using the ASCII code values by subtracting 32 but it just doesn't seem to work in my code. Here is a portion of my code that outputs the letters. I thought word = word - 32 would work but it does nothing when running the program. I'd appreciate the help!
while ((word = getchar()) != EOF && word != '\n'){
if (word == ' ' || word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&' || word == ';' || word == ':'){
printf("\n");
word = word - 32;
}
if ((word >= 'A' && word <= 'z')){
printf("%c", word);
}
}
You can use a flag to check whether the last entry was a punctuation, and then alter the next input based on the flag and reset it again
char word;int flag=0;
while ((word = getchar()) != EOF && word != '\n'){
if(flag==1){
printf("\n");
word = word - 32; flag=0;
}
if (word == ' ' || word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&' || word == ';' || word == ':'){
flag=1;
}
if ((word >= 'A' && word <= 'z')){
printf("%c", word);
}
}

How to run through a loop multiple times in C?

Ok i modified my code but cannot get it to break when the user inputs 0. I tried 0, '0', and "0" and neither break the loop.
#include <stdio.h>
#include<conio.h>
int main(){
int word;
int countword = 0;
int countpunct = 0;
do{
printf("\nEnter the String: ");
while ((word = getchar()) != EOF && word != '\n'){
if (word == ' ' || word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countword++;
}
if (word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countpunct++;
}
}
printf("\nThe number of words is %d.", countword);
printf("\nThe number of punctuation marks is %d.", countpunct);
} while (word!= 0);
}
Your inner loops break when word is either EOF or \n. Since you never modify it when you get to the end of the outer loop, the condition will always be true.
Going back to your pre-edit code, all you really need is to change scanf("%c", word); to scanf("%c", &word);, although you should use a separate char variable for that, since the %c format specifier expected a pointer to char. So your code should look like this:
#include <stdio.h>
#include <stdlib.h>
int main(){
int word;
char cont;
for (;;){
int countword = 0;
int countpunct = 0;
printf("\nEnter the String: ");
while ((word = getchar()) != EOF && word != '\n'){
if (word == ' ' || word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countword++;
}
if (word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countpunct++;
}
}
printf("\nThe number of words is %d.", countword);
printf("\nThe number of punctuation marks is %d.", countpunct);
printf("\nContinue? Y/N?");
scanf("%c", &cont);
if (cont!= 'y' && cont!= 'Y'){
return 0;
}
}
}
Note also that countword and countpunct are moved inside of the outer loop. That way, they're initialized to 0 for each new set of words.

Programming in C how to output inputted string onto the screen?

I am trying to to have my program output the words inputted into my program onto the screen. So far, my program outputs random characters depending on what I type. For example, if I input the word hey, it outputs on the screen %. How do I go about fixing this to output the word hey on the screen? My code is down below.
#include <stdio.h>
#include<conio.h>
int main(){
int word;
char cont;
for (;;){
int countword = 0;
int countpunct = 0;
printf("\nEnter the String: ");
while ((word = getchar()) != EOF && word != '\n'){
if (word == ' ' || word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countword++;
}
if (word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
countpunct++;
}
}
printf("%c", word);
printf("\nThe number of words is %d.", countword);
printf("\nThe number of punctuation marks is %d.", countpunct);
printf("\nContinue? Y/N?");
scanf("%c", &cont);
if (cont != 'y' && cont != 'Y'){
return 0;
}
}
}
You are using & with printf(). It will print the address of the variable (and not its value)!
Do this instead:
printf("%c", word); // notice there is no &
Apart from that, I noticed a few things in your code worth mentioning:
word is declared as an int, but is read and printed as a char. Why?
Your while loop makes it sure that word holds either EOF or \n while it exits the while loop.
while ((word = getchar()) != EOF && word != '\n')

Resources