C Strings

In C Programming Language a string variable represents sequences of characters which is always enclosed in double quotes, like “Hello World” ended with null character (‘\0’). This null character represents the end of the string.String variable can be any length.

In C Programming Language we do not have a string data type like other programming languages, in c we can define string as an array of characters or a pointer to characters.
<h2>Declaring strings</h2>
<h2>Declaring string as array of characters</h2>
<pre class=”lang:default decode:true”>char s[]=”string”;
OR,
char s[7]=”string”;
OR,
char s[]={‘s’,’t’,’r’,’i’,’n’,’g’,’\0′};
OR;
char s[7]={‘s’,’t’,’r’,’i’,’n’,’g’,’\0′};</pre>
<h2>Declaring string as a pointer to characters sequence</h2>
<pre class=”lang:default decode:true”>char* s = “string”;</pre>
<h2>Example – C program to read string from terminal</h2>
<pre class=”lang:default decode:true”>#include &lt;stdio.h&gt;
int main(){
char name[20];
printf(“Enter your name: “);
scanf(“%s”,name);
printf(“Your name is %s.”,name);
return 0;
}</pre>
<strong>Output:</strong>
<pre class=”lang:default decode:true”>Enter your name: steve
Your name is steve.</pre>
<h2>C String functions</h2>
In C Programming Language we have a rich set of function for string operations like –
<ul>
<li>Counting the length of a string.</li>
<li>Comparing two strings.</li>
<li>Copying one string to another.</li>
<li>Converting lower case string to upper case.</li>
<li>Converting upper case string to lower case.</li>
<li>Joining two strings.</li>
<li>Reversing string.</li>
</ul>
We must have included string.h header file before using these functions.
<table class=”table”>
<tbody>
<tr>
<td><strong> Function</strong></td>
<td><strong> Description</strong></td>
</tr>
<tr>
<td>strcat (str1, str2)</td>
<td> Concatenates str2 at the end of str1.</td>
</tr>
<tr>
<td>strcpy (str1, str 2)</td>
<td> Copies str2 into str1</td>
</tr>
<tr>
<td>strlen (strl)</td>
<td> It returns the length of str1.</td>
</tr>
<tr>
<td>strcmp (str1, str2)</td>
<td> Returns 0 if str1 is same as str2. Returns &lt;0 if strl &lt; str2. Returns &gt;0 if str1 &gt; str2.</td>
</tr>
<tr>
<td>strchr (str1, char)</td>
<td> Returns pointer to first occurrence of char in str1.</td>
</tr>
<tr>
<td>strstr (str1, str2)</td>
<td> Returns pointer to first occurrence of str2 in str1.</td>
</tr>
<tr>
<td>strcmpi (str1, str2)</td>
<td> Same as strcmp() function. But, this function negotiates case. “A” and “a” are treated as same.</td>
</tr>
<tr>
<td>strdup()</td>
<td> duplicates the string</td>
</tr>
<tr>
<td>strlwr()</td>
<td> converts string to lowercase</td>
</tr>
<tr>
<td>strncat()</td>
<td> appends a portion of string to another</td>
</tr>
<tr>
<td>strncpy()</td>
<td> copies given number of characters of one string to another</td>
</tr>
<tr>
<td>strrchr()</td>
<td> last occurrence of given character in a string is found</td>
</tr>
<tr>
<td>strrev()</td>
<td> reverses the given string</td>
</tr>
<tr>
<td>strset()</td>
<td> sets all character in a string to given character</td>
</tr>
<tr>
<td>strupr()</td>
<td> converts string to uppercase</td>
</tr>
<tr>
<td>strtok()</td>
<td> tokenizing given string using delimiter</td>
</tr>
</tbody>
</table>