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
dd58c67d
Commit
dd58c67d
authored
8 months ago
by
jl4589
Browse files
Options
Downloads
Patches
Plain Diff
Tried moving it into tux and they're were lots of bugs
parent
690eec90
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Assignment-01/makefile
+20
-0
20 additions, 0 deletions
Assignment-01/makefile
Assignment-01/stringfun
+0
-0
0 additions, 0 deletions
Assignment-01/stringfun
Assignment-01/stringfun.c
+65
-45
65 additions, 45 deletions
Assignment-01/stringfun.c
with
85 additions
and
45 deletions
Assignment-01/makefile
0 → 100644
+
20
−
0
View file @
dd58c67d
# Compiler settings
CC
=
gcc
CFLAGS
=
-Wall
-Wextra
-g
# Target executable name
TARGET
=
stringfun
# Default target
all
:
$(TARGET)
# Compile source to executable
$(TARGET)
:
stringfun.c
$(
CC
)
$(
CFLAGS
)
-o
$(
TARGET
)
$^
# Clean up build files
clean
:
rm
-f
$(
TARGET
)
# Phony targets
.PHONY
:
all clean
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Assignment-01/stringfun
0 → 100755
+
0
−
0
View file @
dd58c67d
File added
This diff is collapsed.
Click to expand it.
Assignment-01/stringfun.c
+
65
−
45
View file @
dd58c67d
...
...
@@ -8,21 +8,20 @@
//prototypes
void
usage
(
char
*
);
void
print_buff
(
char
*
,
int
);
int
setup_buff
(
char
*
,
char
*
,
int
);
int
setup_buff
(
char
*
*
,
char
*
,
int
);
//prototypes for functions to handle required functionality
int
count_words
(
char
*
,
int
,
int
);
int
count_words
(
char
*
,
int
);
//add additional prototypes here
int
setup_buff
(
char
*
buff
,
char
*
user_str
,
int
len
){
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
);
*
buff
=
(
char
*
)
malloc
(
len
);
if
(
!
buff
)
{
return
-
2
;
}
//Variables to help move through the string and also count length
char
*
buff_ptr
=
buff
;
char
*
buff_ptr
=
*
buff
;
char
*
str_ptr
=
user_str
;
int
white_space_checker
=
0
;
...
...
@@ -45,7 +44,7 @@ int setup_buff(char *buff, char *user_str, int len){
}
if
(
length_of_string
>=
BUFFER_SZ
)
{
//Checks if the length is past the buffer or not
free
(
buff
);
free
(
*
buff
);
return
-
1
;
}
...
...
@@ -74,59 +73,65 @@ void usage(char *exename){
}
int
count_words
(
char
*
buff
,
int
len
,
int
str_len
)
{
int
count_words
(
char
*
buff
,
int
str_len
)
{
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
;
int
white_space
=
1
;
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
for
(
int
i
=
0
;
i
<
str_len
;
i
++
)
{
if
(
buff
[
i
]
!=
' '
)
{
if
(
white_space
==
1
)
{
words_in_string
++
;
white_space
=
0
;
}
}
else
{
white_space
=
1
;
}
}
buff_ptr
++
;
printf
(
"Word Count %d
\n
"
,
words_in_string
);
return
words_in_string
;
}
}
int
reverse_words
(
char
*
buff
,
int
len
,
int
str_len
)
{
int
reverse_words
(
char
*
buff
,
int
str_len
)
{
if
(
buff
==
NULL
||
str_len
<=
0
)
{
printf
(
"Error: Invalid input.
\n
"
)
;
return
-
1
;
}
char
temp
;
char
*
reversed_buff
=
(
char
*
)
malloc
((
str_len
+
1
)
*
sizeof
(
char
));
//+1 for null terminator
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
;
while
(
start
<=
end
)
{
reversed_buff
[
start
]
=
buff
[
end
];
reversed_buff
[
end
]
=
buff
[
start
];
//Only copying the string and not the buffer '.'
start
++
;
end
--
;
}
buff
[
str_len
]
=
'\0'
;
//Ends the string so the buffer thingy is not printed
reversed_buff
[
str_len
]
=
'\0'
;
printf
(
"Reversed String:%s
\n
"
,
reversed_buff
);
free
(
reversed_buff
);
//Even freed the reversed_buff. Memory leaks are a joke to me
printf
(
"Reversed String: &s
\n
"
,
buff
)
;
return
0
;
}
int
write_string
(
char
*
buff
,
int
len
,
int
str_
len
)
{
int
write_string
(
char
*
buff
,
int
len
)
{
printf
(
"Word Print
\n
----------
\n
"
);
char
*
buff_ptr
=
buff
;
char
word
[
BUFFER_SZ
];
char
word
[
len
];
int
word_count
=
0
;
int
line_number
=
1
;
int
line_position
=
0
;
...
...
@@ -144,8 +149,9 @@ int write_string(char* buff, int len, int str_len) {
word_count
=
0
;
//reset word counter
}
*
buff_ptr
++
;
buff_ptr
++
;
}
return
0
;
}
...
...
@@ -203,7 +209,7 @@ int main(int argc, char *argv[]){
printf
(
"Error: Memory buggin fwah "
);
}
user_str_len
=
setup_buff
(
buff
,
input_string
,
BUFFER_SZ
);
//see todos
user_str_len
=
setup_buff
(
&
buff
,
input_string
,
BUFFER_SZ
);
//see todos
if
(
user_str_len
<
0
){
printf
(
"Error setting up buffer, error = %d"
,
user_str_len
);
exit
(
2
);
...
...
@@ -211,27 +217,41 @@ int main(int argc, char *argv[]){
switch
(
opt
){
case
'c'
:
rc
=
count_words
(
buff
,
BUFFER_SZ
,
user_str_len
);
//you need to implement
rc
=
count_words
(
buff
,
user_str_len
);
//you need to implement
if
(
rc
<
0
){
printf
(
"Error counting words, rc = %d"
,
rc
);
exit
(
2
);
}
printf
(
"Word Count: %d
\n
"
,
rc
);
break
;
//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
);
rc
=
reverse_words
(
buff
,
user_str_len
);
if
(
rc
<
0
)
{
printf
(
"Error reversing words, rc =
$
dn
\n
"
,
rc
);
exit
(
2
);
printf
(
"Error reversing words, rc =
%
dn
\n
"
,
rc
);
exit
(
3
);
}
break
;
case
'w'
:
rc
=
write_string
(
buff
,
BUFFER_SZ
,
user_str_len
);
rc
=
write_string
(
buff
,
BUFFER_SZ
);
if
(
rc
<
0
)
{
printf
(
"Error writing string, rc = %d
\n
"
,
rc
);
exit
(
2
);
}
break
;
case
'x'
:
if
(
argc
!=
5
)
{
usage
(
argv
[
0
]);
exit
(
3
);
}
printf
(
"Not implmented!
\n
"
);
exit
(
2
);
break
;
default:
usage
(
argv
[
0
]);
exit
(
1
);
...
...
@@ -245,7 +265,7 @@ int main(int argc, char *argv[]){
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
...
...
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