Powered By Blogger

Friday, April 27, 2012

Program : 11


PROGRAM - 11

a)  Write a program that uses function to perform the following operations:
ü  To insert a sub-string in a main string at a specified position.
ü  To delete N characters from a given string from a specified position.

Program for insert a sub string in main string at a specified position:

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
     char mstr[20],sstr[20],rstr[20];
     int i,j,p,c=0;
     clrscr();
     printf("enter the main string:");
     gets(mstr);
     printf("enter the sub string:");
     gets(sstr);
     printf("enter the position to insert the 
                                    sub string");
     scanf("%d",&p);
     for(i=0,c=0;mstr[i]!='\0';i++,c++)
     {
        if(i==p)
        {
          for(j=0;sstr[j]!='\0';j++,c++)
          {
            rstr[c]=sstr[j];
          }
        }
        rstr[c]=mstr[i];
     }
     rstr[c]='\0';
     printf("the resultant string is %s",rstr);
     getch();
}
/*
INPUT:
enter the main string:WELCOMELAB
enter the sub string: TO CP
enter the position to insert the sub string7
OUTPUT:
the resultant string is WELCOME TO CPLAB
*/

Program for to delete n characters from a given string:

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
     char str[50],str2[20];
     int n,p,k=0,i;
     clrscr();
     printf("enter the string:");
     gets(str);
     printf("\nenter the position from 
                    where to delete:");
     scanf("%d",&p);
     printf("\nenter the number of characters 
                              to delete:");
     scanf("%d",&n);
     for(i=0;str[i]!='\0';i++)
     {
         if(i==p)
         {
           i=i+n;
         }
         str2[k]=str[i];
         k++;
     }
     str2[k]='\0';
     printf("resultant string is %s",str2);
     getch();
}
/*
INPUT:
enter the string:welcome to cplab
enter the position from where to delete:7
enter the number of characters to delete:4
OUTPUT:
resultant string is welcomecplab
*/





b)  Write a program to determine whether the given string is palindrome or not.

Program to find the given string is palindrome or not:

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
     char str[20],s[20];
     clrscr();
     printf("enter the string");
     gets(str);
     strcpy(s,str);
     strrev(s);
     if(strcmp(str,s)==0)
     printf("the entered string is palindrome");
     else
     printf("the entered string is not palindrome");
     getch();
}
/*
INPUT:
enter the string MADAM
OUTPUT:
the entered string is palindrome
INPUT:
enter the string WELCOME
OUTPUT:
the entered string is not palindrome
*/

No comments:

Post a Comment