SLC21 Week3 - Strings in C
0 comments
(3 points) Practically (i.e., with code examples) explain the theory from the first part of this lesson, where the concept of two sizes of an array is discussed. Demonstrate how to make it look like the array size can be increased/decreased. All loops should work with the array size stored in the size variable. Keep the physical, actual size in the constant N.
For me to illustrate the concept of managing two sizes for an array in programming, I have to move over to create an array that has a physical size and a logical size which is the number of elements that in used. By working with the size
variable, I can make it look as if the array size can be increased/decreased.
A function that checks if the logic size is less than the physical size (N
) is created using addElement
. If true, it adds an element at the current logical size position and the increments size
as well. Also, there is a function that I created that decreases size
effectively ignoring the last elements without removing the element from its memory.
(1 point) Declare a string variable (store any sentence in the array). Task: reverse the string, i.e., write it backward. For example: char s[]="ABCDEF";.....your code.....cout<<s; => FEDCBA
A simple loop to swap characters from the beginning and end of the string until we have reached the middle is what we can use. For example in doing this, I declare a string st and initialize it with a phrase as "I am Josepha", and then use strlen(st) to find the length.
As said earlier, the simple loop to swap the character from the beginning and end of the string until it babe reaches the middle is what I will use which I swap it with the character at position len - I - 1. This approach effectively reversed the characters in place in the array, and printed the result "!ahpesoJ ma I".
(1 point) Swap neighboring letters char s[]="ABCDEF";.....your code.....cout<<s; => BADCFE
For us to swap neighboring letters in a string, a loop to go through the array in pairs is what we can use and swap each pair. We will need to use strlen(s)
to get the length of the string and loop through the array, incrementing i
by 2 for each time we swap neighboring characters.
The above is the code to achieve swap neighboring letters which in the code, the cout << s
statement displays the modified string with neighboring letters swapped as seen in the code. In terms of an odd-length string, the last character will remain in place the above code works perfectly for strings with both odd lengths and even.
(1.5 points) Remove all vowel letters char s[]="this is some text";...your code...cout<<s; => ths s sm txt
For we to remove all vowels from a string, we can iterate through each of the characters and only keep non-vowel characters which code below is how we can do it.
In the code, the isVoewl
function helps us to check if the character is a vowel by converting it to lowercase and comparing it to a, e, I, o, u
. As said earlier, we will have to iterate through each character in the string. This technique works best for both uppercase and lowercase vowels and leaves only the non-vowel character and the consonants.
(2 points) Double each vowel letter char s[]="this is some text";...your code...cout<<s; => thiis iis soomee teext
To double each vowel letter in a string, we can iterate through the original string, copy the non-vowel characters the way they are, and duplicate each vowel. We will have to use a second array to store the result since the modified string will be longer.
We will need to copy each character from s
to result
. With this, if the character is a vowel, we will then add it again immediately after to double it. The code we have shared can effectively double each vowel in the string, leaving all other characters unchanged.
Additional task (1-2 points) - as a replacement for any of the tasks 2-6: Choose any number, preferably slightly larger than the length of the text. Increase the text length to the specified number by adding spaces between words. char s[]="this is some text";...your code...cout<<s; => (this is some text) len of s => 17 number =27
The code we have shared here starts by calculating the needed spaces to reach the final length. If there are no needed spaces that need to be added, it will then print the original string. It counts spaces in s, calculates how many extra spaces are needed to be added between each existing space, and distributes any remaining spaces. In the s each space is followed by the calculated extra spaces. The string is then printed with space evenly distributed to achieve the desired length of 27.
I am inviting; @dove11, @simonmwigwe, and @ruthjoe
Cc:-
@sergeyk
Comments