Question

A string is a sequence of characters. Write a C++ program by creating a user-defined string...

A string is a sequence of characters. Write a C++ program by creating a user-defined string class that performs the following functions:

  • String comparison
  • String concatenation
  • Returning the length of the string
  • Reversing a string backward

Reversing a string backward

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
class StringOperations
{
   public:
   char *s;
   //counstructor
   StringOperations(char *l)
   {
       s=l;
   }
   //method to find length
   int length(char *l)
   {
       int i;
       for(i=0;l[i]!='\0';i++);
       return i;
   }
   //method to compare
   //return true if both are equal
   //other wise returns false
   bool compare(char *l)
   {
           int l1 = length(s);
           int l2 = length(l);
           if(l1==l2)
           {
               for(int i=0;i<l1;i++)
               if(s[i]!=l[i])return false;
               return true;
           }
           return false;
   }
   //method to concatenated both strings
   void concatenate(char *l)
   {
       int l1 = length(s);
           int l2 = length(l);
       char *a = new char[l1+l2];
           for(int i=0;i<l1;i++)
           a[i]=s[i];
           int i=l1,j=0;
       for(;j<l2;j++)
       a[i++]=l[j];
       a[i]='\0';
       s=a;  
   }
   //method to reaverse a string
   void Reverse()
   {
       int i=0;
       int j= length(s)-1;
       while(i<j)
       {
           char c = s[i];
           s[i]=s[j];
           s[j]=c;
           i++;j--;  
       }
   }
};
//tester method
int main()
{
   char *a = new char[20];
   char *b = new char[20];
   char *c = new char[20];
   cout<<"Enter string1:";
   cin>>a;
   cout<<"Enter string2:";
   cin>>b;
   cout<<"Enter string3:";
   cin>>c;
  

   StringOperations s(a);
  
   cout<<"Comparing "<<a<<" with "<<b<<" result :"<<boolalpha<<s.compare(b)<<endl;

   cout<<"Comparing "<<a<<" with "<<c<<" result :"<<boolalpha<<s.compare(c)<<endl;

  
   cout<<"Concatenating "<<a<<" with "<<b<<" result :";
   s.concatenate(b);
   cout<<s.s<<endl;
  
   cout<<"Length of "<<s.s<<" :"<<s.length(s.s)<<endl;
   cout<<"Reversing "<<s.s<<" :";
   s.Reverse();
   cout<<s.s<<endl;

}

output:

Enter string1:hello
Enter string2:how
Enter string3:hello
Comparing hello with how result :false
Comparing hello with hello result :true
Concatenating hello with how result :hellohow
Length of hellohow :8
Reversing hellohow :woholleh


Process exited normally.
Press any key to continue . . .


//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.


//if you have any doubts, ask me in the comments


Add a comment
Know the answer?
Add Answer to:
A string is a sequence of characters. Write a C++ program by creating a user-defined string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Programming in Amazon WorkSpace if possible. A string is a sequence of characters. Write a...

    C++ Programming in Amazon WorkSpace if possible. A string is a sequence of characters. Write a C++ program by creating a user-defined string class that performs the following functions: String comparison String concatenation Returning the length of the string Reversing a string backward

  • C Program: Write the following function that takes a user input string, computes, and determines whether the input is palindromic or not by returning either true or false:

    A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or reversed. Examples: “Dad”, “Anna”, “Never odd or even”, etc. For this problem, we will only consider a string to be palindromic if its combined alpha-numeric characters (‘A’-’Z’, ‘a’-’z’, and ‘0’-’9’) can be read the same both forward and backward, by skipping all other non-alphanumeric characters.Write the following function that takes a user input string, computes, and determines whether the input is palindromic or not...

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • MIPS programming question Problem 1: Write a program that asks the user to input a string...

    MIPS programming question Problem 1: Write a program that asks the user to input a string (or no more than 50 characters). Your program should then output the length of the string. The string length should be determined using a separate function strlen that will accept the address of the string and return its length. For the purposes of this exercise, the length of a string will be defined as the number of non-null and non-newline characters until either the...

  • Write a C Program that asks the user to input a string and then the user...

    Write a C Program that asks the user to input a string and then the user is prompted back whether the input string is a “Palindrome Word” or not. (A Palindrome Word reads the same backward or forward, eg. madam, racecar, etc.) Your program should contain a function that accepts the input string from the main function and returns a value indicating whether the input string is a Palindrome or not. Use Pointers to traverse the string.

  • Write a java program that accepts a string from the user. List all characters in the...

    Write a java program that accepts a string from the user. List all characters in the string from lowest ASCII value to highest and output the number of occurrences for each of those characters.

  • JAVA Programming scenario: write a program that will prompt a user for 10 legendary people/characters. After...

    JAVA Programming scenario: write a program that will prompt a user for 10 legendary people/characters. After the entries are complete the program will display all 10 characters information. requirements: Create a user-defined class with the fields below: - First - Last - Nickname - Role - Origin Create a main-source that will use the user-defined class and do the prompting. Create get/set methods in your user-defined class and methods in the main-source.

  • JAVA PROGRAMMING QUESTION scenario: write a program that will prompt a user for 10 legendary people/characters....

    JAVA PROGRAMMING QUESTION scenario: write a program that will prompt a user for 10 legendary people/characters. After the entries are complete the program will display all 10 characters information. Requirements: Create a user-defined class with the fields below: First Last Nickname Role Origin Create a main-source that will use the user-defined class and do the prompting. Create get/set methods in your user-defined class and methods in the main-source.

  • Write a C program that acquires a sequence of characters from the keyboard. The sequence ends...

    Write a C program that acquires a sequence of characters from the keyboard. The sequence ends once the user enters the newline character, i.e., hits the ENTER key. The program modifies the entered sequence by: • First replacing every occurrence of the sequence "ch” with the letter "K" • Then replacing all occurrences of the sequence "oo" with the letter "u" . And then replacing all occurrences of the sequence "ee" with the letter "j" and prints the modified version...

  • Write a C program that prompts the user for an input string with all lowercase letters....

    Write a C program that prompts the user for an input string with all lowercase letters. Your program should then sort the characters in the string and print the sorted string. Assume that the string contains no spaces and has at most 30 lowercase characters. Your program should loop continually until the user enters “q” to quit.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT