How to count characters except double or single quotes? - c

I'm new to programming and learning C through a book.
The author of the book explains about logical operators (AND, NOT and OR) by giving the following example which counts the number of characters except the double or single quotes and period character.
I couldn't understand how it counts the number of characters except the quotes and period character. I understand that with the AND operator both conditions should be true.
#include <stdio.h>
#define PERIOD '.'
int main(void)
{
char ch;
int charcount = 0;
while ((ch = getchar()) != PERIOD)
{
if (ch != '"' && ch != '\'')
charcount++;
}
printf("There are %d non-quote characters.\n", charcount);
return 0;
}

I will try to explain you the main part of the code :
while ((ch = getchar()) != PERIOD)
{
Here, it will check every character contained in your text, as long as the character differ from PERIOD, which is a dot, so it simply check all of the characters in the sentence.
if (ch != '"' && ch != '\'')
charcount++;
}
Here, it adds 1 to the charcount if the condition is true. For the if to return true, both ch != '"' and ch != '\'' must be true ! The && operator is a logical AND, and for an AND to return true (1), both conditions must be equal to 1. So if the character is equal to " or ', the AND will return 0, and so we won't add 1 to the charcount.

This condition if (ch != '"' && ch != '\''), is checking if the entered character is "or ' if not then it increments the count of characters otherwise not. If user enterd d or #, it will satisfy the condition because ASCII value of # is not equal to " or ', and the count will get incremented.

Well, for each char returned by getchar() and stored in ch (from an input stream, like a keyboard or a file) it will test if it's not a double quote (ch != '"') and if it's not a quote (ch != '\'')
\ is an escape character, which means '\'' is the char '
If it's neither of them, then it will increments the counter (charcount++;).
And this will go on as long as getchar() doesn't return a period ((ch = getchar()) != PERIOD)(if it does, the PERIOD won't be counted as the code will step out the will loop immediately).

