Question

In: Computer Science

Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char*...

Use the given Strings.c and Strings.h module:

Strings.c:

#include "Strings.h"
#include <string.h>
#include<stdlib.h>
#include<stdio.h>

char* substring(char* str, int iPos){

if(iPos > strlen(str)||iPos < 0)return (char*)NULL;
char* substr;
substr = &str[iPos];
return substr;
}

int charPosition(char* str, char c){
char* string = (char*)malloc(strlen(str)+1);
int i;
for(i = 0; i < strlen(str)+1; i++)
{
if(str[i] == c)
{
return i;
}
}
return -1;
}

Strings.h:

#include <string.h>

/* substring - return a pointer to the substring beginning at the iPos-th position.
* If iPos is out-of-range, return (char*)NULL
*/
char* substring(char* str, int iPos);
char* substringDisplay(char* str, int iPos);

/* charPosition - return the position of the first occurance of c in str.
* If c not present, return -1
*/
int charPosition(char* str, char c);

Write a function

char* fgetString(FILE* pFIn);

that reads the string length from an open file, allocates the memory and reads the string into it.

Write a test driver program that opens a file (file name from the command line arguments) and reads all strings from a file and writes them, one string per line, to standard out

Solutions

Expert Solution

1.Write a function

char* fgetString(FILE* pFIn);

that reads the string length from an open file, allocates the memory and reads the string into it.

2.Write a test driver program that opens a file (file name from the command line arguments) and reads all strings from a file and writes them, one string per line, to standard output

***********************

1.

#include <stdio.h>
#include <conio.h>

char* fgetString(FILE*);

int main()
{
FILE *f;
char *str;
f = fopen("testcontrol.txt", "w+");
fprintf(f, "A testing for fprintf...\n");
fputs("A testing for fputs...\n", f);
fclose(f);
char* fgetString(FILE* fp)
{

char buff[255];

fp = fopen("testcontrol.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s\n", buff );

fgets(buff, 255, (FILE*)fp);
printf("2: %s\n", buff );fgets(buff, 255, (FILE*)fp);
printf("3: %s\n", buff );
fclose(fp);
}

return 0;
}

********************

2.

#include<stdio.h>

#include<conio.h>

int main(int argc, char* argv[])

{

FILE *t;
t=fopen(argv[0],"r");
if(t==NULL)
{
printf("Can't open file%s",argv[0]);
printf("\n press any key to exit...");

exit();
}
while(!EOF)
{
fscanf(t,"%s", str);
printf("%s\n", str);
}
fclose(t);

return 0;

}

************************


Related Solutions

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to...
C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to rewrite the functions linked in the code: "return add_move_lib ( board, col, colour ) ;" "return board_full_lib ( board ) ;" "return display_board_lib ( board ) ; // TASKS // board_full() display_board() add_move() // adds a token of the given value (1 or 2) to the board at the // given column (col between 1 and COLS inclusive) // Returns 0 if successful, -1...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
*****MUST ONLY USE****** #include <iostream> #include <fstream> #include <string.h> #include <stdio.h> Description The word bank system...
*****MUST ONLY USE****** #include <iostream> #include <fstream> #include <string.h> #include <stdio.h> Description The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20. The system should support the following three functions: Word lookup: to check whether a given word exists in the word bank. Word insertion: to insert a...
Create C function for String.c and Strings.h that #include <string.h> /* substring - return a pointer...
Create C function for String.c and Strings.h that #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position. * If iPos is out-of-range, return (char*)NULL */ char* substring(char* str, int iPos); /* charPosition - return the position of the first occurance of c in str. * If c not present, return -1 */ int charPosition(char* str, char c);
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class: Introduction to C Programming 1057 Spring 2019 Section 004 * Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm." * Date: << 02-19-2019 >> * Version: 1 * Description: Program will prompt for some data, read in the values, perform the calculation using library math functions, and display the result. * File: lab5.c */ # define M_PI 3.14159265358979323846 /* pi */...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT