C strings

A string is a 1-d character array terminated by a null(‘\0’) => {this is null character}

The null character is used to denote string termination, characters are stored in contiguous memory locations.

Inshort , in C programming, the one-dimensional array of characters are called strings, which is terminated by a null character ‘\0’.

When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.

Initializing Strings

Since string is an array of characters, it can be initialized as follows:

char s[]={‘P’,’R’,’O’,’G’,’R’,'A','M','I'’\0’}

There is another shortcut for initializing strings in c language:

char s[]=”Programiz”;    => In this case C adds a null character automatically.
Strings in memory

A string is sorted just like an array in the memory as shown below

Quick Quiz: Create a string using ” ” and print its content using a loop.

Printing Strings

A string can be printed character by character using printf and %c.

But there is another convenient way to print strings in C.

char st[] = ”Programiz”;

printf(“%s”,st);    => prints the entire string
Taking string input from the user

We can use %s with scanf to take string input from the user:

char st[50];

scanf(“%s”,&st);

scanf automatically adds the null character when the enter key is pressed.

Note:

  1. The string should be short enough to fit into the array.
  2. scanf cannot be used to input multi-word strings with spaces.
gets() and puts()

gets() is a function that can be used to receive a multi-word string.

char st[30];

gets(st);   => the entered string is stored in st!

Multiple gets() calls will be needed for multiple strings.

Likewise, puts can be used to output a string.

puts(st);   =>Prints the string and places the cursor on the next line

Declaring a string using pointers

We can declare strings using pointers

char *ptr= ”Programiz”;

This tells the compilers to store the string in the memory and the assigned address is stored in a char pointer.

Note:

  1. Once a string is defined using char st[]= ”Programiz”, it cannot be initialized to something else.
  2. A string defined using pointers can be reinitialized.   => ptr=”rohan”;

Standard library functions for Strings

C provides a set of standard library functions for strings manipulation.

Some of the most commonly used string functions are:

strlen() – This function is used to count the number of characters in the string excluding the null (‘\0’) character.

int length=strlen(st);

These functions are declared under <string.h> header file.

strcpy() – This function is used to copy the content of second string into first string passed to it.

char source[ ]= ”Programiz”;

char target[30];

strcpy(target,source);    => target now contains “Programiz”

Target string should have enough capacity to store the source string.

strcat() – This function is used to concatenate two strings

char s1[11]= ”Hello”;

char s2[ ]= ”Programiz”;

strcat(s1,s2);                   => s1 now contains “Hello Programiz” <No space in between>

strcmp() – This function is used to compare two strings. It returns: 0 if strings are equal

Negetive value if first strings mismatching character’s ASCII value is not greater than second string’s corresponding mismatching character. It returns positive values otherwise.

strcmp(“For”, “Joke”);                      => positive value

strcmp(“Joke”, “For”);                      => Negative value

Exercise on C Strings

  1. Which of the following is used to appropriately read a multi-word string-
  • Gets()
  • Puts()
  • Printf()
  • Scanf()
  1. Write a program to take a string as an input from the user using %c and %s. Confirm that the strings are equal.
  2. Write your own version of strlen function from <string.h>
  3. Write a function slice() to slice a string. It should change the original string such that it is now the sliced strings. Take m and n as the start and ending position for slice.
  4. Write your own version of strcpy function from <string.h>
  5. Write a program to encrypt a string by adding 1 to the ASCII value of its characters.
  6. Write a program to decrypt the string encrypted using the encrypt function in problem 6.
  7. Write a program to count the occurrence of a given character in a string.
  8. Write a program to check whether a given character is present in a string or not.

Was this article helpful?
YesNo

Leave a Comment

Your email address will not be published. Required fields are marked *