C How to Capitalize character after punctuation mark without Arrays? - c

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);
}
}

Related

Last character of numeric string being removed, how to fix this problem in C?

I'm trying to solve a problem with replacing letters with numbers. The user will enter a string, in case there are letters, I must substitute the corresponding number, and in case there are * # - symbols, I must simply remove them.
However, I am facing an issue. When the user types only a numeric string, the last character of this string is being removed, which cannot happen. This can only happen if there are letters or symbols in the string.
Source
#include <stdio.h>
#include <string.h>
void alterChars(char phrase[])
{
int i, dashes = 0;
for (i = 0; phrase[i] != '\0'; i++)
{
if (phrase[i] == 'A' || phrase[i] == 'B' || phrase[i] == 'C')
{
phrase[i] = '2';
}
if (phrase[i] == 'D' || phrase[i] == 'E' || phrase[i] == 'F')
{
phrase[i] = '3';
}
if (phrase[i] == 'G' || phrase[i] == 'H' || phrase[i] == 'I')
{
phrase[i] = '4';
}
if (phrase[i] == 'J' || phrase[i] == 'K' || phrase[i] == 'L')
{
phrase[i] = '5';
}
if (phrase[i] == 'M' || phrase[i] == 'N' || phrase[i] == 'O')
{
phrase[i] = '6';
}
if (phrase[i] == 'P' || phrase[i] == 'Q' || phrase[i] == 'R' || phrase[i] == 'S')
{
phrase[i] = '7';
}
if (phrase[i] == 'T' || phrase[i] == 'U' || phrase[i] == 'V')
{
phrase[i] = '8';
}
if (phrase[i] == 'W' || phrase[i] == 'X' || phrase[i] == 'Y' || phrase[i] == 'Z')
{
phrase[i] = '9';
}
if (phrase[i] == '*' || phrase[i] == '#' || phrase[i] == '-')
{
dashes++;
}
else if (dashes > 0)
{
phrase[i - dashes] = phrase[i];
}
}
phrase[strlen(phrase)-1] = '\0';
printf("%s\n", phrase);
}
int main()
{
char phrase[300];
while (!feof(stdin))
{
scanf(" %[^\n]s", phrase);
alterChars(phrase);
}
return 0;
}
Any tips will be valuable. You can access the problem to see where the error is occurring. Anyway, it's on the last entry, at number 190. It is being printed 19, but in fact it should be printed 190, because the removal of characters should only occur when there are letters, or symbols.
Examples
Input: 333-PORTO
Output: 33376786
The problem:
Input: 190
Output: 19
With your "generous" scanning, i.e. practically no expected syntax, you can simply loop while scanning is successful.
while (1==scanf(" %299[^\n]", phrase)) alterChars(phrase);
That fixes the problem with your while condition ( see Why is “while ( !feof (file) )” always wrong? ).
Then you want to read each line until either newline or terminator:
for (i = 0; phrase[i] != '\0' && phrase[i] != '\n'; i++)
And you want to force-terminate with respect to the dashes.
phrase[strlen(phrase)-dashes] = '\0';
and you get the desired output - and in only one line.... ;-)
The explanation is, if you always terminate as with 1 dash, then it works for 1 dash, fails by cutting too much for zero dashes and does other unwanted stuff for more than one dash, i.e. repeat the last few characters.
It is after all unrelated to "numeric only".
I have incorporated this good input by chux:
scanf(" %[^\n]s" is bad as it has not width limit and 2) the s is pointless.

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

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

How to echo user input and skip over punctuation and spaces? (Language C)

I've already got the part of my program that echoes user input to work, but my code seems to ignore the while line where I specify not to echo the characters in the array if they are spaces or punctuation. My code is as follows:
#include <stdio.h>
int main()
{
int word = 0, punct = 0;
int i = 0;
char phrase[500];
printf("Enter any phrase.");
while (1)
{
do
{
phrase[i] = getchar(); //gets incoming char and outputs it
putchar(phrase[i]);
} while (phrase[i] != ' ' && phrase[i] != '.' && phrase[i] != '!' && phrase[i] != '#' && phrase[i] != '#' && phrase[i] != '$' && phrase[i] != '%' && phrase[i] != '%' && phrase[i] != '^' && phrase[i] != '&' && phrase[i] != '*' && phrase[i] != '(' && phrase[i] != ')' && phrase[i] != '-' && phrase[i] != '_' && phrase[i] != '=' && phrase[i] != '+' && phrase[i] != '\\' && phrase[i] != '|' && phrase[i] != '{' && phrase[i] != '}' && phrase[i] != '[' && phrase[i] != ']' && phrase[i] != ':' && phrase[i] != ';' && phrase[i] != '\'' && phrase[i] != '\"' && phrase[i] != '<' && phrase[i] != '>' && phrase[i] != ',' && phrase[i] != '?' && phrase[i] != '/'); //prevents punctuation and spaces from bein outputted
printf("\n");
i++;
}
}
When I run the program the user input is all echoed, including the punctuation and spaces, what exactly is wrong and how do I make it so that the code outputs everything char by char and skip over the punctuation and spaces? And how would I keep the loop going after punctuation is skipped?
//gets incoming char and outputs it
This comment says it all. Your do loop outputs the character before the while test checks it. The while only stops when the input character is a punctuation mark, but by that time the character has already been sent. And then the outer while (1) loop causes the whole thing to happen all over again.
The way to fix the issue is to test the character before sending it to output. For example:
while(1) {
phrase[i] = getchar();
if (isPunctuation(phrase[i]) == false) {
putchar(phrase[i]);
}
else {
printf("\n");
i++;
}
}
Note that you don't really want the while (1) since that means the loop will continue forever. After the 500th character, you'll exceed the capacity of the phrase array and start writing into memory that doesn't belong to you. I'll leave it to you to think up a more reasonable loop condition.

Resources