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

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')

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

do while loop that doesn't end in 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 != '!')

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.

Printing Uppercase/Lowercase letters

I'm doing a program that is asking the user to enter a stream of characters and printing out the number of uppercase and lowercase letters. I'm trying to do it with a function, but having some trouble printing it..for every character input im entering im getting 0, 0
Would appreciate your help to understand what am I doing wrong:
#include <stdio.h>
#include <ctype.h>
int case_letters(int ch);
int main(void)
{
int x;
printf("please enter a some characters, and ctrl + d to see result\n");
case_letters(x);
return 0;
}
int case_letters(int ch)
{
int numOfUpper = 0;
int numOfLower = 0;
while ((ch = getchar()) != EOF)
{
if ((ch = isdigit(ch)) || ch == '\n')
{
printf("please enter a valid character\n");
continue;
}
else if ((ch = isupper(ch)))
{
numOfUpper++;
}
else if ((ch = islower(ch)))
{
numOfLower++;
}
}
return printf("%d, %d", numOfUpper, numOfLower);
}
All of your if statements assign different value to ch and do not check ch's value.
For example, if you enter a correct char, this
if ((ch = isdigit(ch)) || ch == '\n')
will assign 0 to ch, because isdigit(ch) will return 0. I guess you need
if ( isdigit(ch) || ch == '\n')
Same for islower and isupper.
if ((ch = isdigit(ch)) || ch == '\n')
^-- assignment, not equality test.
You're trashing the value of ch with the return value of isdigit(), and isupper(), and islower(), so that the original user-entered value is destroyed as soon as you do the isdigit test.
Try
if (isdigit(ch) || ch == '\n')
else if (isupper(ch))
else if (islower(ch))
instead. No need to preserve the iswhatever values.

Resources