473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to count a specefic word in aparagraph using string ????

write a java program to count aword in aparagraph?
ex: word "is" in "is name is the name is yours".
Dec 10 '10 #1
1 2650
Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.  
  3.     /**
  4.      * @param args the command line arguments
  5.      */
  6.     public static void main(String[] args) {
  7.         String string = "is name is the name is yours";
  8.         String match = "is";
  9.         System.out.println(instancesOf(string, match));
  10.     }
  11.  
  12.     static int instancesOf(final String string, final String match) {
  13.         int ret = 0;
  14.         final int stringlen = string.length();
  15.         final int matchlen = match.length();
  16.         for (int i = 0; i < stringlen - matchlen; i++) {
  17.             if (string.substring(i, i + matchlen).contains(match)) {
  18.                 ret++;
  19.             }
  20.         }
  21.         return ret;
  22.     }
  23. }
The function doesn't care about spaces. it will match is in island.
Dec 11 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Balakrsihna | last post by:
Hi All, Can anyone tell me how to replace an image in MS Word 2003 using c#.net. I am converting an html file to word doc and sending this as attachment using System.Web.Mail; the...
2
by: shapper | last post by:
Hello, I want to remove the first word of a string. For example: MyString = "Hello World! How are you?" I need to create 2 string: MyString1 = "Hello"
2
by: trezraven | last post by:
I am creating a template in Word 2007 using Visual Basic Editor. I am pulling information from an Access database. I know this is probably a stupid question, but for the life of me I can't figure out...
0
by: pshrinivassharma | last post by:
Hi I am writing a application where i need to read a word document and then i need to tag the important points and then i need to save those tagged data in a database . Then i need to use the...
3
by: kushal49 | last post by:
Hi, I need to count a particular word in a string in sql server 2000. Anybody please help me out in this...
0
by: Srinivas Dammalapa | last post by:
Sir, I am D.Srinivas. I am working on JobPortal Website(www.tlabsinc.com). I am downloading MS-Word document using ASP.NET 2.0 & C# 2.0. I am using IE (Internet Explorer). In my...
1
by: xetulul | last post by:
my problem is that im unable to match a user input word to a word in a string. the strings are in a file and then placed in nodes. i have to go through each token in each node to find the 'word'....
0
LegalIT
by: LegalIT | last post by:
Hello, I am creating a Word table using a VB.net application. I have it working with the following declaration: Dim table As Word.Table = myRange.Tables.Add(myRange, intRowCount, 5) I...
3
by: moizpalitanawala | last post by:
Hello I want to do reverse each word of the String example if input String is "Hello World" then the output should be "olleH dlroW" i Have tried this far class g { static String s="Hello...
0
by: Pramod Elig | last post by:
I developed this code which works fine in my current system which has office 2007, but when I run in word 2000 this doesn’t work. Can you please help me to code word automation using late binding. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.