C String

String is a collection of characters or String is an array of character which is terminated by a null character '\0'. To input a string use format '%s'. String is denoted in double quotes ("").

scanf("%s",&variable-name);

The limitation of using scanf() is that it will not accept spaces while inputting a string. To accept string with spaces or sentence use following syntax:

scanf("%[^\n]s", variable-name);

Another way of inputting a string including spaces is by using gets() function, gets is a string function in stdio.h, in which get is for input and s is for string/sentence. Syntax:

gets(a); //a is variable name of type string

Example: String input/output

Example 1: WAP to Input a string using scanf() and display it.

void main()
{
   char a[20];
   clrscr();
   printf("Enter string: ");
   scanf("%s",&a);
   printf("Output: %s",a);
   getch();
}
Enter string: Programming is interesting
Output: Programming

Example 2: WAP to Input a string using gets and display it.

void main()
{
   char a[20];
   clrscr();
   printf("Enter string: ");
   gets(a);
   puts(a); //printf("%s",a);
   getch();
}
Enter string: Programming is interesting
Programming is interesting

Example 3: WAP to initialize a string

void main()
{
   char s1[] = "C-Language";
   char s2[] = {'C','-','L','a','n','g','u','a','g','e'};
   char s3[10];
   clrscr();
   s3[0] = 'C';
   s3[1] = '-';
   s3[2] = 'L';
   s3[3] = 'a';
   s3[4] = 'n';
   s3[5] = 'g';
   s3[6] = 'u';
   s3[7] = 'a';
   s3[8] = 'g';
   s3[9] = 'e';
   printf("S1 = %s\n",s1);
   printf("S2 = %s\n",s2);
   printf("S3 = %s\n",s3);
   getch();
}
S1 = C-Language
S2 = C-Language
S3 = C-Language

Built-in String functions

String functions in string.h
S.No.FunctionSyntaxDescription
strlenint i=strlen(a);Calculate length of String and return an integer value.
strrevstrrev(a);Reverse the string.
struprstrupr(a);Convert string into capital letters.
strlwrstrlwr(a);Convert string into small letters.
strcpystrcpy(b,a);Copy contents of a into b.
strcatstrcat(b,a);Append contents of one string into another.
strcmpint i=strcmp(a,b);Compare two strings and return a integer value. Value is one of the following:
a. Return 0 if both string are equal
b. Return Greater than 0 if 1st string is greater
c. Return Less than 0 if 2nd string is greater.
strcmpi or stricmplen=strcmpi(a,b);Compare 2 strings same as strcmp but it will ignore cases. i means ignore cases.
strncmpstrncmp(b,a,n);Compare two Strings but only 1st n specified characters, where n is a integer number.
strncmpistrncmpi(b,a,n);Compare two Strings but only 1st n specified characters also it will ignore cases.
strncpystrncpy(b,a,n);Copy only n specified characters.
strncatstrncat(b,a,n);Concatenate only n specified characters.
Strlen() function: use to find length of a string

Example 1: Input string and find its length.

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

void main()
{
    char name[20];
    int len;
    clrscr();
    printf("Enter name");
    gets(name);
    len = strlen(name);
    printf("Length = %d",len);
    getch();
}
Strrev() function: use to reverse the string

Example 2: Input string and reverse it.

void main()
{
    char s[20];
    clrscr();
    printf("Enter String");
    gets(s);
    strrev(s);
    printf("Reverse String is %s", s);
    getch();
}
Strupr() function: convert lowercase letters into uppercase.

Example 3: Input string and convert it into capital letters.

void main()
{
    char s[30];
    clrscr();
    printf("Enter string");
    gets(s);
    strupr(s);
    printf("String in Capital = %s",s);
    getch();
}
Strlwr() function: convert uppercase letters into lowercase.

Example 4: Input string and convert it into small letters.

void main()
{
    char s[30];
    clrscr();
    printf("Enter string");
    gets(s);
    strlwr(s);
    printf("String in Lower = %s",s);
    getch();
}
Strcpy() function: copy string

Example 5: Input string and copy into another variable.

void main()
{
    char s1[20], s2[20];
    clrscr();
    printf("Enter string");
    gets(s1); //input string
    strcpy(s2,s1); //copy string
    printf("After Copy String is: %s",s2); //output new string
    getch();
}