Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Fresher Job


 

Search Host

Monthly Fee($)
Disk Space (MB)
Register With us for Newsletter!
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

Tutorials

Java Server Pages

JAXB

Java Beans

JDBC

MySQL

Java Servlets

Struts

Bioinformatics

Java Code Examples

Interview Questions

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Web Promotion

Web Submission

Submit Sites

Manual Submission?

Web Promotion Guide

Hosting Companies

Web Hosting Guide

Web Hosting

Linux

Beginner Guide to Linux Server

Frameworks

Persistence Framework

Web Frameworks

Free EAI Tools

Web Servers

Aspect Oriented Programming

Free Proxy Servers

Softwares

Adware & Spyware Remover

Open Source Softwares

Joption pane
Expert:Thayanithy
The problem
A sentence can be thought of as one or more words which are delimited by spaces. For example: “A sentence is constructed with a series of words” The first word is ‘A’ The second word is ‘sentence’ There are nine words in total Java has a standard to index elements starting with 0. So the first word would be at position 0, the second word would be at position 1, etc “A sentence is constructed with a series of words”
Index 0 1 2 3 4 5 6 7 8 We need to provide a method which will insert a word into a string. The method will take the sentence to be altered, the word to be inserted and the word index location to place the word. The method will return the result as a String.
Method signature:
public static String insertWord(String sentence, String word, int position)


Example program output:
String word = “NEW”;
String sentence = “A sentence is constructed with a series of words”;

String one = insertWord(sentence,word,0);
// one would contain: “NEW A sentence is constructed with a series of words”

String two = insertWord(sentence,word,3);
// two would contain: “A sentence is NEW constructed with a series of words”

String three = insertWord(sentence,word,9);
// two would contain: “A sentence is constructed with a series of words NEW”

// in cases where the range of values is outside the possible index range,
// i.e. less than 0 or greater than the index needed for adding the word
// to the end the string. The methods should in return the string unaltered.

String two = insertWord(sentence,word,-1);
// two would contain: “A sentence is constructed with a series of words”

String three = insertWord(sentence,word,10);
// two would contain: “A sentence is constructed with a series of words”


Hint:
There are two possible solutions to the problem:

Solution one involves looking at the methods of the string class, such as charAt and substring.

The other solution would be to use the scanner class, as discussed during the lectures.
Answers
Hi friend,

Code to solve the problem :


class InsertWord
{
public static String insertWord(String sentence, String word, int position)
{
String sentenceAr[] = sentence.split(" ");
String strAr[] = new String[sentenceAr.length+1];
String stringResult=" ";
if(position==0)
{
System.out.println("position : " + position);
strAr[0] = word;
stringResult +=" "+strAr[0];
for(int i=1;i<strAr.length;i++)
{
strAr[i] =sentenceAr[i-1];
stringResult +=" "+ strAr[i] + " ";
}
}
else if(position!=0)
{

for(int i=0;i<position;i++)
{
strAr[i] =sentenceAr[i];
stringResult +=" "+ strAr[i];
}
strAr[position] =word;
stringResult +=" "+ strAr[position];
for(int i=position+1;i<strAr.length;i++)
{
strAr[i] =sentenceAr[i-1];
stringResult +=" "+ strAr[i];
}
}


return stringResult;

}
public static void main(String[] args)
{
String sentence ="Hello World";
String word ="Roseindia";
int position = -1;
String sentenceAr[] = sentence.split(" ");
if(!(position>sentenceAr.length || position<0))
System.out.println(insertWord(sentence, word, position));
else
System.out.println(sentence);
}
}

Thanks

More Questions
Post Answers
 
Ask Question Facing Programming Problem?
Useful Links
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.