SLC21 Week3 - Strings in C


SLC21 Week3 - Strings in C



Image 2024-11-07 at 10.33.jpg

As soon as I went through the lesson for week 3 by @sergeyk, I got to know that this week is going to be difficult for me as I need to do lot of learning to cover up this part as I'm not that much experienced in dealing with strings in C language. Anyway, from the first lesson I took it as a challenge and I love it. So I'm starting the work now ( 12/11/2024), hopefully I will be able to finish the homework before the end of the week. Okay, let's get in to the work.


(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.


I'm starting the work by declaring a character array to store my name. To study the topic discuss at the start, "can an array have two sizes ?", I thought to declare a character array with size 15 to store my name which has 9 characters "vimukkthi".

Image 2024-11-14 at 15.51.jpg


Now in this array, even I have declared to size 15 initially, I'm using only to store a string of 9 characters. I understand that it has actually two sizes, First one is the size I initially declared, physical size, 15 ( 15 bytes in the memory ) and again the length of the text is 9 characters, actually "vimukkthi" require 10 characters including the null terminator, which is actually used space. So from the above example we can understand the meaning of the capacity of an array and the logical size.

Okay, let's use same array to understand how to change the logical size of the array and to virtually add and remove elements of the array without making any changes to physical size of the array ( we can't as it's already declared).

Image 2024-11-14 at 21.58.jpg


From size++; and size--; we can increase and decrease the logical size of the array by 1. myname[size] = '\0'; will update the new end of the array. As the printf function will only print up to the null terminator, in the first case, when we increase the size, it displayed the new character we added to the end. And in the second case it printed up only to the null terminator removing the new character ( we kept null terminator at myname[size] ).


(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


Woo-hoo! Got it ! I'm not sure this is the best way to get the intended results. But I was able to write the sentence backward and it gave me the right result.

Image 2024-11-14 at 22.28.jpg


But I think there should be a better way to get the size of the array, I mean to know the exact length of the array. Anyway I declared the size to 30, which is the actually size of the array, but if we can get the logical size, or the used space up to the null terminator, I think handling this will be more efficient.

(1.5 points) 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

Found a solution for the above issue, learnt about strlen(myname); function which calculate how many characters stored in the array and return the length of the string ( not including the null terminator). I will use this for this third task. Let's give a try.


Image 2024-11-14 at 22.55.jpg

I learnt I have to use string.h header file for the strlen() function. I used the same array I used for the previous two tasks. Now the strlen() gives me the length of the string. It should be 21 calculating the space also. For the left shift and right shift I'm using a for loop. To perform left shift, in for loop, charter at position "i" is replaced by the character at position "i + 1 ". And for the right, as same, charter at position "i" is replaced by the character at position "i - 1 ". To complete the cyclic shift, the last character stored in "temp", placed at the end or start of the string for left and right cyclic shifts.


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


When thinking about this task, I first thought of filtering the vowels from the string. So there is only one way I got. I have to go through each element of the array, and check whether it's a vowel or not. So we have to go with "if" statement. So after several attempts and debugging number of mistakes I was able to get the result. Here I used a while loop, as we can run the loop till it reach the null terminator. I thought it's also a possible solution for the issue I got in task 2.

Image 2024-11-14 at 23.10.jpg

So my program is simple, if the character or the particular array element is not a vowel, it's move forward in the string. And the vowels are getting removed and at the end we place the null terminator to the end of the string.

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

I first tried this task as follows, used the same algorithm as in the previous task, if the element is a vowel I copied it twice to the array as follows. But it gave me an error continuously even I tried with many adjustments.

Image 2024-11-14 at 23.42.jpg

I know that the array size should be large enough to accommodate when we double the vowels.I tried to apply changes like, making the string more small, making the array size large etc. But still it gave me the same results. So I tried to come to the solution in a different way. What I did was I defined another array and recorded the filtered results with doubling vowels to that array which gave me the result I wanted.


Image 2024-11-14 at 23.55.jpg

Some how I was not able to modify the same array. But even my logic was right, as I'm using an online IDE may be the issue sometimes. Anyway I'm happy I was able to complete all the tasks successfully. I'm not going through the additional task as I went through the first 5 tasks given.

And it was really hard work. A true learning process and took lot of time, but totally worth it. Learnt a lot !

Thanks again @sergeyk for the knowledge given !

I like to invite : @vishwara, @virajherath and @nishadi89 to join the contest.

-Vimukthi Vithanage

About The Author :

Vimukkthi Vithanage (@besticofinder) is an Electrical Engineer and a Lecturer by profession , crypto enthusiast and a person who always values his freedom. He loves traveling , teaching and to spend time with his friends. A good reader and a good listener. He currently works in Faculty of Technology , University of Sabaragamuwa , Sri Lanka and also as a freelance SEO service provider. He writes on different topics related to his daily activities. If you like his writings , don't forget to show your love with an upvote and leave a feedback to show more love. And a resteem will be a great encouragement for his work. Feel free to contact him on discord anytime . He always love to make new friends.

Check his latest research work : visit

Sort:  

You actually did a very great job in this I must confess. This must have actually took you a whole lot of work as I am so sure of that. And I am also sure that you must have gained massively from.there

Thank you very much

@tipu curate

;) Holisss...

--
This is a manual curation from the @tipU Curation Project.

Your shifting code is looking awesome and the most I am liking your example of your name. But you did not gave any size to the array that is why it gave an error. And it is good that you fixed it by defining the new array to store the modified string and by defining size of the array. Programming is learnt by the errors and mistakes because when we do the errors we learn new ways and logic for the same problem.