/*-------------------------------------------------------------------------------------------------------------------------------
Author: Sujith
Program: Write C++ programs to extract a string from given string without using inbuilt function.
Prog Name: 1substr.cpp
Date: 21-04-2015
---------------------------------------------------------------------------------------------------------------------------------*/
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
void stringsub(char s1[],int i,int j,char s2[]);
int main()
{
char str1[20],str2[20];
int i,j,len;
cout<<"Enter the string";
cin>>str1;
len=strlen(str1);
cout<<"Enter the position of the string";
cin>>i;
if(i>len)
cout<<"Invalid poistion\n";
else
{
cout<<"Enter the number of character";
cin>>j;
stringsub(str1,i,j,str2);
cout<<str2;
}
}
void stringsub(char s1[],int i,int j,char s2[])
{
int k,l;
l=i;
for(k=0;k<j;k++)
{
s2[k]=s1[l];
l++;
}
s2[k]='\0';
}
Output:
"1substr.cpp" [New] 37L, 740C written
[141740@localhost C++]$ g++ substr.cpp
[141740@localhost C++]$ ./a.out
Enter the string
commonsense
Enter the position of the string
6
Enter the number of character
5
sense
[141740@localhost C++]$
Author: Sujith
Program: Write C++ programs to extract a string from given string without using inbuilt function.
Prog Name: 1substr.cpp
Date: 21-04-2015
---------------------------------------------------------------------------------------------------------------------------------*/
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
void stringsub(char s1[],int i,int j,char s2[]);
int main()
{
char str1[20],str2[20];
int i,j,len;
cout<<"Enter the string";
cin>>str1;
len=strlen(str1);
cout<<"Enter the position of the string";
cin>>i;
if(i>len)
cout<<"Invalid poistion\n";
else
{
cout<<"Enter the number of character";
cin>>j;
stringsub(str1,i,j,str2);
cout<<str2;
}
}
void stringsub(char s1[],int i,int j,char s2[])
{
int k,l;
l=i;
for(k=0;k<j;k++)
{
s2[k]=s1[l];
l++;
}
s2[k]='\0';
}
Output:
"1substr.cpp" [New] 37L, 740C written
[141740@localhost C++]$ g++ substr.cpp
[141740@localhost C++]$ ./a.out
Enter the string
commonsense
Enter the position of the string
6
Enter the number of character
5
sense
[141740@localhost C++]$
No comments:
Post a Comment