Question

PART A Write c program.Please dount use fget Write the statements to read a line of...

PART A

Write c program.Please dount use fget

Write the statements to read a line of characters and create a new string by eliminating commas and blanks in the input string. Display the new string as given below:

Sample Run:

Enter a string: today, tomorrow, next day, will be cold.

Next string: todaytomorrownextdaywillbecold.

PART B

Please dont use fget.

Write a complete C program that removes extra spaces between words. Your main function should read the string entered by the user (you shall use gets function) and passes it to a function called clean. The function called clean should create a new string consisting of characters with no extra spaces between words. The main function should print this new string. A sample run is provided below:

Sample Run 1:

Enter source characters:hello world.

Result:hello world

Sample Run 2:

Enter source characters:hello my world.

Result:hello my world

Sample Run3:

Enter source characters:hello world.

Result:hello world

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey here is answer to your question.

#include<stdio.h>
int Clean(char s[]){

   // looping over the character array
   // one note : we will loop until we reach the "\0" ending of string
   // as looping over the coundary will be waste of time as its no use to use
   // we only need the string we input
   for (int i = 0; s[i]!='\0'; i++){
       // now we check if character array element is "," or "." or tab("\t")
       // if its true dont print anything
       if (s[i]=='\t'|| s[i]==','|| s[i]=='.')
           printf("");
       // now for multiple blanks
       // we check if 2 consicative chars are white space then print nothing
       else if (s[i]==' ' && s[i+1]==' ')
           printf("");
       // now if there is a space and just next to it is a char other then space then it will print only one blank space
       else
           printf("%c",s[i]);
   }
  
   // just printing a new line at the end to make sure output is looking good  
   printf("\n");

}
void OutputWithoutWhitespceAndComma(char s[]){

   // this loop is also same as explained in upper function
   // with a little change
   for (int i = 0; s[i]!='\0'; i++)
       // now we check if character array element is whitespace or tab("\t")
       // if it is a whitespace print nothing
       if (s[i]==' ' || s[i]=='\t'|| s[i]==','|| s[i]=='.')
           printf("");
       //if it is not a white space then print the character
       else
           printf("%c",s[i]);
   // just printing a new line at the end to make sure output is looking good  
   printf("\n");      
}
int main(int argc, char const *argv[])
{  
   char s[2000]; // sample arrray to store input string
   char s2[2000];
   // now this is the trick in scanf of "C" if we dont put a parameter
   // it will use white space as limit upto where it have to read line
   // exaple "scanf("%s",s)" with input "hi there" will only read upto "hi"
   // so we give a parameter in "[]" so it will read upto that
   // in our case it will read upto newline("\n") character

   // Part A
   printf("Enter a string: ");
   scanf("%[^\n]s",s);
   printf("Next string: ");
   // printing wiout white spaces, Comma and blank.
   OutputWithoutWhitespceAndComma(s);

   // this line is for solveing the danglling new line problem
   //(if you want to test that issue comment the next line and run the program again)
   while ((getchar()) != '\n');

   //Part B
   puts("Enter source :");
   gets(s2);
   puts("Result:");
   Clean(s2);

   return 0;
}

In case of any doubt please comment. Happy Learning :)

note:- when compile this will give "Warning" that we should not use gets function but its ok as its not an error

Add a comment
Know the answer?
Add Answer to:
PART A Write c program.Please dount use fget Write the statements to read a line of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line...

    Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line is read, the program strips out all characters that are not upper or lower case letters or spaces, and then outputs the line. Thus, the program acts as a filter and issues no prompt. There are many ways this program could be written, but to receive full credit, you must observe the following: Place your code in a file called filterChars.cpp. The program should...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • •Write use pytohn a function File2List(filename) that: –Read line by line from a file –For each...

    •Write use pytohn a function File2List(filename) that: –Read line by line from a file –For each line of words, –change all characters to lowercase –remove all the punctuations –remove whitespaces except the blank – ‘ ’ –split the words within the line by the blank and form a list of separated words –Merge all the list of separated words together and the function should return a wordlist of individual words

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • C++ code please asap QUESTIONS Write the test main function as described next. This function is...

    C++ code please asap QUESTIONS Write the test main function as described next. This function is NOT a member function of either of the two classes above. It is located in separate source file. Create an array of type Flight and size 10 1. Prompt the user to enter values for speed, name, and id for all 10 objects. You must use the overloaded input stream operator of class Flight. 1. Write the value of the name variable of all...

  • Write a C program that parses a string (command line) and tokenize it by breaking the...

    Write a C program that parses a string (command line) and tokenize it by breaking the string characters into words that are separated by delimiters that can be white spaces (space and/or tab characters). It also must report how many commands in the input. This program will be reused for your next C programming assignment that is a simple shell interpreter/program called "tech shell". % a.out please enter a string: this is a test 1: this 2: is 3: a...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. 1. write the code using function 2. Please try to implement a function after the main function and provide prototype before main function. Total Characters in String Array 10 points Problem 2 Declare a string array of size 5. Prompt the user enter five strings that are...

  • I'm a bit confused on how to get this program to run right. Here are the...

    I'm a bit confused on how to get this program to run right. Here are the directions: Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed. For example, ‘This line has extra space characters ‘  ‘This line has extra space characters’ Function name: reduceWhitespace Number of parameters: one string line Return value: one string line The main file should handle the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT