C Strings - Internal Working and Functions with example

    0 Votes

Unlike other languages like C++, Java, etc. C does not provide any dedicated data type, rather it uses a character array to store strings. Since, in programming practices, we encounter frequently to handle strings. It is good to know how to do this in C.

Strings in C

As mentioned earlier, in C handles strings in a different manner, uses a character array to store. Now, after reading the Chapter: Arrays in C Language, you might have confused that there was something called character array and C uses also a character array to store strings, then how both gets differ with each other ?

Actually, there is a slight difference between simple character array and strings. A string always ends with ‘\0’ or NULL character which is not mandatory for a simple character array. This is the only difference between these you can say.

Initializing Strings in C

Strings in C can be initialized in various ways and for every method there are some important points you should remember.

char arr[] = {‘A’,’4’,’A’,’C’,’A’,’D’,’E’,’M’,’I’,’C’,’S’,’\0’};

This is the simplest way to initialize the strings. It is same like the character array initialization, but only difference is that there is a ‘\0’ character at the end. Here, the size of the array is not mentioned, so it will automatically allocate memory for 8 characters (8 bytes since a character takes 1 byte) including the ‘\0’ character.

Method 2:

char arr[] = “A4ACADEMICS”;

This is another way to initialize the array. Here, the compiler automatically inserts a ‘\0’ at the end.

Method 3:

char arr[10] = {‘A’,’4’,’A’,’C’,’A’,’D’,’E’,’M’,’I’,’C’,’S’,’\0’};

In this method, arr holds 10 byte of memory as per declaration. From the 0th index to 6th index it will hold the word “STRING” and 7th location holds ‘\0’ character and 8th and 9th location holds garbage values.

Method 4:

char *arr = “A4ACADEMICS”;

This is another interesting way to initialize strings. Specifically, this is called string literals. The most interesting thing in this method is that after this declaration you can’t change the value of the string. This will be constant string literals throughout its scope.

Array of Strings

You know that there are arrays of primitive data types like integers, characters, float etc. Question may be arise in your mind, array of strings is possible or not ?

Answer is yes.

One way to do this is using two dimensional character array and other is using array of character pointers.

Method 1 - using two dimensional char array :

char arr[][10] = {"We", "like", "to", "learn"};
0x2000 ‘W’ ‘e’ ‘\0’      
0x200A ‘l’ ‘i’ ‘k’ ‘e’ ‘\0’  
0x2014 ‘t’ ‘o’ ‘\0’      
0x201D ‘l’ ‘e’ ‘a’ ‘r’ ‘n’ ‘\0’

Memory representation of two dimensional array

All the shaded cells contain garbage values. First Column indicates memory locations of first byte of each string. As you see that in this case memory locations are contiguous.

Method 2 –using array of character pointers:

char *arr[] = {"We", "like", "to", "learn"};

Here, the difference between earlier is that since it is the array of character pointer of string literals, you can’t modify the strings, but you can replace it with new string literals or strings.

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

Commonly Used Functions :

The most commonly used functions in the string library ie. Using <string.h> are:

  • strcat - concatenate two strings
  • strchr - string scanning operation
  • strcmp - compare two strings
  • strcpy - copy a string
  • strlen - get string length
  • strncat - concatenate one string with part of another
  • strncmp - compare parts of two strings
  • strrchr - string scanning operation

strcat:

char *strcat(char *s1, const char *s2);

s1 is a pointer to a string that will be modified. s2 will be copied to the end of s1. s2 is a pointer to a string that will be appended to the end of s1. It returns a pointer to s1 (where the resulting concatenated string resides).

strcpy:

char *strcpy(char *s1, const char *s2);

s1 is a pointer to the destination. s2 will be copied to the end of s1. It returns a pointer to s1 (where the resulting copied string resides).

strlen:

int strlen(char *s1);

s1 is a pointer to the string. It returns the length of the string excluding ‘\0’ character.

Example –

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[])
{
/* Define a temporary variable */
char MyString[100];

/* Copy the first string into the variable */
strcpy(MyString, "a4");

/* Concatenate the following two strings to the end of the first one */
strcat(MyString, "academics ");
strcat(MyString, ".com");

/* Display the concatenated strings */
printf("%s\n", MyString);

/* Display the length of the string */
printf(“Length : %d\n”,strlen(MyString));

return 0;
}

Output –
a4academics.com
Length : 15

strchr :

char *strchr(const char *s, int c);

s is a string (terminated by a null character).c is the character to be found. It returns a pointer to the first occurrence of the character c within the string pointed to by s. If c isn't found, it returns a null pointer.

strcmp :

char *strcmp(const char *s1, const char *s2);

s1 and s2 a string (terminated by a null character). It returns the ASCII value difference of the first index of mismatched characters of the given string.

Example -

strcmp(“Samiran”,Saha);

It will return the difference between ASCII value of ‘m’ and ‘a’ ie. 12. Because first mismatch occurs at 3rd index.

strcmp(“Saha”,Saha);

It will return 0 because there is no mismatch in the given string.

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

Related Articles

C Overview and Basic Syntax
Data Types in C Language
C Qualifiers - Constant and Volatile type Qualifier
C Type Casting with examples - float to int / int to char / int to long
C Constants and Literals
C Operators - Arithmetic, Assignment, Relational and Logical
C Operators - Bitwise Operators and Conditional Operators
C Operator Precedence and Associativity Table with Examples
C Input and Output Statements
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Pointers basics explained with examples
Structures in C Programming with Example
Unions in c programming with example
C Storage Class Specifiers - auto, register, static and extern

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know