Commit 137fe041 authored by kjc015's avatar kjc015
Browse files

Lab Complete

parent b0667bc1
...@@ -6,3 +6,5 @@ head2: head2.o fileio.o ...@@ -6,3 +6,5 @@ head2: head2.o fileio.o
$(CC) $(CFLAGS) head2.o fileio.o -o head2 $(CC) $(CFLAGS) head2.o fileio.o -o head2
head3: head3.o fileio.o head3: head3.o fileio.o
$(CC) $(CFLAS) head3.o fileio.o -o head3 $(CC) $(CFLAS) head3.o fileio.o -o head3
string_tokens: string_tokens.o
$(CC) $(CFLAS) string_tokens.o -o string_tokens
/* Kyle Chrysler
* Tuesday 1:20
* lab 04 - string_tokens.c
* compile with: string_tokens
* notes: none
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// reimplemented from man strtok
main(int argc, char *argv[])
char *token, *subtoken;
// saveptrs store the state for strtok
char *saveptr1, *saveptr2;
int token_num = 1;
if (argc != 4) {
fprintf(stderr, "Usage: %s string delim subdelim\n", argv[0]);
// initialize tokenization with argv[1] as str
token = strtok_r(argv[1], argv[2], &saveptr1);
while (token != NULL){
// while we have a token, first print it
printf("token %d: %s\n", token_num, token);
// then begin extracting subtokens from token
subtoken = strtok_r(token, argv[3], &saveptr2);
while (subtoken != NULL){
// print the subtoken
printf(" subtoken --> %s\n", subtoken);
// try to get next subtoken, using saveptr
subtoken = strtok_r(NULL, argv[3], &saveptr2);
// try to get next token, using saveptr
token = strtok_r(NULL, argv[2], &saveptr1);
token_num ++;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment