Strings in C

Strings :

In c programming language, strings are represented as character array terminated by a null character at the end.

Declaring strings:

char string-name[max-size];


char str[20];

String have one null character i.e. ‘\0’ at the end which represents the end of string. Thus, array size should be one more than a string size.

A string can be initialised in following ways:

char str[4]={‘h’,’e’,’y’,’\0′};

char str[4]=”hye”;

An array of string can be declared as:

char array_name[size_array][maxsize_string];

Array of strings can be initialised as:

char str[3][6]={{‘h’,’y’,’e’,’\0′},{‘h’,’e’,’l’,’l’,’o’,’\0′},{‘b’,’y’,’e’,’\0′}};

char str[][]={“hye”,”hello”,”bye”};

Both the above, statements initialise a string array of size 3 with max size of each string 6.

Inputting string:

A string is a character so it can be printed and input as an array element till ‘\0’ character is encountered.


%s format specifier can also be used to input and print string type values.


char a[5];



But %s can’t be used to input multi length string like “hello world”.

For that we use %[^\n]s instead of %s. This inputs strings until \n (newline) is encountered


scanf(” %[^\n]s”,a);

Also,functions gets() and puts() can be used to input and print multi word strings.

puts() prints a newline after every string.


Pointers and String :

Since, strings are character array. Similar concept of pointers in array applies to it i.e.

str[i] is equivalent to *(str+i) or i[str].

We can also initialise using char pointer as

char *str=”Good Bye”;

Since, a string is initialised it can’t be initialised again and one string can’t be assigned a value of other string. But with pointers we can do both tasks :


In C language, the header file used to use string based functions is string.h

Some commonly used predefined function are :

1. strcpy(a,b) : It copies the string b into string a.

2. strrev(a) : It reverses a string stored at a.

3. strcmp(a,b) : It compares string a and b and returns 0 if both are equal and difference of ASCII value of first different characters in a and b ,if both are unequal.

4. strcat(a,b) : It concatenates string a and b and stores in a.

5. strlen(a) : It returns the length of string a.

6. strchr(a,b) : It returns the pointer to first occurence of character b.

7. strstr(a,b) : It returns the pointer to first occurrence of string b in a.

Leave a Reply

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