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

1.) Many languages (e.g., C and Java) distinguish the character ’c’ from the string “c” with...
1.) Many languages (e.g., C and Java) distinguish the character ’c’ from the string “c” with separate sets of quotation marks. Others (e.g., Python) use “c” for both single characters and strings of length one. Provide (and justify) one advantage and one disadvantage of Python’s approach. 2.The designers of Java distinguish the primitive types (scalars) from the reference types (all other types of values) in the language. Discuss the costs and benefits to the programmer of this decision 3.In Ruby,...
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.
QUESTION 1 Which of the following programming languages does NOT support parametric polymorphism? Java C++ Python...
QUESTION 1 Which of the following programming languages does NOT support parametric polymorphism? Java C++ Python C# 10 points    QUESTION 2 Which of the following programming languages does NOT support operator overloading? C# Python C C++ 10 points    QUESTION 3 In a language that allows nested subprograms, the programming language has to choose a referencing environment for the executing the passed subprogram when a subprogram is passed as a parameter. Which of the following is NOT a choice?...
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)
1.Discuss the Associative Arrays in C# and Java, with example, mention the different operations. 2.Is the...
1.Discuss the Associative Arrays in C# and Java, with example, mention the different operations. 2.Is the size static or dynamic?
Do in c++ programming languages Question 1: Solve the complete Question with all parts and subparts....
Do in c++ programming languages Question 1: Solve the complete Question with all parts and subparts. a) What do you understand by the term DATA TYPE? b) Is it possible to replace for loop with a while loop. Justify your answer with reasoning and example. c) Draw a flow chart for Do-While loop for controlled/uncontrolled infinite loop iterations. d) Write down a function prototype that takes an array and its length as an argument and also return the same array...
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...
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. See the Mathematica notebook on solving the equations for 4th order RK method. .DO NOT USE a[1] or a[2] = 1/2. Then, you will...
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"}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT