Skip to main content

C-Programming Program 6(a)

6.    a) Write a C program that uses functions to perform the following operations:
                        i) To insert a sub-string in to given main string from a given position.
                        ii) To delete n Characters from a given position in a given string.

Algorithm


i) To insert a sub-string in to given main string from a given position.


Step 1 : Start

Step 2 : Declare s, s1, s2, i, pos
Step 3 : Read String s
Step 4 : Read String s1
Step 5 : Read pos
Step 6 : initialize i=pos, j=0
Step 7 : if i<length of string s 
 7.1 : s2[j] = s[i]
 7.2 : s[i]=s1[j]
 7.3 : j=j+1, i=i+1
 7.4 : goto step 7
Step 8 : concat s, s2
Step 9 : Print s
Step 10 : Stop

Program to delete ‘n’ characters from the given position of string


Algorithm:

Step 1 : Start
Step 2 : Declare s, s1, i, j, pos, n
Step 3 : Read String s
Step 4 : Read pos, n
Step 5 : initialize i=pos, j= pos + n
Step 6 : if j< length of string s 
 6.1 : s1[i] = s[j], s[i]=s1[i]
 6.2 : j=j+1, i=i+1
 6.3 : goto step 6
Step 7 : s[length-n]=end of string
Step 8 : Print s
Step 9 : Stop

Flowcharts 

6 i
6 ii

Program 



i) To insert a sub-string in to given main string from a given position.

#include<stdio.h> 
#include<string.h> 
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;

// Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;

// Adding the sub-string
for(i=p;i{
x = c[i];
if(t {
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}

printf("%s", a);
}

  ii) To delete n Characters from a given position in a given string.

#include<stdio.h>
#include<string.h>
void main()
{
char st[20],temp[20];
int pos,i,j,ct=0,n;
printf("Enter the string:");
gets(st);
printf("\nEntre the index position:");
scanf("%d",&pos);
printf("\nEnter the no.of characters:");
scanf("%d",&n);
if(pos<=strlen(st)) {
for(i=0;i<=strlen(st);i++) {
if(i==pos)
{
for(j=0;st[i]!='\0';j++) // to store the 'st' in 'temp' from a giv pos
{
temp[j]=st[i];
i++;
}
temp[j]='\0';
i=pos;
//to delete the 'n' char and after restore the temp in st.
for(j=0;temp[j]!='\0';j++)
{
ct++;
if(ct>n)
{
st[i]=temp[j];
i++;
}
}
st[i]='\0';
}
}
printf("\n\nAfter deletion the string: %s",st);
}
else
printf("\nSorry, we cannot delete from that position.");
}

Comments

Popular posts from this blog

C-Programming with Record

C-Programming Programs Syllabus Click for full record  1.a) Write a C program to ask the user to enter one char ( Upper-Case letter) check  whether user  entered a Upper-case letter or not(by using relational and logical operators) and then if user has  entered a Upper-case letter  convert into a Lower-case letter? ( hint: Upper-case means capital letters,  use ASCII information to check for Upper-case and convert) b) Write a  C program to ask the user to enter two integers and apply all arithmetic operations on those  print the corresponding values?(hint : +,-,*,/,%)     c) Write a C program to Determine the ranges of char, short, int and long int variables both signed and  Unsigned   By using sizeof operator(ii) By printing appropriate values from standard header (limits.h )   2.a) Write a Program to Find the Roots of a Quadratic Equation using if else and Switch statements. b) Write a Program which Gener...

OOPS CPP Program-14

14. Write a program to find transpose of a given matrix of mxn size using unary operator overloading #include<iostream> using namespace std; class matrix {         int x[5][5],m,n; public:         matrix(){ }         matrix(int a,int b)         {                m=a;n=b;         }         void get();         void put();         matrix operator !(); }; void matrix::get() {         int i,j;         cout<<"\n Enter a matrix of order"<<m<<"x"<<n<<"\n";                for(i=0;i<m;i++)                    for(j=0;j<n;j++)                        ...

OOPS CPP Program-15

15. Write a program to concatenate one string to another using binary operator overloading. #include<iostream> #include<string.h> using namespace std; class str  {         char *st; public:         str(int len, char *s)         {                  int l=strlen(s);                st= new char[l];                st=s;          }         str()         {         }         void put();         str operator +(str); }; str str :: operator +(str s)  {  /*str abc; abc.st=strcat(st, s.st); return abc;*/         st=strcat(st, s.st);         return *this; } void str :: put() {   cout<<...