logging in or signing up A string in C language is a variable rajni.b27 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 778 Category: Education License: Some Rights Reserved Like it (0) Dislike it (0) Added: November 14, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... By: rupashicva (16 month(s) ago) plz,send me this ppt on rupashicva@rediffmail.com Saving..... Post Reply Close Saving..... Edit Comment Close Premium member Presentation Transcript A string in C language is a variable length array of characters that is delimited by null character(‘\0’) : A string in C language is a variable length array of characters that is delimited by null character(‘\0’) 11/14/2010 Rajni Bhalla CSE/IT Storing Strings : Storing Strings A string is stored in an array of characters. It is terminated by by the null character(‘\0’). 11/14/2010 Rajni Bhalla CSE/IT End of string Introduction : Introduction Group of characters ,digits,and symbols enclosed within quotation marks are called strings. Character arrays are called strings. Every string is terminated by ‘\0’(null character). Eg : char name[]={‘I’ , ’n’ , ’d’ , ’I’ , ’a’ ,’\0’}; 11/14/2010 Rajni Bhalla CSE/IT Slide 4: 11/14/2010 Rajni Bhalla CSE/IT Important Points To Note Each character of the string occupies one byte of memory. Last character I always ‘\0’.It is not compulsory to write ‘\0’ at the end of the string. Compiler automatically puts ‘\0’ at the end of the character array Declaration and initialization of string : Declaration and initialization of string char name[]=“INDIA” ; The c compiler inserts the NULL (\0) character automatically at the end of the string. So initialization of null character is not essential. 11/14/2010 Rajni Bhalla CSE/IT Char arrays can be intialized as follows:- : Char arrays can be intialized as follows:- a) char name[5]={‘i’ , ‘n’, ‘d’ , ‘I’, ‘a’}; b) char name[6]={‘i’ , ‘n’, ‘d’ , ‘I’, ‘a’}; In case(a) the output will not be India but it contains some garbage value. Arguments in this example are initialized with [6],which is exactly equal no of characters inside the braces.The Null character must be included nd hence,the argument must be [7] instead of [6]. 11/14/2010 Rajni Bhalla CSE/IT WAP to display the output when the account of null character is not considered. : WAP to display the output when the account of null character is not considered. #include<stdio.h> void main() { char name[5]={‘i’ , ‘n’, ‘d’ , ‘I’, ‘a’}; clrscr(); printf(“Name1= %s”,name); }output would be india followed by garbage value. To get the correct result the argument must be [7]. 11/14/2010 Rajni Bhalla CSE/IT Output To display a successive string in case 1st string is not terminated : To display a successive string in case 1st string is not terminated #include<stdio.h> void main() { char name1[5]={‘i’ , ‘n’, ‘d’ , ‘I’, ‘a’}; char name2[6]={‘i’ , ‘n’, ‘d’ , ‘I’, ‘a’}; clrscr(); printf(“Name1= %s”,name1); printf(“Name1= %s”,name2); } The compiler reads the second string immediately followed by the first string because the end of first string is not indentified.Because of this second string is printed followed by the first string. 11/14/2010 Rajni Bhalla CSE/IT Output: indiaindia String Formats with different precision : String Formats with different precision main() { char text[15]=“PRABHAKAR”; printf(“%s\n”,text); printf(“%.5s\n”,text); printf(“%.8s\n”,text); printf(“%.15s\n”,text); printf(“%-10.4s\n”,text); printf(“%11s\n”,text); } 11/14/2010 Rajni Bhalla CSE/IT Use the while loop and print out the elements of the character array : Use the while loop and print out the elements of the character array main() { Char text[]={“have a nice day”}; Int i=0; While(i<=15) { printf(“%c”,text[i]); i++; } } 11/14/2010 Rajni Bhalla CSE/IT You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
A string in C language is a variable rajni.b27 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 778 Category: Education License: Some Rights Reserved Like it (0) Dislike it (0) Added: November 14, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... By: rupashicva (16 month(s) ago) plz,send me this ppt on rupashicva@rediffmail.com Saving..... Post Reply Close Saving..... Edit Comment Close Premium member Presentation Transcript A string in C language is a variable length array of characters that is delimited by null character(‘\0’) : A string in C language is a variable length array of characters that is delimited by null character(‘\0’) 11/14/2010 Rajni Bhalla CSE/IT Storing Strings : Storing Strings A string is stored in an array of characters. It is terminated by by the null character(‘\0’). 11/14/2010 Rajni Bhalla CSE/IT End of string Introduction : Introduction Group of characters ,digits,and symbols enclosed within quotation marks are called strings. Character arrays are called strings. Every string is terminated by ‘\0’(null character). Eg : char name[]={‘I’ , ’n’ , ’d’ , ’I’ , ’a’ ,’\0’}; 11/14/2010 Rajni Bhalla CSE/IT Slide 4: 11/14/2010 Rajni Bhalla CSE/IT Important Points To Note Each character of the string occupies one byte of memory. Last character I always ‘\0’.It is not compulsory to write ‘\0’ at the end of the string. Compiler automatically puts ‘\0’ at the end of the character array Declaration and initialization of string : Declaration and initialization of string char name[]=“INDIA” ; The c compiler inserts the NULL (\0) character automatically at the end of the string. So initialization of null character is not essential. 11/14/2010 Rajni Bhalla CSE/IT Char arrays can be intialized as follows:- : Char arrays can be intialized as follows:- a) char name[5]={‘i’ , ‘n’, ‘d’ , ‘I’, ‘a’}; b) char name[6]={‘i’ , ‘n’, ‘d’ , ‘I’, ‘a’}; In case(a) the output will not be India but it contains some garbage value. Arguments in this example are initialized with [6],which is exactly equal no of characters inside the braces.The Null character must be included nd hence,the argument must be [7] instead of [6]. 11/14/2010 Rajni Bhalla CSE/IT WAP to display the output when the account of null character is not considered. : WAP to display the output when the account of null character is not considered. #include<stdio.h> void main() { char name[5]={‘i’ , ‘n’, ‘d’ , ‘I’, ‘a’}; clrscr(); printf(“Name1= %s”,name); }output would be india followed by garbage value. To get the correct result the argument must be [7]. 11/14/2010 Rajni Bhalla CSE/IT Output To display a successive string in case 1st string is not terminated : To display a successive string in case 1st string is not terminated #include<stdio.h> void main() { char name1[5]={‘i’ , ‘n’, ‘d’ , ‘I’, ‘a’}; char name2[6]={‘i’ , ‘n’, ‘d’ , ‘I’, ‘a’}; clrscr(); printf(“Name1= %s”,name1); printf(“Name1= %s”,name2); } The compiler reads the second string immediately followed by the first string because the end of first string is not indentified.Because of this second string is printed followed by the first string. 11/14/2010 Rajni Bhalla CSE/IT Output: indiaindia String Formats with different precision : String Formats with different precision main() { char text[15]=“PRABHAKAR”; printf(“%s\n”,text); printf(“%.5s\n”,text); printf(“%.8s\n”,text); printf(“%.15s\n”,text); printf(“%-10.4s\n”,text); printf(“%11s\n”,text); } 11/14/2010 Rajni Bhalla CSE/IT Use the while loop and print out the elements of the character array : Use the while loop and print out the elements of the character array main() { Char text[]={“have a nice day”}; Int i=0; While(i<=15) { printf(“%c”,text[i]); i++; } } 11/14/2010 Rajni Bhalla CSE/IT