Strings in C Programming | Part-1 | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is Renoir's so today we are going to discuss about a string handling in C language the concept is string handling in C language first of all what is string so what is the string means it is a one-dimensional character array this is character array and that is one dimensional one dimensional character array is called string here it is so what is the use of strings if you want to process the information such as names right always we use a string datatype only string data type with the help of strings only we can store such type of data so this is one dimensional character array what we can store means here it is only characters we are storing so syntax generally if you want to declare an array how to declare the array we are writing data type followed by identity is nothing but air in name and here it is we are specifying the size we are specifying this is the syntax of array but sir what is the syntax of a string suppose if it is the array any type of data is allowed so that is why generally we are specifying that it is a data type any data type is allowed but now in case of string strictly characters allowed into the array no other type is allowed so here it is in the syntax also you have to write character data type it is a rule we should not suppose to specify data type it is a character because only character elements only allowed into the array right and see the example a simple example I am giving character name the size suppose I am giving 20 the size I am giving 20 and remember if the size of array is a 20 maximum we can store only 19 characters because the last character is a null character it is by default okay how it will store see we can initialize a string in different ways for example character array character array or we are taking that names also suppose s1 the size is a 5 and if you want to initialize the value I am giving that value hurry-hurry is a value then it gets memory allocation sir why the size is of 5 means because I want to store 4 characters it gets memory allocation like this five characters block here it is H a are I last character is a null character null character this is actually blank character null character sir what is the ASCII value of null character is a 0 as K value is a 0 null character ASCII value and as usual index will be present index will be present and the base address suppose starting address is at 2 0 4 6 consider here it is s 1 gets memory allocation and it holds the value 2 0 4 6 and it is pointing to this block we know that array variables always holds base address so that is why array variables are called internal pointer variables internal pointer variables see language supports internal pointer and external pointers also in eternal pointers means arrays strings structures unions all these are comes under internal pointers external pointers means pointers concept will see string variable is also internal pointer variable sir why what is the reason means it holds the reference nothing but address of the block what type of variables holes address only right pointer variables holes so it is a pointer variable okay and each character occupies how many bytes only one byte in a c language that already we discussed so here two zero four seven two zero four eight two zero four nine two zero five zero so like that memory will be allocated how many bytes five bytes memory will be allocated right in the second case suppose if you want to initialize as to of size we are giving four you can initialize character by character also just like array using curly braces suppose here it is whoa en e this is another way of initialization here all the characters at a time we are assigning using double quotes and here it is a character by character we are assigning using single quotations two way of initialization how the memory will be allocated means each character occupies one byte string always initialize with the base address of the block variable this is called internal pointer variable because it holds address okay how can we process the elements right see so very important discussion right in strings is a person days yes person days yes so what is this it's a format specifier we know already some of the format specifiers which are representing characters integer short double float and all it is also one format specifier so what is the use means read and write read and write string elements string elements directly if you want to read the data of a string or if you want to write the data of a string character array with the help of percentage s only we will do all these things we will do all these things and one more thing for percentage s no need to use iterators no need to use iterators sir what is iterate er it is loop generally if you want to read elements into arrays right we are using loops either for loop or while loop we are using to read the elements because array always contains more than one element so suppose if 100 elements are there hundred times as can a function we cannot write impossible case always we should go for percentage s or loops we are using for loop we are repeating 1 to 0 200 times we are repeating and we are reading scanner function we are writing only one time inside the loop but here it is in case of percentages loops not required reasons I will explain okay so why and simply how to read and how to print okay one example see writing the program hash include stdio.h and here it is a main method in the main method I am declaring one character array only declaration just variable name I am giving name on sighs is a 20 sighs is 20 now I am printing a message enter your name enter your name we are writing so on the console on the black screen on the black screen it will print the message what is that enter your name same as is what you have written in the printf function that will print on the console that will print on the console so we have to read scanf scanf % is yes what is that % is yes because I want to read a string no need to use any loop reasons I will explain very clearly ok % is yes and here it is we are passing only name we are passing only name here it is sir actually whenever we are reading some elements using scanf we are using address operator now yes generally we are using address operator but here it is address operator not required reasons I'll explain address operator not required only just pass the name of the character array so they will enter some value for example here they will enter hurry hurry and whenever they will press Enter that will be collected into this name and next I want to print the value we are taking that input Harry and I want to print this message clearly printf hi Harry in place of hurry percentage yes comma welcome welcome and here it is we are printing that name printing that name so what is the value will be print here in place of percentages we know how to format high hurry , welcome so in the output it will print hi hurry , welcome this is a simple program it is a simple program how to read elements into character array nothing but into the string how to read one name and after that how to print that name just by formatting that I am printing this name okay but here it is sir I am not using any loops how it is reading more than one character and how it is printing more than one character this is the question okay see what is happening in the background so what is the advantage of using percentage yes while reading the information into character array so generally suppose if you take one array in ARR the size is a 5 just consider it gets memory allocation 5 locations memory will be allocated and here it is a base address will be stored into array variable ARR 2 0 4 6 and it is pointing to the first location next location integer occupies 2 bytes so two zero four eight two zero five zero two zero five two two zero five four these are the memory addresses of each location in the array and array also internal pointer variable because it holds the reference it holds the address now here so whenever we are reading the elements clearly we are specifying that enter five elements because here we know exactly how many elements you want to read in this case right here it is clearly we are specifying that please enter five elements into the array so we are repeating for loop index number starts with a zero one two three four nothing but less than size size is a five less than five means what is a four so I value starts with a zero index starts with a zero and next I is less than 5 I plus plus location by location it will increase we are reading scanf percentage D into address of a or R of I into address of a RR of IVR reading so means what to read the element the location address we are specifying very clearly here it is a error of I address means what a RR is a 2 0 4 6 right ARR of I a error of 0 address 2 0 4 6 first address is a 2 0 4 6 if you read one element the 10 will go and store here suppose element is a 10 in the next iteration I value become 1 ARR of 1 address that is 2 0 4 8 next to 0 5 0 and so on we are giving so exactly it will repeat 5 times 10 20 30 40 50 it will repeat 5 times exactly because here we are clearly specifying that you please enter 5 elements into array here it is okay we can go for loops but just observe the character array character array I am declaring character array name any name we are giving suppose name and the size we are giving 20 size we are giving 20 so in this case here it is total 20 memory locations will be created 20 memory locations will be created and so on index number starts with a 0 2 3 4 and so on up to 19 up to 19 and the base address suppose starts with a thousand character occupies only one byte so next location address is a 1,001 1,002 three four five and so on so just like this array variable character array variable also holds the base address only what is the base address name name holds the address that is thousand thousand is pointing to this one thousand is pointing but now here the question is here it is we are asking printf generally whenever we are working with character arrays we are asking enter your name but we never ask we never ask please enter twenty characters as your name is that possible impossible case observed in case of arrays how many elements you want to read that already we know here clearly we are specifying that please enter five elements into the array but in case of character array how many elements you have to read how many characters you have to read that we do not know because clearly we are specifying that enter your name name including how many characters how can you say that right that is not possible so that is why simply we are using scanf percentage yes and here it is only name we are giving sir atleast address operator you have to write no reason on the console on the console whenever this message will print enter your name enter your name here we are giving that name so name means already it is the base address name contains what base address it will contain so whenever we start entering the character suppose I am giving Harry H a R I here it is H H will be collected into first location automatically and next a will be collected into second location our eye and whenever you press enter symbol whenever you press enter symbol then it will place a null character in the fourth location in the fourth index location null character will be appended automatically at the end of the characters so why suppose if you are if it is not placing null character right whenever you are printing the data suppose I want to print the value printf printf name % is yes and whenever we are printing the name how many characters it will bring all the twenty characters are no because input is only four characters are there then how can you print all the twenty characters what about the remaining characters some garbage characters it will print that is not possible exactly it has to print only four characters that is from first location to last location whenever it reaches the null character it stops printing so output should be name is name is that hurry it will print whenever it reaches the null character it will stop printing or else it contains some garbage values garbage values corresponding characters will come hurry after that some place multiplication some other symbols will go and print this is the only reason why we are depending on percentage yes instead of using loops in case of arrays how many elements we have to read exactly we know but in case of character array how many characters you have to read that we do not know so that is why already they have written one program in the form of a format specifier percent is yes so with the help of percent is yes we are reading and printing and no need to use address operator while reading once again wiser because variable always holds the base address only if you pass only base address from the base address it will store element by element but here it is every location address manually we are passing but here it is we are passing only base address that is the difference between ordinary array and next one character array how to read the elements and how to print the elements okay some more examples on strings we will see in the next session for more videos please subscribe to nourish ID channel thank you
Info
Channel: Naresh i Technologies
Views: 716,854
Rating: 4.8953385 out of 5
Keywords: C Language, Strings in C, Srinivas, Naresh IT, Strings in C Programming, strings in c programming examples, strings in c language with example, what is string with example in c, string functions in c, string array in c, length of string in c, string concatenation in c, string reverse in c, compare two strings in c, copy string in c, convert string upper to lower and vice versa, strings in c examples, c string functions, c string programs, c programming, learn c language
Id: AW3m7kdWvI8
Channel Id: undefined
Length: 21min 20sec (1280 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.