Tuesday, October 4, 2011

Data Structure Lab Programs - Write a Program to implement the various operation on string such as a)String Length b)String Concatenate c)String Reverse d)String Copy

Practical # 6

 

/* Write a Program to implement the various operation on string such as   a)String Length   b)String Concatenate  c)String Reverse  d)String Copy */

 

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<ctype.h>

int i=0,j=0;

void strleng(char str[10],char str1[10])

{

int ct;

for(;str[i]!='\0',str1[j]!='\0';i++,j++);

ct=i+j;

printf("\nIt's length is %d",ct);

}

void strconc(char str[15],char str1[15])

{

char con_str[30];

int j;

for(i=0;str[i]!='\0';i++)

con_str[i]=str[i];

j=i;

for(i=0;str1[i]!='\0';i++)

{

con_str[j]=str1[i];

j++;

}

puts(con_str);

}

void strreve(char str[15],char str1[15])

{

int len1,len2;

printf("\nThe reverse string1 is ");

len1=strlen(str);

for(i=len1;i>=0;i--)

printf("%c",str[i]);

printf("\nThe reverse string2 is ");

len2=strlen(str1);

for(i=len2;i>=0;i--)

printf("%c",str1[i]);

}

void strcop(char str[15],char str1[15])

{

char copy_str1[15],copy_str2[15];

int j;

for(i=0;str[i]!='\0';i++)

copy_str1[i]=str[i];

for(i=0;str1[i]!='\0';i++)

copy_str2[i]=str1[i];

puts(copy_str2);

}

void main()

{

int ch;

char str[15],str1[15];

char choice;

clrscr();

printf("Enter the string1=");

gets(str);

printf("Enter the string2=");

gets(str1);

do

{

clrscr();

printf("\n Menu");

printf("\n\n\n1. String Length=");

printf("\n\n\n 2. String Concat=");

printf("\n\n\n 3. String Reverse=");

printf("\n\n\n4. String copy=");

printf("\nEnter the choice(1-4)=");

scanf("%d",&ch);

switch(ch)

{

case 1:

strleng(str,str1);

getch();

break;

case 2:

strconc(str,str1);

getch();

break;

case 3:

strreve(str,str1);

getch();

break;

case 4:

break;

 

default:

break;

}

printf("\nDo you want to continue(y/n)?");

fflush(stdin);

scanf("%c",&choice);

}while(toupper(choice)=='Y');

}

 

 

 

 

Output:-

 

Enter the string1=Lalit

Enter the string2=Rohilla

 

 

 Menu

 

1. String Length=

 

2. String Concat=

 

3. String Reverse=

 

4. String copy=

 

Enter the choice(1-4)=1

It’s length is 12

 

Do you want to continue(y/n)y

 

Enter the choice(1-4)=2

LalitRohilla

 

Do you want to continue(y/n)y

 

Enter the choice(1-4)=3

The reverse string1 is=tilaL

The reverse string2 in=allihoR

 

No comments :

Post a Comment