Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Consider in a Document if a String " Hello" is Encoded and stored as "XYZAB"

I want to search the text on document for a word "Hello" and Replace the word with "HelloWorld"

The Program will encrypt the word "Hello" and Search the file then return the encrypted code as "XYZAB" Found

Now i have to replace the word "Hello" with "HelloWorld" in encrypted form so that the Letter "XYZABEFGHI" is replace in the place of Hello

where "World" is encoded as "EFGHI"

Now the Problem is If there is more number of occurrence of the word "Helloworld" exist in the file... How can i Replace only one particular occurrence What

can be done to select the particular occurrence.
Posted
Comments
joshrduncan2012 7-Feb-14 12:07pm    
What have you tried? Where are you stuck?
anamicaa 7-Feb-14 12:31pm    
@josh:Those above was my code if i search particular word and replace it means it replaces all word in the file.but my point is to search and replace the word where i want.can u give me the code for my problem??????

1 solution

Quote:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication7;

import java.io.File;
import java.util.Scanner;

/**
*
* @author Ragunath Gunasekaran
*/
public class JavaApplication7 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception

{

Scanner in = new Scanner(System.in);
Scanner file = new Scanner(new File("C:/Users/Ragunath Gunasekaran/Desktop/sample/file.txt"));

System.out.println("Enter the key : ");


// TIMER START

long startTime = System.currentTimeMillis();

long total = 0;
for (int i = 0; i < 10000000; i++)
{
total += i;
}



String key = in.nextLine().toUpperCase();

String input="";
while(file.hasNext())
input+=file.nextLine().toUpperCase();

System.out.println("PlainText : "+input);

String cipher="";
for(int i=0;i<input.length();i++)
{
char c = input.charAt(i);
if(c>='A'&&c<='Z')
{
c=(char)(c+(key.charAt(i%key.length())-'A'));
// if(c<'A')c+=26;
if(c>'Z')c-=26;
}
cipher+=c;
}

System.out.println("CipherText : "+cipher);


String plaintext1 = "";
for(int i=0;i<cipher.length();i++)>

{
char c = cipher.charAt(i);
if(c>='A'&&c<='Z')
{
c=(char)(c-(key.charAt(i%key.length())-'A'));
if(c<'A')c+=26;
// if(c>'Z')c-=26;
}
plaintext1 +=c;
}

System.out.println("PlainText : "+plaintext1);

// TIMER Run time & Print
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Runtime"+elapsedTime);
//TIME FINISH -Result Finish

}


}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900