Question

In: Computer Science

In C programming, I am trying to search for the names of people that in this...

In C programming,

I am trying to search for the names of people that in this DOISigned.txt file, however I am having trouble getting the first and last names of the multiple people named john, my current code only searches for John once and then it terminates,here is my current code

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUF_SIZE 0x3000
char buf[BUF_SIZE];
int main()
{
   char* inputFile = "DOISigners.txt";
   FILE* fp;
   fp = fopen(inputFile, "r");
   if (!fp)
   {
       char errorMsg[256] = "Could not open error file ";
       strcat(errorMsg, inputFile);
       perror(errorMsg);
       getchar();
       exit(-1);
   }

   int size = fread(buf, sizeof(char), sizeof(buf), fp);
   buf[size] = '\0';                                              
   fclose(fp);

#define NUM_NAMES 3
   char* namesToSearchFor[NUM_NAMES] = { "John" };
   char* stringToSearch = "J";
   int length = strcspn(stringToSearch, ",");
   for (int i = 0; i < NUM_NAMES; i++)
   {
       char* start = strstr(buf, namesToSearchFor[i]);
       if (start != NULL)
       {
           printf("%s\n\n", namesToSearchFor[i]);
       }
   }
   getchar();
   exit(0);
}

and here is from the .txt file

Georgia: Button Gwinnett, Lyman Hall, George Walton

North Carolina: William Hooper, Joseph Hewes, John Penn

South Carolina: Edward Rutledge, Thomas Heyward, Jr., Thomas Lynch, Jr., Arthur Middleton

Massachusetts: John Hancock

Maryland: Samuel Chase, William Paca, Thomas Stone, Charles Carroll of Carrollton

Virginia: George Wythe, Richard Henry Lee, Thomas Jefferson, Benjamin Harrison, Thomas Nelson, Jr., Francis Lightfoot Lee, Carter Braxton

Pennsylvania: Robert Morris, Benjamin Rush, Benjamin Franklin, John Morton, George Clymer, James Smith, George Taylor, James Wilson, George Ross

Delaware: Caesar Rodney, George Read, Thomas McKean

New York: William Floyd, Philip Livingston, Francis Lewis, Lewis Morris

New Jersey: Richard Stockton, John Witherspoon, Francis Hopkinson, John Hart, Abraham Clark

New Hampshire: Josiah Bartlett, William Whipple

Massachusetts: Samuel Adams, John Adams, Robert Treat Paine, Elbridge Gerry

Rhode Island: Stephen Hopkins, William Ellery

Connecticut: Roger Sherman, Samuel Huntington, William Williams, Oliver Wolcott

New Hampshire: Matthew Thornton

Solutions

Expert Solution

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUF_SIZE 0x3000
char buf[BUF_SIZE];
int main()
{
char* inputFile = "DOISigners.txt";
FILE* fp;
fp = fopen(inputFile, "r");
if (!fp)
{
char errorMsg[256] = "Could not open error file ";
strcat(errorMsg, inputFile);
perror(errorMsg);
getchar();
exit(-1);
}

int size = fread(buf, sizeof(char), sizeof(buf), fp);
buf[size] = '\0';
fclose(fp);

#define NUM_NAMES 3

/**** Crash Was here Array size was 3 but ,You have defined only one string
* So we are accessing invalid index and crash/exit happened
*/

// Old code
//char* namesToSearchFor[NUM_NAMES] = { "John"};

//Define three names if you want to search all three
char* namesToSearchFor[NUM_NAMES] = { "John","Thomas","Treat"};


//Start
for (int i = 0; i < NUM_NAMES;)
{
//Start from file Buffer position 0
int wordLocation = 0;
printf("\n=== Searching word : %s ======= ",namesToSearchFor[i]);
while(1)
{

printf("\n");

//Find Matching first Substring
char* start = strstr(buf+wordLocation, namesToSearchFor[i]);

if(start == NULL) break;

//find Position in buffer where Matching substring found
wordLocation += start - (buf+wordLocation);

//Read Position is different then wordLocation
//Example Word "Treat"
//in file for name " Robert treat Paine "
//wordLocation = 't'
//readPosition = 'R'
//Output = Robert treat Paine
int readPosition = wordLocation;

//If word is middle name than go to start of word
while(1)
{
//Go Backword at starting of word
//until EOF NewLine or , not found
char ch = buf[readPosition];

if(ch == EOF ||
ch == '\n' ||
ch == ':' ||
ch == ',')
{
//+2 to overcome space before each word
readPosition+=2;
break;
}
readPosition--;
}

int charPosition = 0;
while(1)
{
//Read and find each charator in order J h o n until EOF NewLine or , not found
char ch = buf[readPosition+charPosition];
if(ch == EOF ||
ch == '\n' ||
ch == ',')
{
break;
}

charPosition++;
printf("%c",ch);
}
//Move to next occurance of that word in file
wordLocation+=charPosition;
}
//Move to new word
i++;
}

printf("\n============ All search Completed=============\n\t");
fflush(stdout);
getchar();
exit(0);
}


/**
Output

=== Searching word : John =======
John Penn
John Hancock
John Morton
John Witherspoon
John Hart
John Adams

=== Searching word : Thomas =======
Thomas Heyward
Thomas Lynch
Thomas Stone
Thomas Jefferson
Thomas Nelson
Thomas McKean

=== Searching word : Treat =======
Robert Treat Paine

============ All search Completed=============

**/


Related Solutions

I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX INPUT: B C D A OUTPUT: D C B A #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]) {         int MAX = 100000;         int i =0;         int k =0;         int j =0;         char array[MAX];         char split[] = " ,.-!?()0123456789";         int n = 0;         char second[MAX];         printf("Please enter in a String: ");...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 2? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 3? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
I am trying to solve a c++ problem over c strings where I have to input...
I am trying to solve a c++ problem over c strings where I have to input an email address and check if it is valid. After completing my code I keep getting errors that my program will not run, specifically the lines with my for loops. Can you please look at my code and tell me what is wrong and how I can fix the code? I included below the assignment instructions and my current code. Assignment Instructions Write a...
I am trying to write a code in C for a circuit board reads in a...
I am trying to write a code in C for a circuit board reads in a temperature from a sensor on the circuit board and reads it onto a 7-segment LED display. How exactly would you code a floating 2 or 3 digit reading onto the LED display? I am stuck on this part.
I am trying to write code for a program in Visual Studo using Visual Basic programming...
I am trying to write code for a program in Visual Studo using Visual Basic programming language that computes the factorial of an entered number by using a For Loop. My problem is that it keeps re-setting the variable for Factorial. Below is my code if anyone can help. I want it to multiply the given number by that number - 1, then continuing to do so until it hits zero without multiplying by zero. Private Sub BtnCalculate_Click(sender As Object,...
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
I am trying to create a makefile for the following program in Unix. The C++ program...
I am trying to create a makefile for the following program in Unix. The C++ program I am trying to run is presented here. I was wondering if you could help me create a makefile for the following C++ file in Unix and show screenshots of the process? I am doing this all in blue on putty and not in Ubuntu, so i don't have the luxury of viewing the files on my computer, or I don't know how to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT