Wednesday 12 September 2012

To delete n characters from a given position in a given string


Description:
in this program we need to delete a string from the given string at a specified position.
Algorithm:
Step 1: start
Step 2: read string
Step 3: find the length of the string
Step 4: read the value of number of characters to be deleted and positioned
Step 5: string copy part of string from position to end, and (position+number of      
             characters to end)
Step 6: stop
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>

void delchar(char *x,int a, int b);

void main()
{
     char string[10];
     int n,pos,p;
     clrscr();

     puts("Enter the string");
     gets(string);
     printf("Enter the position from where to delete");
     scanf("%d",&pos);
     printf("Enter the number of characters to be deleted");
     scanf("%d",&n);
     delchar(string, n,pos);
     getch();
}

// Function to delete n characters
void delchar(char *x,int a, int b)
{
             if ((a+b-1) <= strlen(x))
  {
                         strcpy(&x[b-1],&x[a+b-1]);
                          puts(x);
              }
}
Output:
1.enter the string
   nagaraju
   Enter the position from where to delete:4
    Enter the number of charcters to be deleted3
    nagju
 2. enter the string
      kaliraju
      Enter the position from where to delete:0
       Enter the number of charcters to be deleted4
       Raju
Conclusion: the program is error free


VIVA QUESATIONS:
1)      Which command is used to delete the strings ?
Ans: delstr();
2)      What are the various types of string functions ?
Ans: Strcat(), strcpy(), delstr(), substr() ,strlen()etc..,







No comments:

Post a Comment