
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 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();
}
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();
}
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();
}
| S.No. | Function | Syntax | Description |
|---|---|---|---|
| strlen | int i=strlen(a); | Calculate length of String and return an integer value. | |
| strrev | strrev(a); | Reverse the string. | |
| strupr | strupr(a); | Convert string into capital letters. | |
| strlwr | strlwr(a); | Convert string into small letters. | |
| strcpy | strcpy(b,a); | Copy contents of a into b. | |
| strcat | strcat(b,a); | Append contents of one string into another. | |
| strcmp | int 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 stricmp | len=strcmpi(a,b); | Compare 2 strings same as strcmp but it will ignore cases. i means ignore cases. | |
| strncmp | strncmp(b,a,n); | Compare two Strings but only 1st n specified characters, where n is a integer number. | |
| strncmpi | strncmpi(b,a,n); | Compare two Strings but only 1st n specified characters also it will ignore cases. | |
| strncpy | strncpy(b,a,n); | Copy only n specified characters. | |
| strncat | strncat(b,a,n); | Concatenate only n specified characters. |
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();
}
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();
}
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();
}
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();
}
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();
}
Ad: