Implement the following function:
char* MoveHyphen(char str[], int n)
The function accepts a string str of length n , that contains alphabets and hypens - .implement the fuction to move all the hyphens - in the string to the front of the given string .
string to the front of the
Note: Return null (None in case of python) if stt iss empty.
Example:
Input:
str: Move-Hyphens-to-Front
Output:
---MoveHyphanstofront
sample input
str: string-compare
sample output
-stringcompare
Answers
Answered by
0
Java Code
class MoveHyphenToFront
{
static String MoveHyphen(String str,int n)
{
if(str==null)
return null;
char[] ch=str.toCharArray();
String hyphen="";
for (int i = 0; i < ch.length; i++) {
if(ch[i]=='-')
hyphen+="-";
}
str=str.replace("-","");
return hyphen+str;
}
public static void main(String[] args) {
System.out.println(MoveHyphen("Move-Hyphens-to-Front", 18));
}
}
- The series of characters is represented using the CharSequence interface. It is implemented by the String, StringBuffer, and StringBuilder classes. This implies that we can use these three classes to construct strings in Java.
- Because the Java String is immutable, changes cannot be made to it. Every time we alter a string, a new instance is produced. Use the StringBuffer and StringBuilder classes for mutable strings.
- Immutable strings will be covered later. Let's first examine the definition of a string in Java and the steps for creating a string object.
#SPJ1
Similar questions