You can skip using continue:
#include<stdio.h>
#define PERIOD '.'
int main(void)
{
char ch;
int charcount = 0;
while ((ch = getchar()) != PERIOD)
{
if (ch != '"' && ch != '\''){
continue;
}
charcount++;
}
printf("There are %d non-quote characters.\n", charcount);
return 0;
}
Output:
./program
Michi"""LoL'''Another'"LoL.
There are 8 non-quote characters.

Related

Why does this code work but the other version does not?

Specifically, the code is a solution to Exercise 1-9 in K&R C Programming Language 2nd Edition. I already solved it, but I have a question.
Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
This code works and returns the desired output
int ch, last;
for (last = 0; (ch = getchar()) != EOF; last = ch)
if (ch == ' ' && last == ' ')
;
else
putchar(ch);
This version of the code doesn't work and instead prints literally the same input with excess spaces included.
int ch, last;
last = 0;
while ((ch = getchar()) != EOF)
if (ch == ' ' && last == ' ')
;
else
putchar(ch);
last = ch;
Could somebody tell me the difference between these two versions of code and why the latter version doesn't work?
last = ch; is not inside the loop, so you're not setting it until after you've read the entire file. So you're comparing with 0 every time.
If you want to have multiple statements in a loop, you have to put {} around them.
while ((ch = getchar()) != EOF) {
if (ch == ' ' && last == ' ') {
} else {
putchar(ch);
}
last = ch;
}
This is a perfect example of Why is it considered a bad practice to omit curly braces?
In the 2nd version you need to place the body of the while loop in a {} as it consist of two statements (if and last= assignment). Otherwise only the first statement is executed in the loop, and the 2nd after the loop terminates.
int ch, last;
last = 0;
while ((ch = getchar()) != EOF) {
if (ch == ' ' && last == ' ')
;
else
putchar(ch);
last = ch;
}
I would invert the condition:
if (ch != ' ' || last != ' ')
putchar(ch);
The answers about curly braces are correct.
There is, however, another way to transform the for() expression into a while() loop without an abundance of filigree in the code.
Below traversing a string stands in for getting characters from stdin.
#include <stdio.h>
int main() {
char *str = " Once upon a time in a land far away ";
int i = 0, ch, last = 0;
while( (ch = str[i++]) != '\0' )
if ( ch != ' ' || last != ' ' )
putchar( last = ch ); // NB: copy value
return 0;
}
Output
" Once upon a time in a land far away " // quotes added to show SPs
(Shameless duplication of #Alan Wind's observation about boolean logic.)

The answer outputs blanks

Program task -
Enter a string, display it word for word on the screen.
The problem is that if you type a lot of spaces between words, they will show up when you check. How can this be fixed?
#include <stdio.h>
int main()
{
int inw = 0, i = 0, count = 0;
char s[10000];
printf("Print string (max 10000 sb):\n");
gets(s);
while (s[i] != '\0') {
if (s[i] != ' ' && s[i] != '\t') {
putchar(s[i]);
}
else if (s[i] == ' ') {
printf("\n");
}
i++;
}
return 0;
}
Ugly, but this gets the job done. Just need a flag to keep track of whether or not you just printed a new line. Also cleaned up unused variables and changed to using fgets
#include <stdio.h>
#include <stdbool.h>
int main()
{
int i = 0;
char s[10000];
bool justPrintedNewline = false;
printf("Print string (max 10000 sb):\n");
fgets(s, sizeof s, stdin);
while (s[i] != '\0') {
if (s[i] != ' ' && s[i] != '\t') {
putchar(s[i]);
justPrintedNewline = false;
}
else if (s[i] == ' ' && justPrintedNewline == false) {
printf("\n");
justPrintedNewline = true;
}
i++;
}
return 0;
}
Demo
You did a great job in the algorithm just fix a little thing.
You can create a flag and after space you increase the flag to 1.
Then you will know you will print just one space.
After printing " " check for a char that isn't " " for update the flag to 0.
When the flag is 1 DONT print anything just wait for another valid char.
Take care,
Ori
Only print a line-feeed when starting a word and after all is done.
Change code to:
If a space
-- print a '\n' when the prior character is a non-white-space.
Else
-- if (prior character is white-space) print a '\n'
-- print it
char prior = 'a';
while (s[i]) {
char ch = s[i];
if (ch != ' ' && ch != '\t') {
if (prior == ' ' || prior == '\t') {
putchar('\n');
}
putchar(ch);
}
prior = ch;
i++;
}
putchar('\n');
There is a bit of a trick to it: use a second, inside loop to skip past spaces and another to print words. The outer loop should only terminate if you have reached the end of the string.
while (s[i] != '\0')
{
// skip all spaces
while ((s[i] != '\0') && isspace( s[i] )) ++i;
// print the word
while ((s[i] != '\0') && !isspace( s[i] ))
{
putchar( s[i] );
}
// print the newline after a word
putchar( '\n' );
}
By the way, gets() is a really, really dangerous function. It should never have been included in the language. You are OK to use it for a homework, but in reality you should use fgets().
char s[1000];
fgets( s, sizeof(s), stdin );
The fgets() function is a bit more fiddly to use than gets(), but the above snippet will work for you.
Your other option for solving this homework is to use scanf() to read a word at a time from user input, and print it each time through the loop. I’ll leave that to you to look up. Don’t forget to specify your max string length in your format specifier. For example, a 100 char array would be a maximum 99-character string, so you would use "%99s" as your format specifier.

Counting lines, words, and characters

I am a new to programming. I am trying to write a program that reads lines, words and characters from a text file. Here is the code below.
#include "stdio.h"
#include "stdlib.h"
#define IN 1
#define OUT 0
int main (int argc, char *argv[]) {
FILE *input;
int character, newword, newline, state;
char c;
state = OUT;
character = newline = newword =0;
input = fopen(argv[1], "r");
if ( input == NULL){
printf("Error! Can not read the input\n");
exit(-1);
}
while ((c = fgetc(input)) != EOF){
character++;
if (c <'a' && c >'z'){;}
if ( c <'A' && c >'Z'){;}
if (c == '\n'){
newline++;
}
if (c == ' ' || c == '\n' || c == '\t'){
state = OUT;
}
else if (state == OUT){
state = IN;
newword++;
}
}
printf("The number of lines: %d\n", newline);
printf("The number of words: %d\n", newword);
printf("The number of characters: %d\n", character);
fclose(input);
}
I have been trying to figure how not to read special characters such as !, #, #, $, %, ^, &, *, (, ), _, +.
I tried using if statements so it won't read the special characters but it reads it. I think one the if statement for the capital letters is wrong because it probably will not read lower case letters.
In the file the following text is in it,
!!.
and it outputs in terminal:
The number of lines: 2
The number of words: 5
The number of characters: 7
However, if I take out the two for loops (c < 'A' && c > 'Z') and (c < 'a' && c > 'z'), then the output becomes
The number of lines: 2
The number of words: 1
The number of characters: 7
Any hints to fix this problem (I do not want the Answer!)?
Your if must be something like:
if ('a' <= c && c <='z'){character++;}
else if ( 'A' <= c && c <='Z'){character++;}
The easiest way to solve your problem is to increase your character counter when the character is between the interval 'a' and 'z' or the interval 'A' and 'Z', and then, since the escape sequence '\n' creates a new line, this also means that you are dealing with a new word, thus you should increment this counter too. And finally you can check for the space or the horizontal tab to increment the new word counter.
if ((c <'a' && c >'z') || ( c <'A' && c >'Z')){
++character;
}
else if (c == '\n'){
++newline;
++newword;
}
else if (c == ' ' || c == '\t'){
++newword;
}

Exceptions in c programming language

I'm learning c language and I hit a wall, if you would like to help me I appreciate (here is the ex: "Write a program that reads characters from the standard input to end-of-file. For each character, have the program report whether it is a letter. If it is a letter, also report its numerical location in the alphabet and -1 otherwise." btw is not homework).The problem is with the \n i don't know how to make it an exception. I'm new around here please let me know if I omitted something. Thank you for your help.
int main(void)
{
char ch;
int order;
printf("Enter letters and it will tell you the location in the alphabet.\n");
while ((ch = getchar()) != EOF)
{
printf("%c", ch);
if (ch >= 'A' && ch <= 'Z')
{
order = ch - 'A' + 1;
printf(" %d \n", order);
}
if (ch >= 'a' && ch <= 'z')
{
order = ch - 'a' + 1;
printf(" %d \n", order);
}
if (order != (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
{
if (ch == '\n');
else if (order != (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
printf(" -1 \n");
}
}
system("pause");
}
You are talking about an "exception" which can be interpreted in other ways in programming.
I understand that you want that '\n' be "excepted" in the set of nonalphabetical characters, that is, that it doesn't generate the error value -1.
Since you are using console to run the program, a sequence of character is going to be read till ENTER key is pressed, which generates the character \n. So, I'm not pretty sure that the while() condition you used, that compares against EOF, it's a good decision of yours.
I would put there directly the comparisson against '\n'.
while ((ch = getchar()) != '\n')
To inform if ch is a letter or not, we could use string literals. The following use of string assignment would deserve more explanation, but I will omit it. It's valid with string literals:
char *info;
if (order != -1)
info = "is a letter";
else
info = "is not a letter";
You are assuming an encoding where the letters are in contiguous increasing order (as in ASCII).
By assuming that, it's enough to work with uppercase or lowercase letters, since you are only interested in the position that the letter occupy in the alphabet. So, you can choose to work with uppercase, for example, in this way:
if (ch >= 'a' && ch <= 'z')
ch = (ch - 'a' + 'A');
The effect of that line of code is that ch is converted to uppercase, only if ch is a lowercase letter. Another kind of character is not affected.
As a consequence, from now on, you only have uppercase letters, or nonalphabetical characters.
Then it's easy to code the remaining part:
if (ch >= 'A' && ch <= 'Z')
order = ch - 'A' + 1; // It brings no. position of letter in alphabet
else
order = -1; // This is the erroneous case
A printf() at the end of the loop could bring all the information about the character:
printf(" %16s: %4d \n", info, order);
The resulting code is shorter in more clear:
#include <stdio.h>
int main(void) {
char ch;
int order;
char *info;
while ((ch = getchar()) != '\n') {
printf("%c",ch);
if (ch >= 'a' && ch <= 'z') /* Converting all to uppercase */
ch = (ch - 'a' + 'A');
if (ch >= 'A' && ch <= 'Z')
order = ch - 'A' + 1; /* Position of letter in alphabet */
else
order = -1; /* Not in alphabet */
if (order != -1)
info = "is a letter";
else
info = "is not a letter";
printf(" %16s: %4d \n", info, order);
}
}
If you need to end the input by comparing against EOF, then the type of ch has to be changed to int instead of char, so you can be sure that the EOF value (that is an int) is properly held in ch.
Finally, this means that ch needs initialization now, for example to a neutral value in the program, as '\n'.
Finally, just for fun, I add my super-short version:
#include <stdio.h>
int main(void) {
int ch, order;
while ((ch = getchar()) != '\n') {
order = (ch>='a' && ch<='z')? ch-'a'+1:((ch>='A' && ch<='Z')? ch-'A'+1: -1);
printf("%c %8s a letter: %4d \n", ch, (order != -1)? "is":"is not", order);
}
}
The C language does not have exceptions. Exceptions were first introduced into programming in C++. You can do it manually in C using setjmp() and longjmp(), but it really isn't worth it.
The two most popular of doing error handling in C are:
Invalid return value. If you can return -1 or some other invalid value from a function to indicate 'there was an error', do it. This of course doesn't work for all situations. If all possible return values are valid, such as in a function which multiplies two numbers, you cannot use this method. This is what you want to do here - simply return -1.
Set some global error flag, and remember to check it later. Avoid this when possible. This method ends up resulting in code that looks similar to exception code, but has some serious problems. With exceptions, you must explicitly ignore them if you don't want to handle the error (by swallowing the exception). Otherwise, your program will crash and you can figure out what is wrong. With a global error flag, however, you must remember to check for them; and if you don't, your program will do the wrong thing and you will have no idea why.
First of all, you need to define what you mean by "exception"; do you want your program to actually throw an exception when it sees a newline, or do you simply want to handle a newline as a special case? C does not provide structured exception handling (you can kind-of sort-of fake it with with setjmp/longjmp and signal/raise, but it's messy and a pain in the ass).
Secondly, you will want to read up on the following library functions:
isalpha
tolower
as they will make this a lot simpler; your code basically becomes:
if ( isalpha( ch ) )
{
// this is an alphabetic character
int lc = tolower( ch ); // convert to lower case (no-op if ch is already lower case)
order = lc - 'a' + 1;
}
else
{
// this is a non-alphabetic character
order = -1;
}
As for handling the newline, do you want to just not count it at all, or treat it like any other non-alphabetic character? If the former, just skip past it:
// non-alphabetic character
if ( ch == '\n' )
continue; // immediately goes back to beginning of loop
order = -1;
If the latter, then you don't really have to do anything special.
If you really want to raise an honest-to-God exception when you see a newline, you can do something like the following (I honestly do not recommend it, though):
#include <setjmp.h>
...
jmp_buf try;
if ( setjmp( try ) == 0 ) // "try" block
{
while ( (ch = getchar() ) != EOF )
{
...
if ( ch == '\n' )
longjmp( try, 1 ); // "throw"
}
}
else
{
// "catch" block
}
I'm having hard time trying to understand why you even try to handle '\n' specifically.
You might be trying to implement something like this:
int main(void)
{
char ch;
int order;
printf("Enter letters and it will tell you the location in the alphabet.\n");
while ((ch = getchar()) != EOF)
{
printf("%c", ch);
if (ch >= 'A' && ch <= 'Z') {
order = ch - 'A' + 1;
printf(" %d \n", order);
} else if (ch >= 'a' && ch <= 'z') {
order = ch - 'a' + 1;
printf(" %d \n", order);
} else if (ch == '\n') { } else {
printf(" -1 \n");
}
}
system("pause");
}
While this is a good solution, I would recommend rewriting it in a more optimal way:
int main(void)
{
char ch;
printf("Enter letters and it will tell you the location in the alphabet.\n");
while ((ch = getchar()) != EOF)
{
int order;
if (ch != '\n'){
if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') {
order = ch & 11111B;
printf("Letter %d\n", order);
} else {
order = -1;
printf("Not letter: %d\n", order);
}
}
}
system("pause");
}
This way the program relies on specific way letters coded in ASCII

Logical Error in my C Code

I am a beginner, just learning C; so, please be patient with me.
I am trying to write a very simple program that goes through a string, counts the characters in each word, and then replaces those words with the number of their characters. My problem is that I am stuck in an infinite loop and cannot figure out why! Here is the code:
#define NEWLINE '\n'
#define SPACE ' '
int main(int argc, char *argv[]) {
int character;
int count = 0;
printf("\nType in a sentence of any lenght, then hit ENTER!\n");
character = getchar();
while (character != NEWLINE) {
while ((character != SPACE) || (character != NEWLINE)) {
count++;
character = getchar();
}
printf("%d ", count);
count = 0;
if (character != NEWLINE) {
character = getchar();
}
}
printf("\n");
system("PAUSE");
return 0;
}
Thanks for everyone who helped me! I guess I go back and study logical operators a bit more.
while ((character != SPACE) || (character != NEWLINE)) {
count++;
character = getchar();
}
this will loop infinitely because the inverse:
(character == SPACE) && (character == NEWLINE)
will ALWAYS be false.
I suspect you mean
while ((character != SPACE) && (character != NEWLINE)) {
count++;
character = getchar();
}
You have an always true condition:
(character != SPACE) || (character != NEWLINE)
eg:
character = ' ': false or true => true
character = '\n': true or false => true
You should change it to:
(character != SPACE) && (character != NEWLINE)
That should fix your infinite loop.
Replace the || With &&
Replace the || (OR) with && (AND) because the while loop will always be true and for that reason it will never break out of the loop. It should work if you replace it.

Resources