Write a program in C (when compiled has no warnings) that: Opens a file called M
ID: 3715019 • Letter: W
Question
Write a program in C (when compiled has no warnings) that:
Opens a file called MyQualities.txt for writing.
Repeatedly ...
Reads a sentence from the keyboard (using fgets).
Checks if the sentences starts with "I am " (using strstr)
If it does, then extracts the substring starting at the 5th character, i.e., the substring containing the quality the person believes they possess. (using strncpy).
Concatenates the substring onto a growing string containing all such qualities, with a comma separator (using strncat - the growing string has a maximal length of 1024 characters).
Appends the substring to the MyQualities.txt file.
Stops looping when a string containing just a "." is entered (check using strcmp).
Outputs the grown string of qualities to the screen.
Opens the MyQualities.txt file for reading
Reads and echo the qualities from the file
Deletes the file
Here's what a sample run should look like (with the keyboard input shown in italics) ...
Explanation / Answer
Below I had written the program which takes only one sentence and works on it, you can run it through loops for multiple statement intake,all the program is working fine
Note: Dont forget to create MyQualities.txt file at the same directory you write the program.
Please read it and understand carefully..
Hope this helps...
Thankyou.... :)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
//opening file
FILE *f;
f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file! ");
exit(1);
}
//intial statement
char name[100];
char quality[100];
printf("Enter sentence:");
fgets(name,100,stdin);
fprintf(f,"My qualities are ");
//checking if statement starts with I am
if(strstr(name,"I am")){
int i=5;
while(name[i]!=''){
//wiritng the quality if starts with I am
printf("%c",name[i]);
//writing to file
fprintf(f,"%c", name[i]);
i++;
}
}
fclose(f);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.