SLC21 Week3 - Strings in C

mohammadfaisal -

Hello everyone! I hope you will be good. Today I am here to participate in Steemit Learning Challenge of @sergeyk about the Strings in C. It is really an interesting and knowledgeable contest. There is a lot to explore. If you want to join then:



Join Here: SLC21 Week3 - Strings in C





Designed with Canva

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.

This code is a simple demonstration of managing an array in C++ with both a physical (fixed) and a logical size. The physical size is the array's total capacity, while the logical size represents the current number of meaningful elements stored in the array.



This code represents dynamic array behavior with a fixed size array by tracking the logical size separately from the physical capacity. The functions addElement and removeLastElement manage logical changes to the array content. It allows the array to mimic resizing even though its actual size (N) is fixed.











In the main function I have called all the functions such as to add elements in the array. And then I have called the print function to display the elements in the array. Then I have tested the the remove function. It reduces the logical size and printed the array. After that I again called the function to add another new element in the array.



This is the output of the program where you can see I have added elements in the array and then I have tested other functions as well to check out if the size of the array is changing and working correctly.

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

In order to reverse the the string stored in the character array we can swap elements from the beginning and the end of the array until we reach the middle. We use two pointers for this swapping. One pointer will start from the beginning such as as from i = 0 and at the end such as len - 1. We will move the pointers inward where the start pointer will be incremented and the end pointer will be decremented. Each swap places two characters in their correct position.



This reversing program has reversed Hello, World! to !dlroW ,olleH. This is how we can reverse the string. In this string if I store the text Faisal it will reverse it efficiently by outputting lasiaF.



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

In order to swap the neighboring letters in the string which is stored in the array we can iterate through the array two characters at a time and then we can swap each pair. I will define the string with a character array which will contain the original string such as char s[] = "ABCDEF";. Then I will use a loop to go through the string by incrementing two characters at a time. Then I will swap each pair of characters with the next one.





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

In order to perform the cyclic shifts on a string in C++ we can use array manipulation to move the characters to the left or right while maintaining the original length of the array. We can do left cyclic move as well as right cyclic move.

Explanation of Steps

So after the right shifting of s = "ABCDEF" it becomes "FABCDE".

Explanation of Steps

So after the left shifting of s = "ABCDEF" it becomes "BCDEFA".



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

It is very simple to remove the vowels from the string. In order to remove the vowel letters from the string s we can use a loop to copy only non vowel characters into the string itself.



Working of removeVowels Function:

I gave this input char s[] = "My name is Faisal"; and the program removed all the vowels from the string and it returned M nm s Fsl. In this way we can remove vowels from any string and can return the remaining string after the filteration.



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

This is similar task with the previous one but in previous one we removed the vowels from the string where they were available. But now in this question we need to double the vowels found in the string on their place. We will use a loop to iterate through all the string and double the vowels.



doubleVowels Function:

This approach preserves the original string structure while doubling only the vowels. You can see that I have given this input My name is Faisal and after doubling all the vowels in this string it has returned this output My naamee iis Faaiisaal. In this way we can double the vowels in the string.



Note: I have used Onlinegdb Compiler to compile the code in C++ language.