Write a program that extract a sub string from a given position.

/* Write a program that extract a sub string from a given position.
eg. “Working with string is fun” , then if from positoin 4, and 4 characters to be extracted,
that is “king”. If the number of charcters  to be extracted is 0 or negative then the program should
return whole string.
*/

#include<stdio.h>
main()
{
int i,n;
char str[100];
puts("Enter the string:: ");
gets(str);
printf("\nEnter the position from you wanna extract string:: ");
scanf("%d",&n);
if(n==0 || n<0) puts(str);
else for(i=n;i<n+n;i++)
printf("%c",str[i]);
}

Program to find substring.

#include<stdio.h>
#include<string.h>
#include<conio.h>

void main()
{
char s[30], t[20];
char *found;
clrscr();

/* Entering the main string */
puts("Enter the first string: ");
gets(s);

/* Entering the string whose position or index to be displayed */
puts("Enter the string to be searched: ");
gets(t);

/*Searching string t in string s */
found=strstr(s,t);
if(found)
printf("Second String is found in the First String at %d position.\n",found-s);
else
printf("-1");
getch();
}

Program to delete a substring from a main string.

#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);
}
}

Program to insert a substring into main string.

A C program that uses functions to perform the following operations:  To insert a sub-string in to given main string from a given position.

#include <stdio.h>
#include <conio.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;
clrscr();

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<s;i++)
{
x = c[i];
if(t<n)
{
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}

printf("%s", a);
getch();
}