Strings in C Lan
Strings in C Lan
The group of alpha numeric characters which are enclosed within double quotes (“ ”) are called Strings,.
• Every String is terminated with NULL (‘\0’) character.
• Character array is also called string
• It is not compulsory to write ‘\0’ character at the end of the string, the compiler automatically puts ‘\0’ at the end of string
of a character array.
Declaration syntax: char stringname[size];
Example: char name[30];
Initialization syntax: char stringname[size]=” string value “;
Ex: Char name[30] = “KIET”
K I E T \0
1.STRLEN():The function is used to count the number of characters in the given string
SYNTAX: Len=strlen(“text/string”);
EXAMPLE:
void main()
{
Int l;
char s[100];
printf(“Enter the string”);
gets(s);
l=strlen(s);
printf(“length of string s : %d”, l);
}
5. STRCMP():The function is used to compare two strings i.e. equal or not equal.
SYNTAX: strcmp(str1,str2);
EXAMPLE:
void main()
{
char a[100],b[100];
int cmp;
printf(“Enter string a”);
gets(a);
printf(“Enter string b”);
gets(b);
cmp= strcmp(a,b);
if (cmp>0)
{
printf(“b is less than a”);
}
}
#include <stdio.h>
#include <conio.h>
void main()
{
char st[20];
int i,j,l,c;
printf("Enter string:");
scanf("%s",&st);
l=0;
for(i=0;st[i]!='\0';i++) // finding length
l=l+1
for(i=0,j=l-1,c=0;st[i] !='\0';i++,j--)
{
if ( st[i] == st[j] )
c++;
}
if( c == l )
printf("\nGiven string is palindrome ");
else
printf("\nGiven string is not palindrome ");
}