0% found this document useful (2 votes)
276 views

Java String

The document provides examples of string manipulation programs in Java using methods like substring(), indexOf(), and concatenation. It includes 9 tasks to write programs that: 1. Remove the 2nd letter of a string 2. Add the 0th letter to the beginning of a string 3. Exchange the first two letters of a string 4. Exchange the 4th and 10th letters of a string 5. Insert a string between the 1st and 2nd letters of a string 6. Find and output the string after the first "x" in a string 7. Replace the first "x" with "y" in a string 8. Output the location of the second "x" in
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (2 votes)
276 views

Java String

The document provides examples of string manipulation programs in Java using methods like substring(), indexOf(), and concatenation. It includes 9 tasks to write programs that: 1. Remove the 2nd letter of a string 2. Add the 0th letter to the beginning of a string 3. Exchange the first two letters of a string 4. Exchange the 4th and 10th letters of a string 5. Insert a string between the 1st and 2nd letters of a string 6. Find and output the string after the first "x" in a string 7. Replace the first "x" with "y" in a string 8. Output the location of the second "x" in
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment : String

In a file name (say a.java) type following program. The file can be compiled by command Javac a.java Import java.io.*; import java.lang.*; class kapil { public static void main(String args[]) { String a,b;try{ DataInputStream o=new DataInputStream(System.in); A = o.readLine(); B = a.substring(2,5); System.out.println(b); } catch(IOException e) { System.out.println(e); } } } The program outputs sub string between positions 2 and 5 (including 2 but excluding 5). The first character is at 0th position. e.g. input qwertyuiuo output ert. b=a.substring(4); System .out.println(b); The program outputs string on and after 4th position. input qwertyuiuo output tyuiuo String a,b,c; a=o.readLine(); b=o.readLine(); c=a+b; System.out.println(c); The program takes two strings and joins them. 1.Write program to remove 2nd letter. Let the input string is pwsxtpbcderxrtxgt then output is pwxtpbcderxrtxgt. 2.Write program to add 0th letter in beginning. In above case ppwsxtpbcderxrtxgt

3.Write program to exchange first two letters. In above case wpsxtpbcderxrtxgt. 4.Write program to exchange 4th and 10th letter. In above case pwsxrpbcdetxrtxgt. 5.Write program to insert t between 1st and 2nd letter. In above case pwtsxtpbcderxrtxgt. [Hint: a+t+b]. A = o.readLine(); i = a.indexOf('x'); System.out.println (i); At what location 'x' is present. If more than one occurrence of 'x' is there then the location of first 'x' is returned. If 'x' is absent then -1 is returned. e.g. input wedxtyhxu output 3 int i; String a,b,c,d; I = a.indexOf('x'); b=a.substring(0,i); c=a.substring(i+1); d=b+c; System.out.println(d); The first 'x' in the given string is deleted. 6.Write a program, which reads a string and finds string after the first x. Let the input string is pwsxtpbcderxrtxgt then output is tpbcderxrtxgt. 7.Write program to replace first x by y. In above case pwsytpbcderxrtxgt. 8.Write program to output the location of second x. In above case 11. 9. Write program to print the string between 1st and 2nd x. In above case tpbcder

You might also like