Strings in C | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is strain was in the last session we discussed some of the examples on a string handling now in this session we'll continue that discussion here how to find the length of the string using string length function already we discussed now in this program how to find the length of the string without using a library function observe so program main method we are writing main method inside the main method I am declaring one string variable character array the size we are taking maximum you can input 30 characters and next to find the length we are declaring one length variable LG and aaliyan and here so we are asking printf printf enter string enter string and this is we are reading using get as that string we are passing STR we are passing that STR it will read multi-word characters also it will read so that is why I have written get s and next in the previous program directly we are calling STR le n but now this is user-defined function you can write any name I am writing string length this is user-defined function you have to write the logic for this function right it returns length of the string what string you are passing that you have to pass as an argument and the return value we are collecting into le n Elyan is of type word in teaser because a string length is always in the form of integer we are printing printf printf length percentage D length is always we are representing in integers so percentage D we are printing that length sir up to here it is okay here what is the length how can you find the length you have to write the logic inside the string length function is a string length function you are passing STR as an argument STR is of type word is a character array so here you have to declare a character array size no need to specify sir variable name here you are passing STR no problem here you can collect into any type of variable right and what it is written in integer here you have to write the logic this is the code written value sir where is the prototype we know that in C language every function is having three things function definition that is which contains the logic function call to access that function function call is important and next function prototype also important declaration is very very important right declaration in TZ and next one function name is a string length and next it is taking character array as an argument that's it in the Declaration of prototype what type of data it is taking only that much we have to specify no need to specify the variables this is prototype and this is logic and this is calling three are their functions concept now sir what is the logic now this concentrate on logic here suppose this is the string a simple string we are taking any empty T last character is a null character an E and here the base address starts with a thousand next one thousand and one two three four five STR STR holds that address thousand is pointing to this one now we need to find out how many characters inside that array just initially declare the variable I value zero and Counting starts with a zero counting starts with a zero so how many characters we do not know so we have to find out how many characters we have to count counting starts with a zero right we are using loop how many characters number of times it has to iterate here it is STR sir we are writing logic here now suppose here it is a value X right yes you can use anything suppose if you are based on string STR you can go for STR or you can go for X also X also pointing to same location STR and X both are pointing to same location so logic you can write either by using STR or by using X suppose here you are writing logic now here in this method so it is better to use X so now I am using X here X of I what is that I value zero X of zero value is a we are checking whether it is not equals to null character or not sir why because last character in the string is a null character whenever it reaches the null character whenever it reaches the null character we should stop counting how many characters in that string so that is the only reason we are writing this observe xfi a is not equals to null so come inside increase the count value one character is there so count value become one and next to repeat the loop index number also we have to increase so I value I plus plus I value also increases I value increases next in the second iteration loop will repeat X of one en n not equals to null condition true come inside count value increases next I value next st x of two that is in count value increases x value increases next i count value increases x y value increases next ii count value increases i you al u increases in the last iteration X of I nothing but X of five five is a null character null character not equals to null character null character not equals to null character condition failed then it terminates so finally it will return the count value so count value is what five see how many characters are there 1 2 3 4 5 so this logic this logic you have to place here this is the logic this is the program okay how to find the length of a string without using library function here we are writing our own logic we are not depending on any other logics ok so now we will see right how to reverse all the elements in a string how to reverse all the elements in a string we know how to swap two numbers right here it is for example first we will see a swapping swapping means what is just interchanging two numbers or two characters anything two things we are interchanging just consider a value is a 10 B value 20 a value 10 B value 20 so can we write directly B value we are storing into a and a value we are storing into be not allowed this is wrong reason observe B value we are assigning to a what is the B value 20 that we are assigning to a 20 we lost in next a value we are assigning to B means what now a value is modified value 20 that we are storing into be so values become 20 20 we lost 10 so that is why interchanging means we need to take the help of another person nothing but another variable here what you lost that you should place that you should save into another variable now temp variable contains the a value what is that a value 10 now stored into a we are storing the B value B value will be storing into a it is 20 now into B I want to store a 10 in to be I want to store 10 then is already inside the temp variable so we are using temper temper value 10 will go and store into B that is and one more thing is there I do not want to use any temporary variable and I want to swap two numbers here a equals to 10 B equals 220 B equals 220 first add a plus B after adding you can store into any variable either you can store into a or you can store into B that is your wish I am storing into a a value 10 B value 20 value 30 30 we are storing into a so a value become 30 now here I want to store a 10 sir how can we store 10 here how you will get 10 here 30 minus 20 will give 10 30 means a minus 20 means B a minus B will give 10 that I want to store into B so B value 10 next to Sir here I have to store 2010 is okay one we got and second one is a 20 how you will get wente same operation if you perform a minus B a value 30 30 minus 10 20 that we are storing here that is into a 20 this is mapping here we are not depending on any temporary variable but here we are using one extra variable okay so long X so if you want to perform swapping operation either we go for this one or we can go for this one now based on this how to swap write all the characters in a string will see this is a logic so same logic is applicable to write even number of characters and odd number of characters how observe suppose first I am taking even number of characters six characters we are taking 0 1 2 3 4 5 suppose if length length is equal to length we will find out later in the logic I'll write write any name we have to write 6n a r e s H nourish six characters and of course last character is a null character that no need to swap no need to reverse this is one and next we are taking one more string one more swing so this is odd number of characters this is what odd number of characters so take the same one any 0 1 2 3 4 0 1 2 3 4 then write the logic find the length of the string every string length how to find out first I value starts with a 0 so why I value you are using first I want to swap these two characters I value 0 J value should be 5 and here also same story here I want to swap these two here it is I valued 0 J value should be 4 so who will give that J value very simple string length of X are STR suppose this is X array name is ax X we are taking string length of X in this case it will give 5 string length of X in this case it will give 4 so I say well values are ready just simply perform a swap operation take the temporary variable temporary variable I value and Z value nothing but X of I X of J you have to swap in to temper X of I will go and store nothing but into the temp what EXIF I value is n next into X of I X of J will go on store X of Z value is H H will come to here an X 1 into X of J temp value will go and store so what is that temp value temp already contains Y n n will go and store here n right so 2 swapping completed nothing but here also II come to here and M over there swapping completed only first and last characters next these two characters we have to swap I value 1 J value for increasing the I value decreasing the J value increasing the I value decreasing the J value this is here also same story here also same I value 1 J value 3 I value 1 J value 3 and here it is how it executes second iteration same story yes come to here a move over there so same logic now yes you have to place inside the while loop sir what is the condition condition will come you keep on execute here I will come to here and n over there this is second iteration come to the third iteration here it is I value become 2 J value become 3 these two will swap II and are sir here here I value is a 2 J value is also to nothing but whenever I value equals to J value we should stop no need to swap because see already elements reversed a n n IA are in this case these two swapped in the next iteration I value is 3 J value is to increase the value decrease the Z value same story now here it is observed I is greater than J value in this case also we should stop observe very clearly whenever we are swapping right first character last character second character last but one character like that whenever we are swapping whenever it reaches a particular position whenever it reaches a condition what are the conditions are whenever I value is greater than 0 we can stop and whenever I equals to J also we can stop then in which situation right on which condition we have to perform a swap operation means if it is not I equals to Z and if it is not I is greater than J it should be I is less than J this is the condition this is the condition first I value 0 J value v 0 is less than Phi swapping 1 is less than for swapping 2 is less than 3 swapping next we should stop here 0 is less than for swapping 1 is less than 3 swapping 2 is less than 2 stop okay so this is how to swap all the characters in a string how to reverse the string this is very simple logic if you know how to swap the elements in the array easily we can use this one okay now we will see how to convert all the uppercase characters of a string into lowercase so generally in a string dot H library in a string dot H library one function is there so what is that function STR LWR right STR LWR will convert all the uppercase characters in a string into lowercase here suppose if you pass any string any string directly if you pass all our uppercase characters only suppose nourish that will be converted into lowercase but suppose observe we are passing like this here it is a plus n N and here it is a star we are passing like this then what will happen means it will consider only uppercase characters it will not disturb the remaining characters so only it will convert a into small letter plus small n again small n and star that is it so here it is whenever we are writing the logic user-defined logic we should be take care of that sir how to converts a very simple logic based on ascii character system based on ascii character system here capital a value is a 65 capital B value is a 66 and capital Z value 90 in a ASCII character system small a value 97 small B 98 and so on small Z is a 122 for capital a if you add 32 will give a small Y a 65 plus 32 a value is a 97 everywhere 90 plus 32 value is a 122 so to the character uppercase character if you add 32 it will give small gays so here to capital a and to capital and only we have to add 32 by mistake if you add 32 to plus symbol or to small n and 2 star will give some other characters that is a problem okay so on conditions we have to add 32 so what is the logic very simple see take the example this is one string memory allocations we are writing suppose en star small R plus yes small H last character is a null character suppose X is a arrayname string name is a X here it is length index number index number first we have to find the length or without finding the length also we can find out easily how to send the control from first location to last location in the finding the length of the string already we have seen I value I value starts with a 0 while while X of I naught equals to null character not equals to null character it will send the control from first location to last location it will check character by character first X of I nothing but X of 0 X of 0 value is what capital n so first it will collect that value capital n next we need to check the capital n value is within the range of capital a to capital Z or not if it is not within the range if it is not the capital letter that we should not suppose to convert into lowercase so that is why directly we should not suppose to add to X of I directly we cannot add third - on some condition sir what is the condition very simple if that X of I if that character is in between capital A and capital Z capital A and capital Z this is the condition X of I is a n n is within the range if you do not know the ASCII values then no need to means what right that one directly we can go for capital a to capital Z sir I know then you can write a 65 and 90 it is also allowed accepted so now capital n is within the range of capital a to capital Z only so to that value 32 will be added and that will go on store it will change to small in and next we should move to next location increase the I value it will repeat star star is not within the range of A to Z leave it I plus plus increases it will check our our is not within the range of capital A to Z leave it next place is not within the range next es yes it is within the range then it will be converted into smallest and then H is not within the range next whenever it reaches the null character null character not equal to null character condition fails this is a simple logic that how to convert how to converts all the capital letters of his string into lowercase steps first convert nothing but repeat the loop from first location to last location character by character we have to read after reading the character we need to check whether it is within the range of A to Z or not a to Z if it is within the range of a to Z then only add 32 to that character if it is not within the range of A to Z skip that character go for the next character this process will continue until right it reaches that null character whenever it reaches the null character it will stop okay so this is the logic next we will see how to merge two strings nothing but how to concatenate two strings okay so take two strings simply we are taking s1 s1 string here it is s1 string is a hello we are taking null character and next one s2 we are taking s2 suppose this is world world so these are the two strings ready now just merging these two strings nothing but concatenated these two strings so concatenation means adding all the characters of a first string at the end of the second string simple right and here it is a string s2 characters we are adding at the end of the s1 here here we need to add like this but see whenever we are trying to add w we should not suppose to add here because whenever you add w while printing that finally s one whenever it reaches the null character it will stop printing well it will not print so that is why we should replace this null with a w-who are L D and null character you should write here this one here this one here so like that we need to add this is merging concept merging index 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 very simple logic to much faster find out the length of each string uses tearily and no problem string length of s 1 that we are collecting into l1 length l1 equals to what is the length 5 second string length l2 l2 equals to STR le n of s 2 string length l2 is also fine now how many characters s 2 from 0 to 5 null character also you have to merge so not less than length right less than or equal to length from 0 location to fifth location all the characters you have to append so here we are repeating the for loop I value starts with 0 to I is less than or equals to important equals to means last character also l2 I press place and we need to append how to append zeroth character first I value zero s s 2 of s 2 of I value will be stored into which location into s 1 of fifth location sir what is the fifth location L one is a five at the end so s 1 of L 1 plus I L 1 plus I see first s 2 of 0 s 2 of 0 value W the W will be stored into s 1 of L 1 what is it L 1 value 5 5 plus I value 0 only 5 plus 0 5 so in the fifth location it will go and store next I value increases increases so s 2 of I nothing but s 2 of 1 s 2 of 1 value will go and store into s 1 of L 1 plus 1 L 1 value is a 5 5 plus 1 6 into sixth location next one next one so like that all the characters will be appended including null character right here it is in the same way all the characters of a second string will be added at the end of the first string this is called concatenation of a string nothing but merging two strings ok so this is all about a strings concept in a C language so some more practical examples we will see in a coming sessions for more videos please subscribe to nourish high technologies
Info
Channel: Naresh i Technologies
Views: 462,209
Rating: 4.8903255 out of 5
Keywords: C Language, Strings in C, Srinivas, Naresh IT, Hands on C Language Training, C Language Demo, Online C Language Training, C Language Tutorial Videos, C Language Overview, C Language Interview Questions
Id: Zs6eQaqMsgk
Channel Id: undefined
Length: 33min 34sec (2014 seconds)
Published: Fri Sep 09 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.