Computer Science, asked by anshikajassal066, 5 months ago

Question 6 Write a program in Java to input a string and find the given character input by the user
present in the string. If present replace that character with @' (without using replace(function) otherwise
display not present
Example: Input: Enter string: JAVA
character given by the user A
Output: New string: J@v@​

Answers

Answered by puneetdevman
0

Answer:

Output is

Input: str = "Geeks", index = 2

Output: e

Input: str = "GeeksForGeeks", index = 5

Output: F

Explanation:

The code is here, just use your skills to modify it as per your requirements

// Java program to get a specific character

// from a given String at a specific index

class GFG {

// Function to get the specific character

public static char

getCharFromString(String str, int index)

{

 return str.charAt(index);

}

// Driver code

public static void main(String[] args)

{

 // Get the String

 String str = "GeeksForGeeks";

 // Get the index

 int index = 5;

 // Get the specific character

 char ch = getCharFromString(str, index);

 System.out.println("Character from " + str

     + " at index " + index

     + " is " + ch);

}

}

Basically you need to work little bit on the index part to search the character in the string. All the best.

Answered by anindyaadhikari13
1

Answer:-

This is the required program for the question.

import java.util.*;

class Java

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

String s="", n="";

int i;

char ch, x;

System.out.print("Enter a String: ");

s=sc.nextLine();

System.out.print("Enter the character: ");

ch=sc.next().charAt(0);

for(i=0;i<s.length()-1;i++)

{

x=s.charAt(i);

if(x==ch)

n+='@';

else

n+=x;

}

System.out.println("New String is: "+n);

}

}

Similar questions