Skip to main content

Strings Implementation in C Language

C Language Beginner level

Strings :

As we know , Any string is the collection of various characters or the collection of Characters , Numbers or Symbols . For example we can say " Author@89 " is a string and is the collection of the various characters , numbers , symbols  as A , u , t ,h ,o ,r ,@ ,8 ,9 .

So Come to the Strings use in the C Language -

  • To Store a String in variables in C language is not possible So we use the Array Concept to store the various characters . 
  • While performing any Action on a string like Copy to other string , Concatenation ( To add two strings ) , Splitting ( To break the string into parts ) etc , we need the Array to store a particular string in the C Language . 
  • When we are using Array to store a string , This string must be of similar or character data type , means each string can contain only characters .
  • If an Array Box[20] stores the string " Author " , It is Right but The string " Author89 " is not valid . 
How to Initialize a String in C :

we can use the syntax as - 

#include <stdio.h>
void main()
char Box[20] = { 'A' , 'u' ,'t' ,'h' ,'o' ,'r'} ;
printf(" The string inside Box Array is %c " , Box ) ;

return 0 ;

Note :

  • Here Array Box can hold the maximum 20 elements and currently holding the elements A,u,t,h,o,r  and the printf statement will print all characters as a string . 
Some basic operations over Strings :
  • Concatenation - To add two strings . This can be implemented by strcat( s1 , s2 ) function , where s1 is the first and s2 is the second string . 
  • Copy - We use strcpy( s1 , s2 ) function to perform copy operation . This copy the s2 string into s1 string . 
  • To calculate the string length - We use strlen(s) and this shows the length of string of s . 
  • To compare two strings - We use strcmp( s1 , s2 ) . If the string s1 and s2 are same it returns a boolean True or 1 value . 
Let's see the various Conditional Statements in the next Page .

                                                 << Back            >>Next