Professional Documents
Culture Documents
Cs 270 Assignment 1
Cs 270 Assignment 1
Cs 270 Assignment 1
Hunter Brazee
February 15th, 2024
1 Program Design
This program parses a string into tokens (words) and is similar to what a shell
does. It uses the function makearg, and accepts a string and a pointer to a
pointer to char. It returns the pointer defined to point to an array pointing to
the separate tokens, and how many tokens are in the string.
2 What is Needed
1. Read the input line
2. Call the function to parse the string
3. Count the number of words
4. Store each word in memory using array
5. Print the number of words found
6. In the main function, print the separate words
7. Return -1 as the value if problem occured in the function
3 Programming Log
3.1 Sunday 02/11
I started the project today by setting up the basics for the main function, and
the string input. I started on the makearg function, and created two pointers
that were set to the char *s, and these will be used for looping through the string
and adding the parsed words into memory. I also set up the string traversal and
set up the word counter, which counts each time there is a space in between
words. I spent around 1 1/2 hours working on the project today.
1
3.2 Monday 02/12
Today I just had to finish the makearg function, so I started by implementing
the two pointers, and used the first to traverse the string and used the second
pointer is used to add each word to the array of memory, then the word count
total is returned. Finally I finished the printing of each word separately and the
total number of words, then used free to free up the memory used. I think I
spent around 2 1/2 hours working on the assignment today, and in total worked
on it for about 4.
4 Output
Script started on 2024-02-14 14:51:57-08:00
bash-4.4$ gcc prog1.^H^[[K^H^[[K1.c
bash-4.4$ ./a.out
Enter your line of text: ls -l file
Number of words: 3
Words:
1: ls
2: -l
3: file
bash-4.4$ g^H^[[K./a.out
Enter your line of text: This is a string for assignment 1
Number of words: 7
Words:
1: This
2: is
3: a
4: string
2
5: for
6: assignment
7: 1
bash-4.4$ exit^H^[[K^H^[[K^H^[[K^H^[[K^Gexit
exit
5 Program Files
5.1 prog1.c
//Hunter Brazee
//Assignment 1
//This program has makearg function that takes a string and a pointer to a pointer
//defined to point to an array pointing to the separate tokens(words) on the command line
//It prints the tokens(words) as well as the number of tokens found
#include <stdio.h>
#include <stdlib.h>
int main() {
char input[256]; //Storage for user input
char **argv;
int argc;
3
for (int i = 0; i < argc; i++) { //Frees the memory
free(argv[i]);
}
free(argv);
return 0;
}
//Function that takes string into separate words and counts #
int makearg(char *s, char ***args) {
int word_num = 0; //Counter for # of words
char *p1 = s; //Pointer for looping the string
char *p2 = s; //Pointer for adding the words to the memory
while (*p1 != ’\0’) { //Loops through the input and counts # of words
// Allocate memory for each token and copy it into the array
p1 = p2; // Reset p1 to the front of the input string
for (int i = 0; i < word_num; i++) {
while (*p1 == ’ ’) { //Skips spaces
p1++;
}
4
if ((*args)[i] == NULL) {
return -1; // If problem occured during operation
}
for (int j = 0; j < p1 - p2; j++) { //Adds each word to the array
(*args)[i][j] = p2[j];
}
}
return word_num;
}