Question

C programming language question. Supposed that there is a txt file with content "AA101 ABC ADC...

C programming language question.

Supposed that there is a txt file with content "AA101 ABC ADC 2019-11-24", I want to read the information by blank space.

When program read the file, I want the program only print out ABC and ADC.

Please show the code.

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

//Rate my solution

//Comment if any doubts

CODE:

#include<stdio.h>
#include<stdlib.h>
main(){
   char c,*str;
   int index,flag;
   // to open a file we need a file pointer
   FILE *fp;
   // openinig the file f.txt you change f.txt to your filename.txt
   // r is the mode of the file i.e., to read the contents of file f
   fp=fopen("f.txt","r");
   do{
       // getting each character in the file
       c=fgetc(fp);
       // this is the memory allocation for the string we want to print
       str=(char *)malloc(100*sizeof(char));
       // index is the index for storing in the str
       index=0;
       // flag is used to check whether it has any non aplhabets
       flag=1;
       // this loop is for checking the string upto space or newline or End of file
       while(c!=' ' && c!=EOF && c!='\n'){
           // if the character in the file is not an alphabet then falg is zero
           if(!isalpha(c))
               flag=0;
           else
               str[index++]=c; //storing the aplhabet in the str and incrementing the index
           // it works as
           // str[index]=c;
           // index=index+1;  
           //getting next character int the file      
           c=fgetc(fp);  
       }
       //the last value of every string is \0 it means the string has ended
       str[index]='\0';
       // flag 1 says that it has only alphabets
       // index 0 means empty string so index should be not zero
       if(flag==1 && index!=0)
           printf("%s\n",str);
       // clearing the space created by the str for new string
       free(str);
      
   }while(c!=EOF);
}

Code(Screenshot):

File (f.txt):

Output:

Add a comment
Know the answer?
Add Answer to:
C programming language question. Supposed that there is a txt file with content "AA101 ABC ADC...
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
  • programming language is C++. Please also provide an explanation of how to create a file with...

    programming language is C++. Please also provide an explanation of how to create a file with the given information and how to use that file in the program Let's consider a file with the following student information: John Smith 99.0 Sarah Johnson 85.0 Jim Robinson 70.0 Mary Anderson 100.0 Michael Jackson 92.0 Each line in the file contains the first name, last name, and test score of a student. Write a program that prompts the user for the file name...

  • Please provide C language code no c++ ,txt file also needed with comment Finish task 5...

    Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...

  • C++ programming help. The only change you have to make is in current program, where it...

    C++ programming help. The only change you have to make is in current program, where it says "your code goes here", do not make any changes to any other files, they are just there to show you. This code should be written for arbitrary data, not specifically for this sequence of data. The only place you need to write a code is marked YOUR CODE GOES HERE. You have been supplied a file with data in it (data2.txt). The line...

  • Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space....

    Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and prints how many numbers are prime between them. Your program must have a user defined function that takes one number as input parameter and detects whether its prime or not. Show your output in a file named B2.txt using the following format: Sample Input: 3 17 42 91 Sample Output: 6...

  • For this assignment - you will be writing a program (you may choose whatever programming language...

    For this assignment - you will be writing a program (you may choose whatever programming language you want) to read in two of your database tables from Lab 5, and then print out the contents of each of the two tables separately. These MUST be database tables that have already been created and stored as input for your program. You may use either SQL code to select All of your tables and do a screen capture. Turn in a copy...

  • I am writing a program in c programming and it is supposed to do the following using mc9s12dg256.h microcontroller When...

    I am writing a program in c programming and it is supposed to do the following using mc9s12dg256.h microcontroller When both DIP switches #8 and #1 are high, turn on all LEDS. When both DIP switches #8 and #1 are low, turn off all LEDs When DIP switch #8 is high and #1 is low, turn on all the even numbered LEDs. When DIP switch #1 is high and #8 is low, turn on all the odd numbered LEDs. Your...

  • C Programming Language on Linux - Word Frequency Program Please write a Program in C that...

    C Programming Language on Linux - Word Frequency Program Please write a Program in C that will accept a text file name as a command-line argument via a main program that will do the following: First, read the file (first pass) and create a linked list of words (in their order of occurrence), with the frequency of each word set to 0. Then, read the file (second pass) and for each word identified, search the linked list, and when found,...

  • Question II This question carries 50% of the marks for this assignment. This question is about...

    Question II This question carries 50% of the marks for this assignment. This question is about avoiding race condition by using synchronization mechanism provided by the JAVA programming language. We provided you with a Netbeans JAVA project. The project idea is as follows: we want to merge the content of four files into a single file, as follows. Below is the content of all four files: F1 content: 1 1 1 1 F2 content: 2 2 2 2 F3 content:...

  • C Language program problem. Suppose that I want to create a program by using command line....

    C Language program problem. Suppose that I want to create a program by using command line. User will type a name of file such as "test.data" to access the content of the file. Program should scan whole file and create a list with 30 position . You should show the two situation, one is for when scanned space, the other one is when it scanned new line. Then the other task is printing them out. You see there are spaces...

  • Hi, please provide a code solution written in the C++ language to the following programming problem....

    Hi, please provide a code solution written in the C++ language to the following programming problem. The answer you provide has to include code comments, as it is being explained to a person with no technical background. In your answer, please show which portion of the code is a .cpp and which is a .h (if used). We are asked to separate them out (ie. main.cpp, classname.cpp, headername.h). Please show the output of your program to show it compiled error...

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