In this article string programs in c we give the details string handling programs with their input and output. also reverse string and palindrome string program with output.

String Programs in C:

// 1. Write a C Program to show your Name

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char name[15]={‘R’,’a’,’j’,’v’,’e’,’e’,’r’,’\0′};  // Compile time initialization

clrscr();

printf(“\n My Name is %s.”,name);

getch();

}

Output:

My Name is Rajveer.

// 2. Write a C Program to show Your Name

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

// run time initialization

char name[15];

clrscr();

printf(“\n\n Enter the Name: “);

scanf(“%s”,&name);                                                // run time initialization

printf(“\n My Name is %s.”,name);

getch();

}

Output:

Enter the Name: Yashraj

My Name is Yashraj

// 3. Write a C program to show String Functions

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char str1[15], str2[15], str3[20];

clrscr();

printf(“\n Enter the first string: “);

scanf(“%s”,str1);

printf(“\n Enter the second string: “);

scanf(“%s”,str2);

printf(“\n Show First string length: %d”,strlen(str1));

printf(“\n Show First string in Lower case: %s”,strlwr(str1));

printf(“\n Show First string Upper case: %s”,strupr(str1));

printf(“\n Concatenate two Strings: %s”,strcat(str1,str2));

printf(“\n Show string Reverse order: %s”,strrev(str1));

printf(“\n Copy string str1 to str3: %s”,strcpy(str3,str1));

getch();

}

Output:

Enter the first string: Yashraj

Enter the second string: Rajveer

Show First string length: 7

Show First string in Lower case: Yashraj

Show First string Upper case: YASHRAJ

Concatenate two Strings: YASHRAJRajveer

Show string Reverse order: reevjaRJARHSAY

Copy string str1 to str3: YASHRAJRajveer

// 4. Write a C program to count word for the given sentence.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char sen[30];

int i,len,cnt=1;

clrscr();

printf(“\n Enter the sentence: “);

gets(sen);

printf(“\n %s”,sen);

len=strlen(sen);

for(i=0;i<len;i++)

{

if(sen[i]==32)

{

cnt=cnt+1;

}

}

printf(“\n\n %d words in the sentence.”, cnt);

getch();

}

Output:

Enter the sentence: I Love India

3 words in the sentence.

// 5. Write a C program to count alphabet for the given sentence.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char a,sen[30];

int i,len,cnt=0;

clrscr();

printf(“\n Enter the sentence: “);

gets(sen);

printf(“\n %s”,sen);

len=strlen(sen);

printf(“\n Enter alphabet which you find in given sentence: “);

scanf(“%c”,&a);

for(i=0;i<len;i++)

{

sen[i]=tolower(sen[i]);

if(sen[i]==a)

{

cnt=cnt+1;

}

}

printf(“\n\n %c alphabet is found %d times in the sentence.”, a, cnt);

getch();

}

Output:

Enter the sentence: I Love India

Enter alphabet which you find in given sentence: i

i alphabet is found 3 times in the sentence.

// 6. Write a C Program to show reverse string using string function.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char str[20],str1[20],str2[20];

int i,len;

clrscr();

printf(“\n Enter the word: “);

gets(str);

printf(“\n Given string: %s\n”,str);

printf(“\n Reverse String: %s”,strrev(str));        // use srtrev function

getch();

}

Output:

Enter the word: Ram

Given string: Ram

Reverse String: maR

// 7. Write a C Program to show reverse string without using strrev function.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char str[20];

int i,len;

clrscr();

printf(“\n Enter the word: “);

gets(str);

printf(“\n Given string: %s\n”,str);

len=strlen(str);

printf(“\n Reverse String: “);

for(i=len-1;i>=0;i–)

{

printf(“%c”,str[i]);

}

getch();

}

Output:

Enter the word: Sham

Given string: Sham

Reverse String: mahS

// 8. Write a C program to check if a given string is palindrome or not

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char str[20];

int l = 0,h;

clrscr();

printf(“\n Enter the string: “);

gets(str);

h=strlen(str)-1;

while (h > l)

{

if (str[l++] != str[h–])

{

printf(“%s is not a palindrome string.”, str);

}

else

{

printf(“%s is a palindrome string.”, str);

break;

}

}

getch();

}

Output:

Enter the string: madam

Madam is palindrome string.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *