Already have an account? Get multiple benefits of using own account!
Login in your account..!
Remember me
Don't have an account? Create your account in less than a minutes,
Forgot password? how can I recover my password now!
Enter right registered email to receive password!
Aim: To implement a program for following string operations:
Code:
class String
{
char str[50];
public:
String();
void getdata();
int length1();
void concat(String s);
String substring(int ind, int len);
char charAt(int ind);
int indexOf(char ch);
String reverse();
void display();
};
void String::display()
cout< } void String::getdata() { gets(str); } String::String() { strcpy(str,""); } int String::length1() { int l; l=strlen(str); return (l); } void String::concat(String s) { strcat(str,s.str); } String String::substring(int ind, int len) { int l,x,m; char su[50]; String sub; strcpy(sub.str,""); for(x=ind,m=0;x<=len+1;x++,m++) { su[m]=str[x]; } su[m]='\0'; strcpy(sub.str,su); return (sub); } char String::charAt(int ind) { char c=' '; int l=strlen(str); if(ind>l) cout<<"Position out of Bounds"; else c=str[ind]; return (c); } int String::indexOf(char ch) { int x=1; while(str[x]!=ch && str[x]!='\0') x++; if(str[x]!=ch) return (-1); else return (x); } String String::reverse() { String s; strcpy(s.str,str); strrev(s.str); return (s); } void main() { clrscr(); String s1,s2,s3,s4; cout<<"Enter string 1:"; s1.getdata(); cout<<"\n\nEnter string 2:"; s2.getdata(); char ch='o'; int l=s1.length1(); cout<<"\n\nLength of string: '"; s1.display(); cout<<" 'is "< s1.concat(s2); cout<<"\n\nAfter concatenation:"; s1.display(); int ind=2, len=4; cout<<"\n\nStart Index="< s3.display(); char c=s1.charAt(ind+4); cout<<"\n\nCharacter at Index: "< int i=s1.indexOf(ch); cout<<"\n\nIndex of '"< s4=s1.reverse(); cout<<"\n\nReverse of \n\t'"; s1.display(); cout<<"'\n\t\tis \n\t"; s4.display(); getch(); } Output: Enter string 1: Ankit Goyal is Enter string 2: a student of VESIT. Length of string: 'Ankit Goyal is ' is 14 After concatenation: Ankit Goyal is a student of VESIT. Start Index=2 Length of Substring=4 Substring: kit Character at Index: 6 is 'G' Index of 'o' is: 7 Reverse of 'Ankit Goyal is a student of VESIT.' is .TISEV fo tneduts a si layoG tiknA
}
void String::getdata()
gets(str);
String::String()
strcpy(str,"");
int String::length1()
int l;
l=strlen(str);
return (l);
void String::concat(String s)
strcat(str,s.str);
String String::substring(int ind, int len)
int l,x,m;
char su[50];
String sub;
strcpy(sub.str,"");
for(x=ind,m=0;x<=len+1;x++,m++)
su[m]=str[x];
su[m]='\0';
strcpy(sub.str,su);
return (sub);
char String::charAt(int ind)
char c=' ';
int l=strlen(str);
if(ind>l)
cout<<"Position out of Bounds";
else
c=str[ind];
return (c);
int String::indexOf(char ch)
int x=1;
while(str[x]!=ch && str[x]!='\0')
x++;
if(str[x]!=ch)
return (-1);
return (x);
String String::reverse()
String s;
strcpy(s.str,str);
strrev(s.str);
return (s);
void main()
clrscr();
String s1,s2,s3,s4;
cout<<"Enter string 1:";
s1.getdata();
cout<<"\n\nEnter string 2:";
s2.getdata();
char ch='o';
int l=s1.length1();
cout<<"\n\nLength of string: '";
s1.display();
cout<<" 'is "< s1.concat(s2); cout<<"\n\nAfter concatenation:"; s1.display(); int ind=2, len=4; cout<<"\n\nStart Index="< s3.display(); char c=s1.charAt(ind+4); cout<<"\n\nCharacter at Index: "< int i=s1.indexOf(ch); cout<<"\n\nIndex of '"< s4=s1.reverse(); cout<<"\n\nReverse of \n\t'"; s1.display(); cout<<"'\n\t\tis \n\t"; s4.display(); getch(); } Output: Enter string 1: Ankit Goyal is Enter string 2: a student of VESIT. Length of string: 'Ankit Goyal is ' is 14 After concatenation: Ankit Goyal is a student of VESIT. Start Index=2 Length of Substring=4 Substring: kit Character at Index: 6 is 'G' Index of 'o' is: 7 Reverse of 'Ankit Goyal is a student of VESIT.' is .TISEV fo tneduts a si layoG tiknA
s1.concat(s2);
cout<<"\n\nAfter concatenation:";
int ind=2, len=4;
cout<<"\n\nStart Index="< s3.display(); char c=s1.charAt(ind+4); cout<<"\n\nCharacter at Index: "< int i=s1.indexOf(ch); cout<<"\n\nIndex of '"< s4=s1.reverse(); cout<<"\n\nReverse of \n\t'"; s1.display(); cout<<"'\n\t\tis \n\t"; s4.display(); getch(); } Output: Enter string 1: Ankit Goyal is Enter string 2: a student of VESIT. Length of string: 'Ankit Goyal is ' is 14 After concatenation: Ankit Goyal is a student of VESIT. Start Index=2 Length of Substring=4 Substring: kit Character at Index: 6 is 'G' Index of 'o' is: 7 Reverse of 'Ankit Goyal is a student of VESIT.' is .TISEV fo tneduts a si layoG tiknA
s3.display();
char c=s1.charAt(ind+4);
cout<<"\n\nCharacter at Index: "< int i=s1.indexOf(ch); cout<<"\n\nIndex of '"< s4=s1.reverse(); cout<<"\n\nReverse of \n\t'"; s1.display(); cout<<"'\n\t\tis \n\t"; s4.display(); getch(); } Output: Enter string 1: Ankit Goyal is Enter string 2: a student of VESIT. Length of string: 'Ankit Goyal is ' is 14 After concatenation: Ankit Goyal is a student of VESIT. Start Index=2 Length of Substring=4 Substring: kit Character at Index: 6 is 'G' Index of 'o' is: 7 Reverse of 'Ankit Goyal is a student of VESIT.' is .TISEV fo tneduts a si layoG tiknA
int i=s1.indexOf(ch);
cout<<"\n\nIndex of '"< s4=s1.reverse(); cout<<"\n\nReverse of \n\t'"; s1.display(); cout<<"'\n\t\tis \n\t"; s4.display(); getch(); } Output: Enter string 1: Ankit Goyal is Enter string 2: a student of VESIT. Length of string: 'Ankit Goyal is ' is 14 After concatenation: Ankit Goyal is a student of VESIT. Start Index=2 Length of Substring=4 Substring: kit Character at Index: 6 is 'G' Index of 'o' is: 7 Reverse of 'Ankit Goyal is a student of VESIT.' is .TISEV fo tneduts a si layoG tiknA
s4=s1.reverse();
cout<<"\n\nReverse of \n\t'";
cout<<"'\n\t\tis \n\t";
s4.display();
getch();
Output:
Enter string 1: Ankit Goyal is
Enter string 2: a student of VESIT.
Length of string: 'Ankit Goyal is ' is 14
After concatenation: Ankit Goyal is a student of VESIT.
Start Index=2
Length of Substring=4
Substring: kit
Character at Index: 6 is 'G'
Index of 'o' is: 7
Reverse of
'Ankit Goyal is a student of VESIT.'
is
.TISEV fo tneduts a si layoG tiknA
You're going to simulate a metropolitan subway train. The train system will start out at Station #1, or Home Station, and will service multiple stations along its route.
(Random Numbers) Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤2 b) 1 ≤ n ≤100 c) 0 ≤ n ≤9 d) 1000 ≤ n ≤1112 e)
Rules of Inheritance: Private members are not inherited; the members can be accessed only within its class. It cannot be used through the object. Protected members are
how to declare multi dimensional array
Create an applet that bounces a blue ball inside an applet using Thread. The ball (diameter is 10) will start at position (0,0). When the ball hits the edge of the applet, the ba
First line starts with T, number of test cases. Each test case T contains a necklace (N).
dynamic initialization of object using constructor in C++
use of header files
Use the program called harris-shell.c and add some code to find the corners in the image checkers.jpg. You should say that a pixel in the image is a corner if it passes the given t
Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b
Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!
whatsapp: +1-415-670-9521
Phone: +1-415-670-9521
Email: [email protected]
All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd