String in Java - Word Programs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this video we will cover how to solve different string programs which require you to break a sentence into words in our last video we covered a set of programs which require you to read a string character by character in this video we will learn how to read a string word by word and then process it now to do the string programs you need to know character class and string class methods we have already covered these in separate videos so in this video we will focus on only how to solve different programs using these methods when i am saying word programs it means all those programs where you have a sentence and you have to break it into words to do some processing these could be just simple printing programs asking you to print any specific words or counting or finding words kind of programs or you could be asked to edit the string to make some changes to the word like capitalize each word etc now to do these programs you need a string input this input could be taken in from the user using scanner class or you could be pass this string as a function parameter the string could also be available as an instance variable for our program we will assume you have a string and we will start the program from here we will assume you will know how to insert the program in main or a function as required by the question when we did character level programs we created a template which could read a string character by character now to solve any program which requires you to read a string word by word we will create a template which will now give us the word every time we can then just add the logic in it as per the question so how do we create this template let's take a sentence and see what breaks a sentence into words when we start reading a sentence when we encounter a space we get one word then again from this space to next space is the next word and so on so space character is what gives us the end of the word we will use this logic for extracting each word from the sentence now java actually has a string tokenizer class in util library we can pass the string and the delimiter we are using like space and it extracts words or tokens for us you can use this as a template for any of our word programs if you are not using string tokenizer i will now explain how you can read a string character by character and form a word so we start with a character template which takes a string and gives us character by character in the same template we will first define a string variable w outside the loop which will hold a word every time so when we start traversing the string using this template we will read character by character we will check if the character is not a space if it is not a space we will just add the character to the variable w we will continue traversal till we get a space space means we have a complete word and now we can do word processing as required in a program an important thing we need to do after we have done our processing is that we will set our variable w to blank so that we can fill it up with the next word this is important otherwise the next word will just get concatenated to the previous word which will be wrong this process will continue for all words in a sentence now we have a little problem when we reach the last word for each word we have been using space to tell us that we have one word ready but the last word does not have a space in the end so the loop will end without giving us the last word how do we fix it what we will do is add a space to the end of our string before the loop so that we can get the last word and then the loop can end so we will use this template to solve all our word problems so our first program is to print each word in a separate line we already have a template ready in the program we are just asked to print each word so in the else block we will just add a line to print the variable w which stores our word every time this will print each word and give us the program using this template now what if the program is to print only the first character of each word in that case in the same template we will extract the first character of the word by using carat 0 and then we will print it this will give you the first character of each word similarly if the program is to print the first two characters of each word in the print statement we can use the substring function to extract more characters this program will extract the first two characters of each word and print it now what if the program was to print only the word starting with r in that case we will again take a template now every time we get a word we put a if condition where we will use the startswith function to check if the word starts with r if yes then we will print it this will give the output and the desired program you can get multiple variations of the program like instead of starts with it can be ending with specific string like print all words ending with ed so you just need to change the function over here or it could be print string which contains a given character sequence let's say print all words which contains letter e so we can just use the contains function in it if it is true it will print the word otherwise it will not print the program could also be to print only words which have three characters we will just change our if condition to check which all words have length 3 if yes we will print it this will give us the desired output and the program the next program we will see is to print all special words in a sentence a special word is a word which starts and ends with the same character like anna bob etc here too we will take our template in word processing we will extract the first character of the word by using w dot care at 0 and the last character of the word by using w dot cad at w dot length minus 1 we will then compare if both are the same since there could be differences in case you can convert the string itself or the characters here to uppercase if they match we print the word this is the program for printing special words or the program could be to find the longest word we again take a template here we declare a variable to store the longest word before the loop when we get a word we just compare if the length of it is greater than the longest word we have stored outside if yes then the new word becomes the longest word once we have finished comparing with all words or we have reached the end of the sentence we print the longest word outside the loop this gives us the required program now the program could be to find if a matching word exists let's say we find the word or or you could be asked to take the search word from the user as well we will again take our template we will first initialize account variable to 0 outside the loop we will write an if condition with equals or equals ignore case to check if the word matches if yes we will just increment the count once outside the loop we will check if count is zero this means that the word is not found else the word is found you can also print count to tell the frequency of the word in the sentence now instead of printing you could get count programs like the program to give number of words in a sentence here again we will take a template we will initialize a count variable outside the loop we will increment it every time we get a word and we will print it once we have traversed the entire string this program will give us number of words in a string the program could also be to count all words ending with d so we will just add a if condition and use ends with function to check for words ending with d this will give us the program to count the words ending with d so actually we can convert any of the programs we did earlier for printing like print words starting with t or words with specific length by just adding a count variable before the loop change the if condition as required in the question and instead of print write count plus plus and print this count once we are outside the loop this will give you the count of words instead of printing them now let's do the third category of programs where you have to create a new string by making some changes to the word let's start with the program which is to change the word red to blue and create a new sentence here again we will take a template this time since we have to create a new string so we will declare a string variable before the loop when we get a word we will use if with functions equal to check if the word is red if it is we will add blue to the new string otherwise we will just add the word as is there is one important thing to remember here when we are forming a sentence we have to remember to put space in between the words so we will always add a space in the end once we are out of the loop we will print this new sentence this gives us the program to change a word the program could also be to delete a word let's say delete read from the sentence the word to be deleted can be taken in from the user as well here again we will take our template we will declare a string variable before the loop this time we will use not in front of equals to check if the word is not equal to red only then we will add it to the new sentence we will add space between each word as required once we are out of the loop we will print this new sentence this gives us the program to delete a word let's take the next program which takes a string and changes case of alternate word so the first word should be uppercase second word lowercase then again uppercase and so on here again we will take a template we will declare a string variable before the loop since we need to keep track of which is an alternate word we will declare a count variable to outside the loop inside the loop we will check if count is even or odd if it is even we will convert the word to uppercase and if it is odd we will convert the word to lowercase and then add it to our new string we will increment the count every time we get a new word once out of the loop we will print it this gives us the program to generate a sentence with alternate cases next program is to capitalize each word of the sentence here again we will take a template we will declare a string variable before the loop inside the loop we will first extract the first character of the word by using carat 0 and convert it into uppercase then we will use the substring function to get the remaining part of the string we will concatenate these two to get a capitalized word and add it to the new sentence along with space once we are out of the loop we will print this new sentence this gives us the program to capitalize each word next program is to swap the first and last letter of each word here again we will take a template we will declare a string variable before the loop inside the loop we will just first extract the first character of the word using carat 0 and the last character by using carat with w dot length minus 1 which gives us the last index of the word then we will use the substring function from 1 to w dot length minus 1 to get the middle part of the string we will now just concatenate the last character in the beginning and the first character in the end of the string to get our swapped word we will add it to the new sentence along with space once we are out of the loop we will print this new sentence this gives us the program to swap the first and last letter of each word with this we have done a set of programs which require you to do word by word manipulation if you have any doubts you can always reach out to us at simplycoding.n thank you and all the best [Music]
Info
Channel: Simply Coding
Views: 22,980
Rating: undefined out of 5
Keywords: Simply Coding, simplycoding, CBSE, ICSE, SSC, Computer Applications, Computer Science, Information technology, NCERT, String in java, String programs in java, icse string programs, isc string programs, Special word in java, count words in java, print word in java
Id: hmMpfZWbNq0
Channel Id: undefined
Length: 16min 5sec (965 seconds)
Published: Wed Mar 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.