Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cs283
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Joey Le
Cs283
Commits
690eec90
Commit
690eec90
authored
10 months ago
by
Joey Le
Browse files
Options
Downloads
Patches
Plain Diff
Finished all of the TODOs
parent
ab1fef64
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
.vscode/settings.json
+2
-1
2 additions, 1 deletion
.vscode/settings.json
Assignment-01/a.exe
+0
-0
0 additions, 0 deletions
Assignment-01/a.exe
Assignment-01/stringfun.c
+154
-9
154 additions, 9 deletions
Assignment-01/stringfun.c
with
156 additions
and
10 deletions
.vscode/settings.json
+
2
−
1
View file @
690eec90
...
...
@@ -55,5 +55,6 @@
"C_Cpp_Runner.useLeakSanitizer"
:
false
,
"C_Cpp_Runner.showCompilationTime"
:
false
,
"C_Cpp_Runner.useLinkTimeOptimization"
:
false
,
"C_Cpp_Runner.msvcSecureNoWarnings"
:
false
"C_Cpp_Runner.msvcSecureNoWarnings"
:
false
,
"C_Cpp.errorSquiggles"
:
"disabled"
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Assignment-01/a.exe
+
0
−
0
View file @
690eec90
No preview for this file type
This diff is collapsed.
Click to expand it.
Assignment-01/stringfun.c
+
154
−
9
View file @
690eec90
...
...
@@ -16,9 +16,49 @@ int count_words(char *, int, int);
int
setup_buff
(
char
*
buff
,
char
*
user_str
,
int
len
){
//First create a buffer and checks if the memory will be allocated properly
buff
=
malloc
(
BUFFER_SZ
);
if
(
!
buff
)
{
return
-
2
;
}
//Variables to help move through the string and also count length
char
*
buff_ptr
=
buff
;
char
*
str_ptr
=
user_str
;
int
white_space_checker
=
0
;
int
length_of_string
=
0
;
while
(
*
str_ptr
!=
'\0'
)
{
if
(
*
str_ptr
==
' '
||
*
str_ptr
==
'\t'
)
{
if
(
white_space_checker
==
0
)
{
//I know nested if's are bad but it's the onl way I could think of
*
buff_ptr
++
=
' '
;
// Using whitespace checker to determine to add it to the buffer or not
length_of_string
++
;
white_space_checker
=
1
;
}
}
else
{
*
buff_ptr
++
=
*
str_ptr
;
length_of_string
++
;
white_space_checker
=
0
;
}
if
(
length_of_string
>=
BUFFER_SZ
)
{
//Checks if the length is past the buffer or not
free
(
buff
);
return
-
1
;
return
0
;
//for now just so the code compiles.
}
str_ptr
++
;
}
while
(
length_of_string
<
BUFFER_SZ
)
{
//To fill up buffer with .
*
buff_ptr
++
=
'.'
;
length_of_string
++
;
}
return
length_of_string
;
}
void
print_buff
(
char
*
buff
,
int
len
){
...
...
@@ -35,9 +75,79 @@ void usage(char *exename){
}
int
count_words
(
char
*
buff
,
int
len
,
int
str_len
)
{
//YOU MUST IMPLEMENT
return
0
;
if
(
buff
==
NULL
||
str_len
<=
0
)
{
//Makes sure it valid
printf
(
"Error: Invalid input.
\n
"
);
return
-
1
;
}
char
*
buff_ptr
=
buff
;
int
words_in_string
=
0
;
while
(
*
buff_ptr
!=
'\0'
)
{
if
(
*
buff_ptr
==
' '
)
{
//I assume that space is a new word so it checks for that everytime
words_in_string
++
;
//If it's not space it keeps reading
}
buff_ptr
++
;
printf
(
"Word Count %d
\n
"
,
words_in_string
);
return
words_in_string
;
}
}
int
reverse_words
(
char
*
buff
,
int
len
,
int
str_len
)
{
if
(
buff
==
NULL
||
str_len
<=
0
)
{
printf
(
"Error: Invalid input.
\n
"
)
;
return
-
1
;
}
char
temp
;
int
start
=
0
;
int
end
=
str_len
-
1
;
while
(
start
<
end
)
{
//Its like a circle that flips the word around
temp
=
buff
[
start
];
//My first idea was to make a another array, but that would have took to much time
//Then I remebered about temp varaibles
buff
[
start
]
=
buff
[
end
];
buff
[
end
]
=
temp
;
start
++
;
end
--
;
}
buff
[
str_len
]
=
'\0'
;
//Ends the string so the buffer thingy is not printed
printf
(
"Reversed String: &s
\n
"
,
buff
);
}
int
write_string
(
char
*
buff
,
int
len
,
int
str_len
)
{
printf
(
"Word Print
\n
----------
\n
"
);
char
*
buff_ptr
=
buff
;
char
word
[
BUFFER_SZ
];
int
word_count
=
0
;
int
line_number
=
1
;
int
line_position
=
0
;
//I use a loop to print out each line one at a time
while
(
*
buff_ptr
!=
'\0'
)
{
if
(
*
buff_ptr
!=
' '
)
{
word
[
line_position
]
=
buff_ptr
[
line_position
];
word_count
++
;
}
else
{
word
[
line_position
]
=
'\0'
;
//Resets the word
printf
(
"%i. %s (%i)"
,
line_number
,
word
,
word_count
);
line_number
++
;
line_position
=
0
;
word_count
=
0
;
//reset word counter
}
*
buff_ptr
++
;
}
}
//ADD OTHER HELPER FUNCTIONS HERE FOR OTHER REQUIRED PROGRAM OPTIONS
...
...
@@ -51,6 +161,13 @@ int main(int argc, char *argv[]){
//TODO: #1. WHY IS THIS SAFE, aka what if arv[1] does not exist?
// PLACE A COMMENT BLOCK HERE EXPLAINING
// argv is a way to split a string up into seperate pieces
//The purpose of argv[0] is that it allows you to call a certain part of the command line using
//A bracket and number
//This is a way to be able to pass on arguements into the function
// and it's make the program a lot easier to work with for developers and users alike
if
((
argc
<
2
)
||
(
*
argv
[
1
]
!=
'-'
)){
usage
(
argv
[
0
]);
exit
(
1
);
...
...
@@ -67,7 +184,8 @@ int main(int argc, char *argv[]){
//WE NOW WILL HANDLE THE REQUIRED OPERATIONS
//TODO: #2 Document the purpose of the if statement below
// PLACE A COMMENT BLOCK HERE EXPLAINING
// The purpose of this is that it checks how many arguements are passed into it to make sure that
// It has the arguements required to function properly. If it's less than 3 then it exits the program
if
(
argc
<
3
){
usage
(
argv
[
0
]);
exit
(
1
);
...
...
@@ -80,6 +198,10 @@ int main(int argc, char *argv[]){
// return code of 99
// CODE GOES HERE FOR #3
buff
=
(
char
*
)
malloc
(
BUFFER_SZ
*
sizeof
(
char
));
if
(
buff
==
NULL
)
{
printf
(
"Error: Memory buggin fwah "
);
}
user_str_len
=
setup_buff
(
buff
,
input_string
,
BUFFER_SZ
);
//see todos
if
(
user_str_len
<
0
){
...
...
@@ -99,15 +221,31 @@ int main(int argc, char *argv[]){
//TODO: #5 Implement the other cases for 'r' and 'w' by extending
// the case statement options
case
'r'
:
rc
=
reverse_words
(
buff
,
BUFFER_SZ
,
user_str_len
);
if
(
rc
<
0
)
{
printf
(
"Error reversing words, rc = $dn
\n
"
,
rc
);
exit
(
2
);
}
case
'w'
:
rc
=
write_string
(
buff
,
BUFFER_SZ
,
user_str_len
);
if
(
rc
<
0
)
{
printf
(
"Error writing string, rc = %d
\n
"
,
rc
);
exit
(
2
);
default:
usage
(
argv
[
0
]);
exit
(
1
);
break
;
}
//TODO: #6 Dont forget to free your buffer before exiting
print_buff
(
buff
,
BUFFER_SZ
);
free
(
buff
);
exit
(
0
);
}
}
//TODO: #7 Notice all of the helper functions provided in the
// starter take both the buffer as well as the length. Why
...
...
@@ -115,4 +253,11 @@ int main(int argc, char *argv[]){
// is a good practice, after all we know from main() that
// the buff variable will have exactly 50 bytes?
//
// PLACE YOUR ANSWER HERE
\ No newline at end of file
// I think it's good practice, because having both the pointer
// passing the buffer size everything is important, because
// then it makes the code more readable and also it can make it
// so that it will be a bit easier to reused in the future
// because you can put in different buffers and stuff and
// you can prevent a lot of errors, because you can check the size
// of the buffer whenever.
//
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment