C Programming Cheat Sheet - 6
Tags: c
Published: 2021-04-11 14:09:56

###6 Strings
####6.1 What is String?

Strings are arrays of characters. Each member of array contains one of characters in the string.

Example
```
#include<stdio.h> 
main() 
{ 
    char name[20]; 
    printf("Enter your name : "); 
    scanf("%s",name); 
    printf("Hello, %s , how are you ?\n",name); 
} 
```
Output Results:
```
Enter your name : Vineet 
Hello, Vineet, how are you ?
```
If user enters "Vineet" then the first member of array will contain 'V' , second cell will contain 'i' and so on. C determines end of a string by a zero value character. We call this character as `NULL` character and show it with `\0` character. (It's only one character and its value is 0, however we show it with two characters to remember it is a character type, not an integer).

Equally we can make that string by assigning character values to each member.
```
name[0]='B'; 
name[1]='r'; 
name[2]='i'; 
name[3]='a'; 
name[4]='n'; 
name[5]='\0';
```
As we saw in above example placeholder for string variables is `%s`. Also we will not use a `&` sign for receiving string values.
####6.2 Point to Note

While entering the string using `scanf()` we must be cautious about
two things:

    * The length of the string should not exceed the dimension of the character array. This is because the C compiler doesn’t perform bounds checking on character arrays.
    * `scanf()` is not capable of receiving multi-word strings. Therefore names such as "Vineet Choudhary" would be unacceptable. The way to get around this limitation is by using the function `gets()`.The usage of functions `gets()` and its counter part `puts()` is shown below.
```
#include<stdio.h> 
main( ) 
{ 
    char name[25] ; 
    printf ("Enter your full name ") ; 
    gets (name) ; 
    puts ("Hello!") ; 
    puts (name) ; 
} 
```
And here is the output...
```
Enter your name Vineet Choudhary 
Hello! 
Vineet Choudhary
```
The program and the output are self-explanatory except for the fact that, `puts()` can display only one string at a time (hence the use of two `puts()` in the program above). Also, on displaying a string, unlike `printf()`, `puts()` places the cursor on the next line. Though `gets()` is capable of receiving only one string at a time, the plus point with `gets()` is that it can receive a multi-word string.

If we are prepared to take the trouble we can make `scanf()` accept multi-word strings by writing it in this manner:
```
char name[25] ; 
printf ("Enter your full name ") ; 
scanf ("%[^\n]s", name) ;
```
Though workable this is the best of the ways to call a function, you would agree.
####6.3 Standard Library String Functions

With every C compiler a large set of useful string handling library functions are provided in `string.h` file.

    * `strlen` - Finds length of a string
    * `strlwr` - Converts a string to lowercase
    * `strupr` - Converts a string to uppercase
    * `strcat` - Appends one string at the end of another
    * `strncat` - Appends first n characters of a string at the end of
    another
    * `strcpy` - Copies a string into another
    * `strncpy` - Copies first n characters of one string into another
    * `strcmp` - Compares two strings
    * `strncmp` - Compares first n characters of two strings
    * `strcmpi` - Compares two strings without regard to case ("i" denotes
    that this function ignores case)
    * `stricmp` - Compares two strings without regard to case (identical to
    strcmpi)
    * `strnicmp` - Compares first n characters of two strings without regard
    to case
    * `strdup` - Duplicates a string
    * `strchr` - Finds first occurrence ofa given character in a string
    * `strrchr` - Finds last occurrence ofa given character in a string
    * `strstr` - Finds first occurrence of a given string in another string
    * `strset` - Sets all characters ofstring to a given character
    * `strnset` - Sets first n characters ofa string to a given character
    * `strrev` - Reverses string

Make sure to Follow me here, to get the update when i post a blog.