KnowledgeBoat Logo

Computer Applications

Write a program to extract a portion of a character string and print the extracted string. Assume that m characters are extracted, starting with the nth character.

Java

Java String Handling

15 Likes

Answer

import java.util.Scanner;

public class KboatExtractSubstring
{
    public static void main(String args[])  
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter string : ");
        String s = in.nextLine();
        int len = s.length();
        System.out.print("Enter start index : ");
        int n = in.nextInt();
        
        if(n < 0 || n >= len)
        {
            System.out.println("Invalid index");
            System.exit(1);
        }
        
        System.out.print("Enter no. of characters to extract : ");
        int m = in.nextInt();
        
        int substrlen = n + m;
        if( m <= 0 || substrlen > len)
        {
            System.out.println("Invalid no. of characters");
            System.exit(1);
        }       

        String substr = s.substring(n, substrlen);
        System.out.println("Substring = " + substr); 
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to extract a portion of a character string and print the extracted string. Assume that m characters are extracted, starting with the nth character.

Answered By

5 Likes


Related Questions