Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS503
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
Vanshika Mohan Bongade
CS503
Commits
e0bcad0f
Commit
e0bcad0f
authored
6 months ago
by
Vanshika Mohan Bongade
Browse files
Options
Downloads
Patches
Plain Diff
Update Stringfun.c
parent
6eeae00c
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
WEEK-2/Stringfun.c
+13
-76
13 additions, 76 deletions
WEEK-2/Stringfun.c
with
13 additions
and
76 deletions
WEEK-2/Stringfun.c
+
13
−
76
View file @
e0bcad0f
...
...
@@ -23,24 +23,7 @@ void usage(char *exename){
printf
(
"
\t
example: %s -w
\"
hello class
\"
\n
"
,
exename
);
}
//count_words algorithm
// 1. create a boolean to indicate if you are at the start of a word
// initialize to false
// 2. Loop over the length of the string
// 2a. Get the current character aka str[i]
// 2b. Is word_start state false?
// - Is the current character a SPACE_CHAR?
// * if YES, continue loop (a.k.a) goto top with "continue;"
// * if NO, we are at the start of a new word
// > increment wc
// > set word_start to true
// 2c. Else, word_start is true
// - Is the current character a SPACE_CHAR?
// * if YES we just ended a word, set word_start to false
// * if NO, its just a character in the current word so
// there is nothing more to do
// 3. The current word count for the input string is in the wc variable
// so just 'return wc;'
// Function to count the number of words in the string
int
count_words
(
char
*
str
)
{
int
wc
=
0
;
bool
word_start
=
false
;
...
...
@@ -57,21 +40,7 @@ int count_words(char *str) {
}
//reverse_string() algorithm
// 1. Initialize the start and end index variables
// a. end_idx is the length of str - 1. We want to remove one
// becuase at index str[len(str)] is the '\0' that we want
// to preserve because we are using C strings. That makes
// the last real character in str as str[len(str)-1]
// b. start_idx is 0, thus str[0] is the first character in the
// string.
//
// 2. Loop while end_idx > start_idx
// 2a. swap the characters in str[start_idx] and str[end_idx]
// 2b. increment start_idx by 1
// 2c. decrement end_indx by 1
//
// 3. When the loop above terminates, the string should be reversed in place
// Function to reverse the string in place
void
reverse_string
(
char
*
str
)
{
int
end_idx
=
strlen
(
str
)
-
1
;
// Last character index
int
start_idx
=
0
;
// First character index
...
...
@@ -88,43 +57,7 @@ void reverse_string(char *str) {
}
}
//word_print() - algorithm
//
// Start by copying the code from count words. Recall that that code counts
// individual words by incrementing wc when it encounters the first character
// in a word.
// Now, at this point where we are incrementing wc we need to do a few more things
// 1. incrment wc, and set word_start to true like before
// 2. Now, set wlen to zero, as we will be counting characters in each word
// 3. Since we are starting a new word we can printf("%d. ", wc);
//
// If word_start is true, we are in an active word, so each time through the loop
// we would want to:
// 1. Check if the current character is not a SPACE_CHARACTER
// a. IF it is NOT A SPACE -> print the current character, increment wlen
//
// 2. In the loop there are 2 conditions that indicate a current word is ending:
// a. word_start is false and the current character is a SPACE_CHARACTER
// OR
// b. the current loop index is the last character in the string (aka the
// loop index is last_char_idx)
//
// IF either of these conditions are true:
// * Print the word length for current word - printf(" (%d)\n", wlen);
// * Set word_start to false
// * Set wlen to 0 given we are starting a new word
//
// EXAMPLE OUTPUT
// ==============
// ./stringfun -w "C programming is fun"
// Word Print
// ----------
// 1. C (1)
// 2. programming (11)
// 3. is (2)
// 4. fun (3)
// Function to print each word and its length
void
word_print
(
char
*
str
)
{
int
len
=
strlen
(
str
);
int
wc
=
0
;
...
...
@@ -163,8 +96,7 @@ int main(int argc, char *argv[]){
}
opt_string
=
argv
[
1
];
//note arv[2] should be -h -r -w or -c, thus the option is
//the second character and a - is the first char
if
((
opt_string
[
0
]
!=
'-'
)
&&
(
strlen
(
opt_string
)
!=
2
)){
usage
(
argv
[
0
]);
exit
(
1
);
...
...
@@ -197,10 +129,6 @@ int main(int argc, char *argv[]){
case
'r'
:
{
reverse_string
(
input_string
);
// Reverse the string
printf
(
"Reversed string: %s
\n
"
,
input_string
);
// Print the result
// Explanation for TODO #4:
// Strings in C are passed by reference, so changes to the array
// in reverse_string are directly applied to input_string.
break
;
}
case
'w'
:
{
...
...
@@ -217,8 +145,17 @@ int main(int argc, char *argv[]){
//TODO: #6. What is the purpose of the default option here?
// Please describe replacing this TODO comment with
// your thoughts.
// ANSWER #: Invalid input options are handled by the default option.
//It shows the usage instructions and an error
// message before ending if the user enters an unsupported
// flag (not -h, -c, -r, or -w, for example).
// This prevents unexpected behavior by making sure the program doesn't continue with incorrect inputs.
}
//TODO: #7. Why did we place a break statement on each case
// option, and did not place one on default. What
// would happen if we forgot the break statement?
// ANSWER #: Break statements make sure that after running its function, the program ends the current case.
// In the absence of a pause, the program would "fall through" and go on running the code for ensuing situations,
// leading to mistakes or unexpected behavior.
//We don't require a break in the default scenario since the program ends further execution by using exit(1).
}
\ 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
register
or
sign in
to comment