Cs 270 Assignment 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

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.

1.1 General Operations


1. Input C-style string from the user.
2. Call the function of makearg
3. Prints the number of arguements (tokens) found in the string
4. Display the arguements with one per line

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.

3.3 Things Encountered/Learned


Things I encountered while working on the assignment is that strtok() would
have been a very easy way to parse the words, and I had to do some research and
figure out how to manually parse the words. I also encountered that realizing
how many * are being used is very important and keeping it consistent helps
the program actually work correctly. I also learned that allocating memory is
very important, and caused me many problems at first before I realized that
it was never allocated. This assignment was very interesting for me because it
is one of the first times having to use C for an assignment and not C++, and
trying to get used to the little things like printf instead of cout. It felt weird at
first being so used to C++, however thankfully the main functionality is very
similar to C++, so I became more comfortable as I kept working. Also just a
general catching myself of mixing up names, like accidentally using uppercase
then lowercase, or using an underline or a dash.

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

Script done on 2024-02-14 14:53:02-08:00

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 makearg(char *s, char ***args);

int main() {
char input[256]; //Storage for user input
char **argv;
int argc;

printf("Enter your line of text: ");


fgets(input, sizeof(input), stdin); //Gets input string from user

argc = makearg(input, &argv); //Sets argc to the makearg function


if (argc == -1) { //If return is -1 and error occured
printf("Error: Memory allocation failed.\n");
return 1;
}

printf("Number of words: %d\n", argc); //Prints the # and words


printf("Words:\n");
for (int i = 0; i < argc; i++) {
printf("%d: %s\n", i + 1, argv[i]);
}

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

while (*p1 == ’ ’) { //Moves over spaces


p1++;
}
if (*p1 != ’\0’) {
word_num++; // Increment word count for each word
}

while (*p1 != ’ ’ && *p1 != ’\0’) {//Moves p1 to end of word


p1++;
}
}
// Allocate memory for the array of pointers to tokens
*args = (char **)malloc(sizeof(char *) * word_num); //
if (*args == NULL) {
return -1; // Memory allocation failed
}

// 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++;
}

p2 = p1; //Moves p2 to next word

while (*p1 != ’ ’ && *p1 != ’\0’) { //Moves p1 to end of the word


p1++;
}

(*args)[i] = (char *)malloc(sizeof(char) * (p1 - p2 + 1)); //Allocate current word

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;
}

You might also like