Learn C Language 

Learn C Programming Language Step By Step. It’s easy to learn c programming language with real-life examples and hands-on. We are trying our best to deliver quality content.

Strings In C

 

[adinserter name="Block 7"]

« Previous Page                                                                                                                               Next Page »

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

[et_pb_dmb_code_snippet code=”Y2hhciBncmVldGluZ1s2XSA9IHsnSCcsICdlJywgJ2wnLCAnbCcsICdvJywgJ1wwJ307Cg==” _builder_version=”4.0.9″]Y2hhciBncmVldGluZ1s2XSA9IHsnSCcsICdlJywgJ2wnLCAnbCcsICdvJywgJ1wwJ307Cg==[/et_pb_dmb_code_snippet]

If you follow the rule of array initialization then you can write the above statement as follows −

[et_pb_dmb_code_snippet code=”Y2hhciBncmVldGluZ1tdID0gIkhlbGxvIjsK” _builder_version=”4.0.9″]Y2hhciBncmVldGluZ1tdID0gIkhlbGxvIjsK[/et_pb_dmb_code_snippet]

Following is the memory presentation of the above-defined string in C/C++ −

Index 0 1 2 3 4 5
Variable H e l l o \0
Address 0×A 0×(A+1) 0×(A+2) 0×(A+3) 0×(A+4) 0×(A+5)

 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 −

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbiAoKSB7CgogICBjaGFyIGdyZWV0aW5nWzZdID0geydIJywgJ2UnLCAnbCcsICdsJywgJ28nLCAnXDAnfTsKICAgcHJpbnRmKCJHcmVldGluZyBtZXNzYWdlOiAlc1xuIiwgZ3JlZXRpbmcgKTsKICAgcmV0dXJuIDA7Cn0=” _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbiAoKSB7CgogICBjaGFyIGdyZWV0aW5nWzZdID0geydIJywgJ2UnLCAnbCcsICdsJywgJ28nLCAnXDAnfTsKICAgcHJpbnRmKCJHcmVldGluZyBtZXNzYWdlOiAlc1xuIiwgZ3JlZXRpbmcgKTsKICAgcmV0dXJuIDA7Cn0=[/et_pb_dmb_code_snippet]

When the above code is compiled and executed, it produces the following result:

[et_pb_dmb_code_snippet code=”R3JlZXRpbmcgbWVzc2FnZTogSGVsbG8K” _builder_version=”4.0.9″]R3JlZXRpbmcgbWVzc2FnZTogSGVsbG8K[/et_pb_dmb_code_snippet]

 [adinserter name=”Block 3″]

C supports a wide range of functions that manipulate null-terminated strings −

Sr.No. Function & Purpose
1

strcpy(s1, s2);

Copies string s2 into string s1.

2

strcat(s1, s2);

Concatenates string s2 onto the end of string s1.

3

strlen(s1);

Returns the length of string s1.

4

strcmp(s1, s2);

Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.

5

strchr(s1, ch);

Returns a pointer to the first occurrence of character ch in string s1.

6

strstr(s1, s2);

Returns a pointer to the first occurrence of string s2 in string s1.

The following example uses some of the above-mentioned functions:

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCmludCBtYWluICgpIHsKCiAgIGNoYXIgc3RyMVsxMl0gPSAiSGVsbG8iOwogICBjaGFyIHN0cjJbMTJdID0gIldvcmxkIjsKICAgY2hhciBzdHIzWzEyXTsKICAgaW50ICBsZW4gOwoKICAgLyogY29weSBzdHIxIGludG8gc3RyMyAqLwogICBzdHJjcHkoc3RyMywgc3RyMSk7CiAgIHByaW50Zigic3RyY3B5KCBzdHIzLCBzdHIxKSA6ICAlc1xuIiwgc3RyMyApOwoKICAgLyogY29uY2F0ZW5hdGVzIHN0cjEgYW5kIHN0cjIgKi8KICAgc3RyY2F0KCBzdHIxLCBzdHIyKTsKICAgcHJpbnRmKCJzdHJjYXQoIHN0cjEsIHN0cjIpOiAgICVzXG4iLCBzdHIxICk7CgogICAvKiB0b3RhbCBsZW5naHRoIG9mIHN0cjEgYWZ0ZXIgY29uY2F0ZW5hdGlvbiAqLwogICBsZW4gPSBzdHJsZW4oc3RyMSk7CiAgIHByaW50Zigic3RybGVuKHN0cjEpIDogICVkXG4iLCBsZW4gKTsKCiAgIHJldHVybiAwOwp9″ _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCmludCBtYWluICgpIHsKCiAgIGNoYXIgc3RyMVsxMl0gPSAiSGVsbG8iOwogICBjaGFyIHN0cjJbMTJdID0gIldvcmxkIjsKICAgY2hhciBzdHIzWzEyXTsKICAgaW50ICBsZW4gOwoKICAgLyogY29weSBzdHIxIGludG8gc3RyMyAqLwogICBzdHJjcHkoc3RyMywgc3RyMSk7CiAgIHByaW50Zigic3RyY3B5KCBzdHIzLCBzdHIxKSA6ICAlc1xuIiwgc3RyMyApOwoKICAgLyogY29uY2F0ZW5hdGVzIHN0cjEgYW5kIHN0cjIgKi8KICAgc3RyY2F0KCBzdHIxLCBzdHIyKTsKICAgcHJpbnRmKCJzdHJjYXQoIHN0cjEsIHN0cjIpOiAgICVzXG4iLCBzdHIxICk7CgogICAvKiB0b3RhbCBsZW5naHRoIG9mIHN0cjEgYWZ0ZXIgY29uY2F0ZW5hdGlvbiAqLwogICBsZW4gPSBzdHJsZW4oc3RyMSk7CiAgIHByaW50Zigic3RybGVuKHN0cjEpIDogICVkXG4iLCBsZW4gKTsKCiAgIHJldHVybiAwOwp9[/et_pb_dmb_code_snippet]

When the above code is compiled and executed, it produces the following result −

[et_pb_dmb_code_snippet code=”c3RyY3B5KCBzdHIzLCBzdHIxKSA6ICBIZWxsbwpzdHJjYXQoIHN0cjEsIHN0cjIpOiAgIEhlbGxvV29ybGQKc3RybGVuKHN0cjEpIDogIDEw” _builder_version=”4.0.9″]c3RyY3B5KCBzdHIzLCBzdHIxKSA6ICBIZWxsbwpzdHJjYXQoIHN0cjEsIHN0cjIpOiAgIEhlbGxvV29ybGQKc3RybGVuKHN0cjEpIDogIDEw[/et_pb_dmb_code_snippet]

[adinserter name=”Block 7″]

Top Book Suggestion

The two books on C Programming Language that I Personally Recommend. I really loved reading these books. These books are listed for quality content, easy steps, and affordable price. You can get it from Amazon and Flipkart. 

 

Check The Price On Amazon:

1. C Programming Beginner’s Guide

2. Head First C: A Brain-Friendly Guide

Check The Price On Flipkart:

1. Introduction to C Programming 

2. Head First C: A Brain-Friendly Guide

« Previous Page                                                                                                                               Next Page »

[adinserter block=”6″]

Buy Premium Courses At Lowest Price. Become A Certified Developer Today!

Best Books Selected by Our Experts

Buy Premium Projects At Lowest Price

Ebooks Written by Our Experts

CoursePot.Com

Buy Premium Courses At Lowest Price. Grab Golden Opportunity To Become A Certified Developer Today!

Useful Links

Pin It on Pinterest

Share This