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

 

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

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

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 −

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

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:

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

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 »

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