Question

write program in C language.

To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns the contents of the file as an array of strings. Each element in the array should correspond to a line in the file. Any end line character should be chomped out and not included char **getFileLines (const char *filePath, int *numLines);

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

string_utils.h

#ifndef _STRING_UTILS_h
#define _STRING_UTILS_h
#include <string.h>

void replaceChar(char *,char oldChar,char newChar);
char *replaceCharCopy(const char *,char oldChar,char newChar);
void removeChar(char *,char);
char * removeCharCopy(const char * ,char);
char **lengthSplit(const char*,int);
#endif

string_utlis.c

#include <string.h>

#include<stdio.h>

#include<stdlib.h>

#include"string_utils.h"

void replaceChar(char * s,char oldChar,char newChar)

{

int i;

for(i=0;i<strlen(s);i++)

{

if(s[i]==oldChar)

s[i]=newChar;

}

puts(s);

}

char *replaceCharCopy(char *s,char oldChar,char newChar)

{

char *copyOfs;

int i;

copyOfs = (char*) malloc (sizeof (char) * strlen(s));

for(i=0;i<strlen(s);i++)

{

if(s[i]==oldChar)

copyOfs[i]=newChar;

else

copyOfs[i]=s[i];

}

copyOfs[i]='\0';

return copyOfs;

}

void removeChar(char*s ,char c)

{

int i,j;

for (j=i=0;i<s[i]!='\0';i++)

if (s[i] != c)

s[j++] = s[i];

  

s[j] = '\0';

printf("\n\nAfter removing charcter %c string is: ",c);

puts(s);

}

char *removeCharCopy(const char *s ,char c)

{

char *copyOfs;

int i,j;

copyOfs = (char*) malloc (sizeof (char) * strlen(s));

for (j=i=0;i<s[i]!='\0';i++)

if (s[i] != c)

copyOfs[j++] = s[i];

copyOfs[j]='\0';

return copyOfs;

}

stringTester.c

#include <string.h>

#include<stdio.h>

#include"string_utils.h"

int main()

{

char s[50]="Hj my name js BOM.",*newString;

replaceChar(s,'j','i');

printf("\nAfter replacing j with i copy of string is: ");

newString=replaceCharCopy(s,'j','i');

printf("%s",newString);

removeChar(s,'.');

newString=removeCharCopy(s,'.');

printf("\n\nAfter removing . from the string: %s",newString);

return 0;

}

output

uploding the last fucntion it is vary deficult to implement so please wait i am doing it please do no thumbs down there is time factor so please wait. thank you.

Add a comment
Know the answer?
Add Answer to:
write program in C language. To get more practice working with files, you will write several...
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
  • 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...

  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[],...

    Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[], char destination[], int n); This function copies string source to string destination. Parameter n represents the size of array destination. If the latter array is not sufficiently large to hold the whole source string then only the prefix of the string which has room in the latter array should be copied. Note that after copying, the null character should also be included to mark...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • Assignment Overview In Part 1 of this assignment, you will write a main program and several...

    Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...

  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

  • Reading and Writing Complete Files in C: The first part of the lab is to write...

    Reading and Writing Complete Files in C: The first part of the lab is to write a program to read the complete contents of a file to a string. This code will be used in subsequent coding problems. You will need 3 functions: main(), read_file() and write_file(). The main function contains the driver code. The read_file() function reads the complete contents of a file to a string. The write_file() writes the complete contents of a string to a file. The...

  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

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