Strings in C programming Language

A string is a series of characters that ends with a null character i.e. “\0”
The size of the character list containing the string must be one greater than the number of characters in the word to accommodate the null character at the end of the array.

C supports following functions that makes our program easier while handling strings.

String Functions and their description

String Function Description (What it does?)
strlen(x) Find out the length of the string called “x”.
strcpy(destination, source) Copies the value from the source string to the destination string.
strcat(first_string, second_string) Joins the first string with the second string. And the result obtained is stored in the first string.
strcmp(first_string, second_string) It compares the first string with the second one. If both strings are the same, it returns 0.
strrev(x) It returns a reverse string.
strlwr(x) It converts all string characters in lowercase.
strupr(x) It converts all string characters in uppercase.

Examples of Strings Functions in C programming

Write a Program to check the given string is palindrome or not

/* WAP to check the given string is palindrome or not*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100],b[100];
printf("Enter any string: \n");
gets(a);
strcpy(b,a);
strrev(a);
if(strcmp(a,b)==0)
{
printf("\nString is Palindrome");
}
else
{
printf("\nString is not Palindrome");
}
getch();
}

Write a Program to counts the number of vowels & consonants in a string

/* WAP to counts the number of vowels & consonants in a string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100];
int v=0,c=0,i;
printf("Enter any string: \n");
gets(a);
strupr(a);
for(i=0;a[i]!='\0';i++)
{
	if(a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')
	{
	v++;
	}
	else if(a[i]>'A'&& a[i]<='Z')
	{
	c++;
	}
}
printf("\n Vowels = %d \n Consonants = %d",v,c);
getch();
}

Write a Program to sort any 10 names in alphabetical order

/* WAP to sort any 10 names in alphabetical order*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10][20],t[20];
int i,j,c;
printf("Enter any 10 names: \n");
for(i=0;i<10;i++)
{
scanf("%s",a[i]);
}
for(i=0;i<10;i++)
{
	for(j=0;j<9-i;j++)
	{
	c=strcmp(a[j],a[j+1]);
	if(c>0)
	{
	strcpy(t,a[j]);
	strcpy(a[j],a[j+1]);
	strcpy(a[j+1],t);
	}
	else
	{
	strcpy(t,a[i]);
	}
	}
printf("Ascending Order are: \n");
for(i=0;i<10;i++)
{
printf("%s\n",a[i]);
}
getch();
}

Print Friendly, PDF & Email

Leave a Comment

Hey, wait!Get Regular Update's

Subscribe Stay Connected, Keep Learning!