SLC21 Week3 - Strings in C

in #slc21w3sergeyk11 days ago (edited)

Hello steemians,

Here is my homework for SLC21 Week 2 on programming arrays, with the corresponding tasks assigned by @sergeyk!

Strings in C.png

Edited by Canva

Explain the theory from the first part of this lesson, where the concept of two sizes of an array is discussed.

To illustrate concretely the notion of array size in C, let's break this idea down into several steps with code examples, we will thus create an array with a fixed physical size, which remains constant throughout the execution of the program , and a logical size that can vary to simulate the increase or decrease of the array according to the elements used.

image.png

The constant 'N' will specify the physical size corresponding to the memory allocated to the array while the variable 'size' will represent the logical size which is the number of elements currently active or used in this array, the physical size of the array is specified by "N" which guarantees that the memory allocation remains unchanged throughout the program but that the logical size is flexible allowing us to add or remove elements by simply changing the value of "size". In this part, we declare the array "a" with a fixed physical size of "N", which means that the array can hold up to 1000 integers even if only a part of this space is actually used and the logical size represented by the variable 'size', initially set to 10, indicates that only the first ten elements of the array are actually active or used.

image.png

To add a new element to the array, simply place this new value at the index 'size', that is, at the end of the logical part of the array and then increase 'size' to increase the logical size by one. In this example, the statement "a[size] = newElement;" adds the value "11" to the end of the logical part of the array and by increasing "size" this element becomes an active part of the array although the actual size is still 1000 the array now logically contains 11 elements.

image.png

To simulate the removal of the last element, simply decrease the "size" which excludes that element from the logical part of the array without removing it from memory, and by decreasing the "size" we remove the last element from the active part even if it is still physically in memory, this technique effectively restricts the "visible" part of the array.

image.png

By using the "size" variable in the loops, we have precise control over which part of the array is processed, allowing us to add or remove elements almost without changing the actual size initially allocated. This approach therefore shows how to manage the logical size of an array, giving the illusion that elements can be added or removed while maintaining a fixed memory allocation. By simply changing the "size" we simulate the dynamic behavior of a fixed-size array, providing great flexibility of use without the need to reallocate memory.

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

To reverse a string stored in a character array in C we can use two pointers one starting at the beginning of the array and the other at the end and swap the characters at these positions by moving inward until they meet. Here’s the code for this approach:

image.png

In this example, we declare a string st and initialize it with a phrase, "I am Kouba01", then use strlen(st) to find its length. The for loop loops through the first half of the string, where for each character at position i, we swap it with the character at position len - i - 1. This technique effectively reverses the characters in place in the array, the result, "10abuok ma I", is printed as a reversed string. This method works effectively for any string stored in a character array as it simply rearranges the characters without requiring any additional memory.

Swap neighboring letters char s[]="ABCDEF";.....your code.....cout<<s; => BADCFE

To swap neighboring letters in a string stored in a character array in C, we can iterate through the array and swap every pair of adjacent characters. Here’s the code to achieve this:

image.png

In this code, we use the string st initialized to "ABCDEF". The for loop iterates over st in steps of 2, swapping each pair of adjacent characters by temporarily storing st[i] in temp, then assigning st[i + 1] to st[i], and finally assigning temp to st[i + 1]. The output of this code will be "BADCFE", where each pair of neighboring letters has been swapped.

Shift the string cyclically to the left (it’s easier to start with this), then cyclically to the right. char s[]="ABCDEF", x[]="abrakadabra";.....your code.....cout<<s<<"\n"<<x; => BCDEFA aabrakadabr

image.png

This code performs a left cyclic shift on the string st and a right cyclic shift on the x string. For st, we store the first character, shift all characters one position to the left, and place the first character at the end, turning "ABCDEF" into "BCDEFA". For x, we store the last character, shift all characters one position to the right, and place the last character at the start, transforming "abrakadabra" into "aabrakadabr". The results are then displayed.

Remove all vowel letters char s[]="this is some text";...your code...cout<<s; => ths s sm txt

We begin by creating a temporary result string of the same size as the original string to store the result without vowels. Then, by going through each character of the string s, we check if it is a vowel (lowercase or uppercase), if not, the character is copied into result. Once the loop is complete, we end the result string with the null character '\0' to mark the end of the string, then we use strcpy to copy the contents of result into s, which replaces the original string with its version without vowel The final display of s then produces "ths s sm txt" by removing all the vowels from "this is some text".

image.png

Double each vowel letter char s[]="this is some text";...your code...cout<<s; => thiis iis soomee teext

image.png

In this code, I start by defining a string s with the text "this is some text". I then create a temporary string result that will have enough room to hold the version of s with each vowel doubled. As I loop through each character in s, I add each character to result. If the character is a vowel (checked by an if condition), I copy it a second time to result, which doubles the vowel.

With each addition, I increment the index j to advance through result. Once the loop is complete, I add the null character '\0' to mark the end of the result string. Finally, I print result, which contains the modified string with each vowel doubled, and the result is displayed as "thiis iis soomee teext".

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

image.png

The code starts by calculating the spaces needed to reach finalLength. If no spaces need to be added, it prints the original string. Otherwise, it counts spaces in s, calculates how many extra spaces to add between each existing space, and distributes any remaining spaces. Each space in s is followed by the calculated extra spaces. Finally, the string is printed with spaces evenly distributed to achieve the desired length of 27.


Thank you very much for reading, it's time to invite my friends @analp, @crismenia, @elianyto participate in this contest.

Best Regards,
@kouba01

Sort:  

You have completed all the tasks with passion and while explaining them logically. The cyclic shifting is very interesting in programming where we can shift the string characters in both ways while shifting to the right side and similarly to the left side.

Loading...