C Program to extract substring from string

Program : c program to extract substring from string

Source Code :

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

#define STR_SIZE 80

void SubStr(char* pStr, char *pSubStr, int nOffset, int nSize)
{
int i, j;

for(i = 0, j = nOffset; i < nSize; i++, j++)
{
pSubStr[i] = pStr[j];
}
}

void main()
{
char sStr[STR_SIZE], sSubStr[STR_SIZE] = “”;
int  nOffset, nSize;

printf(“\nEnter any character string: “);
gets(sStr);

printf(“\nEnter offset to extract sub-string: “);
scanf(“%d”, &nOffset);

printf(“\nEnter number of character to be extracted: “);
scanf(“%d”, &nSize);

if(nOffset >= 0 || (nOffset+nSize) <= (STR_SIZE-1))
{
SubStr(sStr, sSubStr, nOffset, nSize);
printf(“%s”, sSubStr);
}
else
{
printf(“\nOffset is not valid\n”);
}
}

 

This entry was posted in Technology and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>