Question

In: Computer Science

1.In C++, C#, Java. Discuss string operations functions, with examples. Then make Comparison of programming languages  ...

1.In C++, C#, Java. Discuss string operations functions, with examples. Then make Comparison of programming languages  

2.String and StringBuffer (C#, Java) with examples.

Solutions

Expert Solution

In C++, string is an object of std::string class that represents sequence of characters. We can perform many operations on strings such as concatenation, comparison, conversion etc.

int compare(const string& str) It is used to compare two string objects.
int length() It is used to find the length of the string.
void swap(string& str) It is used to swap the values of two string objects.
string substr(int pos,int n) It creates a new string object of n characters.

In C# programming, string is another kind of data type that represents Unicode Characters. It is the alias of System.String, however, you can also write System.String instead of a string. It is the sequence of character in which each character is a Unicode character.

String Functions Definitions
Clone() Make clone of string.
CompareTo() Compare two strings and returns integer value as output. It returns 0 for true and 1 for false.
Contains() The C# Contains method checks whether specified character or string is exists or not in the string value.
EndsWith() This EndsWith Method checks whether specified character is the last character of string or not.

In Java, a string is a sequence of characters. For example, "hello" is a string containing a sequence of characters 'h', 'e', 'l', 'l', and 'o'.Unlike other programming languages, strings in Java are not primitive types (like int, char, etc). Instead, all strings are objects of a predefined class named String

Methods Description
concat() joins the two strings together
equals() compares the value of two strings
charAt() returns the character present in the specified location
getBytes() converts the string to an array of bytes
indexOf() returns the position of the specified character in the string
C# C++
Comparably slower. Comparably faster.
Mainly built for Windows platform. Compatible with multiple platforms including Windows, Linux and Mac.
Used for Web or Desktop application. Mainly used for Performance-oriented application with hardware interaction.
Has inbuilt garbage collector. No support for garbage collection.
Compiler warnings are displayed at the time of writing the code. Need to write and compile the entire code to check for error.
C# Java
C# runs on CLR. Java runs on JVM.
C# needs .Net framework to run. JDK is required for Java.
C# can be used to develop both Web, and Game development along with Mobile development. Java is mainly used for designing complex web applications.
Not as efficient as Java. Very efficient, flexible and cross platform compatible.
Offers single type of exception. Offers both check and uncheck exceptions.
Libraries development and update depends upon Microsoft. Open source nature allows continuous development and update.
Decent server side performance. Useful for server side interaction.
Parameter of Comparison C++ Java
Based on the concept of Write once compile anywhere Write once run anywhere everywhere
Type of Programming Language Procedural and object-oriented language Only object-oriented language
Type of Language Compiled Compiled + Interpreted
Dependency on platform Platform dependent language Not dependent on platform
Other languages compatibility Yes, with most high-level languages Not compatible, no backward compatibility
Mechanism of Input Input/output statements More complex
Relationship of source code and filename No relationship Yes, relationship exists
Interface with libraries Allows direct calls to native system libraries Only through Java native interface
Portability Not portable Portability is there as it can be executed on any platform
Is operator overloading allowed? Yes No, only method overloading allowed
Type of root hierarchy No root hierarchy Follows single root hierarchy
Access control Flexible Complex
Detection of runtime error responsibility Responsibility of programmer System controlled
Management of Memory Manual System-managed

2)In c#

No String StringBuffer
1 String class is immutable StringBuffer class is mutable (modifiable)
2 Consumes more memory during concatenation Consumes less memory during concatenation
3 Slow performance Fast performance
4 Overrides equals() and hashcode() methods of the object class Doesn't override the equals() and hashcode() methods of the object class

In java

No. String StringBuffer
1) String class is immutable. StringBuffer class is mutable.
2) String is slow and consumes more memory when you concat too many strings because every time it creates new instance. StringBuffer is fast and consumes less memory when you cancat strings.
3) String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method. StringBuffer class doesn't override the equals() method of Object class.

***********************************************************************************************************************************

In case of any doubt do ask in the comment section


Related Solutions

Java, Python, and C++ are three of the most useful programming languages to learn. Compare the...
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the functionalities of all three programming languages. Why would you choose one language over another? Provide code examples demonstrating their usefulness in a real-world scenario.
Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages...
Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages which support the user of the programming language deciding which to use when creating their method. (Programming Languages)
Design and construct a computer program in one of the approved languages (C, C++, C#, Java,...
Design and construct a computer program in one of the approved languages (C, C++, C#, Java, Pascal, Python, etc.) that will illustrate the use of a fourth-order explicit Runge-Kutta method of your own design. In other words, you will first have to solve the Runge-Kutta equations of condition for the coefficients of a fourth-order Runge-Kutta method. Then, you will use these coefficients in a computer program to solve the ordinary differential equation below. Be sure to follow the documentation and...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
2. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string....
2. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string. Ex, "$6.25", "$0.21", "$4.01", "$2.00". MAKE SURE TO CONSIDER ALL EXAMPLES! b) *String moneyToText(int[] money); // Returns the Money as words. Ex,{123,51} => "one hundred and twenty three dollars and fifty one cents." YOU MAY ASSUME money <$1000. context: this is what I have so far package com.company; import java.util.*; public class Main { public static void main(String[]args) { int money[] = createMoney(12, 34);...
Discuss any API / libraries of your favorite programming language (e.g. python, c#, java etc.) that...
Discuss any API / libraries of your favorite programming language (e.g. python, c#, java etc.) that you have used/reused in any of your previous projects and how it contributes to the overall project.
This is for C programming. Suppose one string is defined for a sentence with maximum 100...
This is for C programming. Suppose one string is defined for a sentence with maximum 100 characters. Write a method that prints maximum occurring characters in the input string and its frequency. For example, if input string is "this is testt", your code should print 't' : 4. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT