In C language, an array of characters is known as a string. It has the following syntax :
char sk[15]; // It declares a string array with 15 characters.
There are four types of String Handling Functions in the C language.
1. strlen() 2. strcpy() 3. strcat() 4. strcmp()
strlen(): This function is used to find the length of a character string.
Syntax: int n; char sk[15]="Las Vegas"; n=strlen(sk); strcpy(): This function is used to copy a character string to a character variable. Syntax: char sk[15]; strcpy(sk, "Las Vegas"); strcat(): This function is used to join character strings. Syntax: char sk[15]="Las Vegas"; char pin[10]="88905"; strcat(sk,pin); strcmp(): This function is used to compare two character strings. Syntax: char sk[15]="Las Vegas"; char rp[15]=" New York"; strcmp(sk,rp);
C program to count number of
vowels in a string #include<stdio.h> #include<conio.h> #include<string.h> void main() { char sk[10]; int count=0,i; clrscr(); printf("Enter a String :"); gets(sk); for(i=0;i<strlen(sk);i++) switch(sk[i]) { case 'A': case 'E': case 'I': case 'O': case 'U': case 'a': case 'e': case 'i': case 'o': case 'u': count++; break; } printf("Vowels are present in the string : %d", count); getch(); } Output:
C Program to Check whether the
Given String is a Palindrome #include<stdio.h> #include<conio.h> #include<string.h> void main() { char sk[15], rp[15]; int i, j; clrscr(); printf("Enter the string :"); scanf("%s",sk); i=0; j=strlen(sk)-1; while(j>=0) { rp[i]=sk[j]; i++; j--; } rp[i]='\0'; if(strcmp(sk,rp)==0) printf("%s is a palindrome string",sk); else printf("%s is not a palindrome string",sk); getch(); } Output: