Showing posts with label String manipulation. Show all posts
Showing posts with label String manipulation. Show all posts

Java - File handling - String Manipulation - remove unnecessary space between words


// File Handling
// String manipulations

/*
Write a program that copies the content of one file to another by removing unnecessary space between words
*/

import java.io.*;

class Filespace
{
     public static void main(String as[]) throws IOException
     {
          FileReader fr = new FileReader("ipt2.txt");
          FileWriter fw = new FileWriter("opt2.txt");
          BufferedReader br = new BufferedReader(fr);
         
          while(true)
          {
              String str = br.readLine();
             
               if(str==null)
                   break;             
             
              String sa[] = str.split(" ");
             
              for(String s:sa)
              {  
                   s = s.trim();
                   if(s.length()!=0)
                        fw.write(s+" ");
              }
              fw.write("\n");
          }
          fr.close();
          fw.close();
     }
}

String manipulation

# String manipulation

str1 = input("Enter string : ")

print("Length of string = ", len(str1))

L = str1.split(" ")
print("Reverse words in a string : ")
L.reverse()
for wd in L:
print(wd, end=" ")

Ntimes = int(input("\nEnter number of times to display string : "))
print(str1*Ntimes)

print("Enter two strings : ")
str2 = input()
str3 = input()

print("Concatenation of given strings : ", (str2+str3))

Str1="South India"

idx = Str1.index("India")

print("String splice : ")
print(Str1[idx:])