Thursday, May 29, 2014

String Function - V [ TCS - Assignments ]




Input a Sentence and a stretch factor, each word will be repeated according to the given stretch factor.

Ex.
String “Hello! Friends.
Stretch =2

Final String – Hello! Hello! Friends. Friends.




import java.io.*;
import java.util.*;
public class Stretch_Factor
{  
    public static void main(String[] args)throws IOException
        {
           BufferedReader Br = new BufferedReader (new InputStreamReader (System.in));
           int N=0;
           String S,S1="";;
           int P;
           System.out.print("Enter a String ");
           S=Br.readLine();
           System.out.print("Enter a Streach ");
           P=Integer.parseInt(Br.readLine());
           String List[]=S.split(" ");
           Stretch(List,P);
        }
      public static void Stretch(String List[], int P)
        {
                                      for(int j=0;j<List.length;j++)
                                                {
                                                          String s="";
                                                          for(int i=0;i<P;i++)

                                                                   {

                                                                             s=s+List[j]+" ";
                                                                   }
                                                          List[j]=s;
                                                }
                                                for(int i=0;i<List.length;i++)
                                                   {
                                                                   System.out.print(List[i]);
                                                   }

                   }           
        }

           



Output:


Enter a String Man is the artificer of his own happiness.

Enter a Streach 2

Man Man is is the the artificer artificer of of his his own own happiness. happiness.



Input a Sentence, if the word contain a number then repeat next letter for given number of time.

Ex.

String “Wo3rk”
Final String “Worrrk”



import java.io.*;
import java.util.*;
public class Repeat
{  
    public static void main(String[] args)throws IOException
        {
            BufferedReader Br = new BufferedReader (new InputStreamReader (System.in));
            int N;
            String S,S1="";;
            int P;
            System.out.print("Enter a String ");
            S=Br.readLine();
            for(int i=0;i<S.length();i++)
                {
                    if(S.charAt(i)>48 && S.charAt(i)<=57)
                        {
                            N=S.charAt(i)-48;
                            for(int j=1;j<N;j++)
                                {
                                    S1=S1+S.charAt(i+1);
                                }
                               
                               
                        }
                        else
                        S1=S1+S.charAt(i);
                   
                }
           
            System.out.println(S1);
        }
    }
           


Output:

Enter a String th2is is t3est.

thiis is teeest.



No comments: