0% found this document useful (0 votes)
13 views3 pages

LO4 Strings

C Programming use of Arrays

Uploaded by

jaltiti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

LO4 Strings

C Programming use of Arrays

Uploaded by

jaltiti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
16. STRINGS Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null. The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello." char greeting[6] = {'H', ‘e’, ‘1', ‘L', '0', ‘\O"}; If you follow the rule of array initialization, then you can write the above statement as follows: char greeting[] = "Hello"; Following is the memory presentation of the above defined string in C/C+ rr er er Naratle Pe fede fle ¥ Address onz34si_ | 0123452 | oxazasa | On2345¢ | oxaaess | 0123436 Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '\0' at the end of the string when it initializes the array. Let us try to print the above mentioned string: #include int main () { char greeting(6] = (‘H', printf ("Greeting message: %s\n", greeting ); 117 ¢ Programming return @; } When the above code is compiled and executed, it produces the following result: Greeting message: Hello C supports a wide range of functions that manipulate null-terminated strings: S.N. Function & Purpose 1 strepy(s1, 52); Copies string s2 into string s1. 2 streat(s1, 52); Concatenates string s2 onto the end of string s1. 3 strlen(s1); Returns the length of string s1 4 stremp(s1, 52); Returns 0 if s1 and s2 are the same; less than 0 if s1s2. 5 strehr(si, ch); Returns a pointer to the first occurrence of character ch in string s1. 6 _ strstr(si, s2); Returns a pointer to the first occurrence of string s2 in string st. The following example uses some of the above-mentioned functions: #include include int main () 118 CG) tutorialspoint ¢ Programming i char stri[12) ello"; char str2[12] = "world"; char str3[12]; int len ; /* copy stri into ste3 */ strepy(str3, str1); printf("strepy( str3, strt) : s\n", str3 5 /* concatenates stri and str2 */ streat( stra, str2); printf("streat( stri, str2): s\n", stra )5 /* total lenghth of stri after concatenation */ len = strlen(str1); printf("strien(strt) : d\n", len ); return 0; y When the above code is compiled and executed, it produces the following result: strepy( str3, str) : Hello streat( str1, str2): strlen(str1) : 10 Helloworld 119 CG) tutorialspoint

You might also like