0% found this document useful (0 votes)
34 views

String Handling Functions in C

C programming

Uploaded by

Yash Jade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

String Handling Functions in C

C programming

Uploaded by

Yash Jade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

String Handling Functions in C

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:

C Program to Concatenate Two


Strings without using strcat()
 #include<stdio.h>
 #include<conio.h>
 #include<string.h>
 void main()
 {
 char sk[10], rk[10], st[20];
 int i,j;
 clrscr();
 printf("Enter first string :");
 scanf("%s",sk);
 printf("Enter second string :");
 scanf("%s",rk);
 i=0;
 j=0;
 while(sk[i]!='\0')
 {
 st[j]=sk[j];
 i++;
 j++;
 }
 st[j]=' ';
 j++;
 i=0;
 while(rk[i]!='\0')
 {
 st[j]=rk[i];
 i++;
 j++;
 }
 st[j]='\0';
 printf("The Resultant string is %s",st);
 getch();
 }
Output:
C Program to Convert Lowercase
to Uppercase
 #include<stdio.h>
 #include<conio.h>
 #include<string.h>
 void main()
 {
 char sk[15];
 int i;
 clrscr();
 printf("Enter a string :");
 gets(sk);
 for(i=0;i<strlen(sk);i++)
 if(sk[i]>='a' && sk[i]<= 'z')
 sk[i]=sk[i]-32;
 printf("The converted upper case string is %s", sk);
 getch();
 }
Output:

You might also like