String – Find and replace the character (First occurrence) Program in java

Question:

Write a Java program to find a character from a sentence and replace it with another character. If the character is not found in the string print “character not found”.

Note: Replace only the first occurrence.

Sample input 1:

Enter the string:

java programming

Enter the character to be searched:

a

Enter the character to replace:

o

Sample output 1:

jova programming

Sample input 2:

Enter the string:

java programming

Enter the character to be searched:

e

Enter the character to replace:

o

Sample output 2:

character not found

CODE:

import java.util.*;
public class FirstOccurence 
{
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the string:");
        char[] str=sc.nextLine().toCharArray();
        
        System.out.println("Enter the character to be searched:");
        char srch=sc.next().charAt(0);
        
        System.out.println("Enter the character to replace:");
        char replace=sc.next().charAt(0);
        
        int flag=0;
        int len=str.length;
        for(int i=0;i<len;i++)
        {char r=str[i];
            if(r==srch)
            {
                str[i]=replace;
                flag++;
                break;
            }
        }
        if(flag==0)
        {
            System.out.println("character not found");
        }
        else
        {
            for(char i : str)
            {
                System.out.print(i);
            }
        }
    }
}
public class Student 
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    
    public void setId(int id)
    {
        this.id=id;
    }
    
    public int getId()
    {
        return this.id;
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    
    public int[] getMarks()
    {
        return this.marks;
    }
    
    public void calculateAvg()
    {   float sum=0;
        
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
        
    }
    
    public void setAverage(float average)
    {
        this.average=average;
    }
    
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    
    public float getAverage()
    {
        return this.average;
    }
    
    public char getGrade()
    {
        return this.grade;
    }
}
Next:
  1. Sort the first and second half of an array
  2. Retail Shop
  3. Palindrome
  4. Numerology
  5. InitCap
  6. Array Compatiblilty

Leave a Comment

Scroll to Top