//QUESTION 1
- //This line includes a library called stdio, which make it possible for you
- //use functions like fprintf , fopen, etc.
- #include <stdio.h>
- //Every file must have a main function, functions begins with a return type, the name, then any parameters
- //so in this case, int would be the return type, main is the name, and inside () would be any parameters
- int main(){
- //So fprintf basically prints a string to an output
- //fprintf takes 2 arguments, an output (screen, file. or any other output streams) and a string
- //In this case, stdout is the output (which is just the screen), and "Hello World" is the string
- fprintf (stdout, "Hello World");
- }
//QUESTION 2
- #include <stdio.h>
- #include <ctype.h>
- int main(){
- char c = 0;
- char prev = 0;
- int wordCount = 0;
- int upperCase = 0;
- int lowerCase = 0;
- int numDigits = 0;
- int punctuation = 0;
- /*
- While-loop loops until the condition is false, that is, until scanf returns a value of EOF (End of File)
- Scanf if is used to get input from the keyboard, the first parameter is "%c". which is basically the type,
- which in this case is a character type. The second parameter is the address of the variable, & means to get the
- address.
- */
- while(scanf("%c", &c) != EOF){
- // || means OR , && means AND
- //Want to check the if the CURRENT character is not a space, and the PREVIOUS character is a space
- //OR we want to check if the CURRENT character is a new line, and the previous is NOT whitespace
- //isblank is different from isspace, isspace returns true for any white space, including newline
- if(((!isspace(c)) && isblank(prev)) || (c == '\n' && !(isspace(prev)))){
- ++wordCount;
- }
- //Now we just update all the variables, checking using the functions
- if(isupper(c)){
- //++ means to add 1 to the value of the variable
- ++upperCase;
- }
- else if(islower(c)){
- ++lowerCase;
- }
- else if (isdigit(c)){
- ++numDigits;
- }
- else if (ispunct(c)){
- ++punctuation;
- }
- else{}
- prev = c;
- }
- //PRINT VARIABLES HERE*************************
- }
//QUESTION 3
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- int main(){
- char c = 0;
- char prev = 0;
- char *filename;
- FILE *fp;
- int wordCount = 0;
- int upperCase = 0;
- int lowerCase = 0;
- int numDigits = 0;
- int punctuation = 0;
- printf("Enter filename: ");
- scanf("%s", filename);
- if((fp = fopen(filename, "r")) == NULL){
- printf("Can't open %s\n", filename);
- }
- else{
- while((c = getc(fp)) != EOF){
- if(((!isspace(c)) && isblank(prev)) || (c == '\n' && !(isspace(prev)))){
- ++wordCount;
- }
- if(isupper(c)){
- ++upperCase;
- }
- else if(islower(c)){
- ++lowerCase;
- }
- else if (isdigit(c)){
- ++numDigits;
- }
- else if (ispunct(c)){
- ++punctuation;
- }
- else{}
- prev = c;
- }
- }
- //PRINT VARIABLES HERE*************************
- }
Learn More :
Assignment Question