Thread: Remove first few words from a string

  1. #1
    Registered User
    Join Date
    Nov 2020

    Remove first few words from a string

    I am trying to read in a string from a file (easy enough). But I am then trying to remove the first word or two to be able to save only the trailing information.

    Example of inFile text would be:
    Agency Name: ABCDEFG
    Agency POC: Mr. XYZ
    Where all I want to save to my struct is the ABCDEFG and Mr. XYZ (for different variables).

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    The Round Table
    Scan past the colon and leading whitespace, then copy the rest into a buffer. The strchr() function will take care of the first bit of the task:

    #include <stdio.h>
    #include <string.h>
    int main(void)
     char* text = "Agency Name: ABCDEFG";
     char* colon = strchr(text, ':');
     if(colon != NULL)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Is the ":" a useful delimiter?

    If so, strchr() is your friend.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2020
    I'm thinking that the colon might not be a good delimiter based on a need to provide a time example. I've attached what the input file would look like. I'm thinking of changing the ':' to a '-' to try to make use of strchr().
    Agency Name: Test
    Agency POC: Mr. Test
    POC Phone: 12345
    POC Email: [email protected]
    Aircraft Type: B747
    Aircraft Tail: 0000
    Aircraft Callsign: HVY123
    Arrival Date(**dd/mm/yyyy): 01/01/2021
    Arrival Time(**HH:MM Zulu): 15:45
    Previous ICAO: KMCO
    Departure Date(**dd/mm/yyyy): 01/01/2021
    Departure Time(**HH:MM Zulu): 19:00
    Next ICAO: KADW
    Inbound Passengers(Number): 247
    Inbound DV(Number): 0
    Inbound Cargo(Details): None
    Inbound HAZMAT(Details): None
    Outbound Passengers(Number): 240
    Outbound DV(Number): 17
    Outbound Cargo(Details): None
    Outbound HAZMAT(Details): None
    In my program code, I'm reading in each line using fgets and then a function to remove the trailing new line character.
    fgets(new->AgencyInfo.AgencyName, 100, inFile);
    Could I use the strchr function on the struct element?

  5. #5
    Registered User
    Join Date
    Nov 2020
    I believe I found it!
    fgets(buf, 100, inFile);removeNL(buf);
    token = strtok(buf, s);
    t = strtok(NULL," ");
    strcpy(new->AgencyInfo.AgencyName, t);
    Thank you for your help getting me looking in the right direction!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using indexof to remove first two words of string
    By jeremy duncan in forum C# Programming
    Replies: 3
    Last Post: 08-05-2017, 04:17 PM
  2. Replies: 3
    Last Post: 12-10-2012, 12:24 AM
  3. Replies: 2
    Last Post: 07-23-2011, 10:26 PM
  4. Replies: 7
    Last Post: 10-01-2010, 04:09 PM
  5. remove duplicate words from linked list
    By rocketman03 in forum C Programming
    Replies: 8
    Last Post: 11-22-2008, 07:47 PM

Tags for this Thread