Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Basics
Home > Programs > Basics

1. Write a program to print next to the last word of a sentence.

Answer:

import java.io.*;
class test
{
    public static void main(String args[]) throws IOException 
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s;
        while((s=br.readLine())!=null)
        {
            /* One Method to find Second last Word */
            
            String str=s.substring(0,s.lastIndexOf(" "));
            String str1=str.substring(str.lastIndexOf(" "),str.length());
            System.out.print(str1);
            
            /*Second Method to find Second last word #NOT WORKING */
            
            String[] arr=s.split(" ");
            String result=arr[arr.length-2];
            System.out.print(result);
        }
    }
}

Post Your Answer Here:

Name *
Email

rai said:Sun, May 14, 2017, 8:57:14 pm

Language:

import java.io.*;
class test
{
    public static void main(String args[]) throws IOException 
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s;
        while((s=br.readLine())!=null)
        {
            /* One Method to find Second last Word */
            
            String str=s.substring(0,s.lastIndexOf(" "));
            String str1=str.substring(str.lastIndexOf(" "),str.length());
            System.out.print(str1);
            
            /*Second Method to find Second last word #NOT WORKING */
            
            String[] arr=s.split(" ");
            String result=arr[arr.length-2];
            System.out.print(result);
        }
    }
}

Post Your Reply Here:



vikash said:Wed, June 7, 2017, 10:01:45 am

Language:

#include<iostream>
using namespace std;
int main(){
      int t;
          cin>>t;
                      while(t--){
                             cin.ignore();
                           char str[100];
                            int l=0;
                        char nstr[100];
                               cin.getline(str,100);
                                       for(int i=0;str[i]!='\0';i  ){
                                                 l  ;
                                       }
                                int k=0,j=0;
                                  for(int i=l;i>=0;i--){</p>

<p>                                                      if(j==1

Post Your Reply Here:



vikash said:Wed, June 7, 2017, 10:17:11 am

Language:

<p>what the hack!..this editor is shit...not showing whole code</p>

Post Your Reply Here:



vikash said:Wed, June 7, 2017, 10:12:03 am

Language:

#include<iostream>
using namespace std;
int main(){
      int t;
          cin>>t;
                      while(t--){
                             cin.ignore();
                           char str[100];
                            int l=0;
                        char nstr[100];
                               cin.getline(str,100);
                                       for(int i=0;str[i]!='\0';i  ){
                                                 l  ;
                                       }
                                int k=0,j=0;
                                  for(int i=l;i>=0;i--){

                                                      if(j==1

Post Your Reply Here:



Monika said:Sun, June 11, 2017, 12:28:52 am

Language:

<!DOCTYPE html>
<html>
<body>

<?php 
function Second_last() {
    
$str ="what is your name"/* input string */
    
echo  "Input string is :" $str."<br/>";
    
/* conversion of string into array */
    
$arr explode" "$str);   
    
/* counting lengh of array */    
    
$arrlength=count ($arr);     
   
/* printing 2nd last value( including blank space) */    
    
echo $arr[$arrlength-2];                                
}
Second_last();
?>  

</body>
</html>

Post Your Reply Here:



Varun said:Sat, June 17, 2017, 7:46:00 am

Language:

import java.io.*;
class test
{
    public static void main(String args[]) throws IOException 
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s;
String str,str1;
int c=0;
s=br.readLine();
/* With string compariosons there may be an arrayoutofbounds exception rather that statement can be replaced with this */
        while((c<=s.length()))
        {
            /* One Method to find Second last Word */
            
            str=s.substring(0,s.lastIndexOf(" "));
            str1=str.substring(str.lastIndexOf(" "),str.length());     
c  ;
if(c==1)
System.out.print(str1);            
        }
    }
}

Post Your Reply Here:



Aditya said:Thu, July 6, 2017, 8:50:10 pm

Language:

#include <stdio.h>

int main()
{ char str[20];
printf("enter string");
gets(str);
getLastWord(str);
char lastword[5];
    int last=text.size()- 1;
    int beginlast=0;
    if (text == "")
    return  "";
    for (int i=last; i>=1; i--)
    {
        if (isspace(text[i]))
            beginlast=beginlast+i;
    }
    for (int k=0; k!=text.size; k++)
    {
        if (isalpha(text[k]))
            lastword=lastword+lastword[k];
    }
return 0;
}

Post Your Reply Here:



Neha said:Thu, July 6, 2017, 9:00:45 pm

Language:

#include <iostream>
using namespace std;
 
string getLastWord(string text)
{
    string lastword="";
    int last=text.size()- 1;
    int beginlast=0;
    if text == "";
    return  "";
    for (int i=last; i>=1; i--)
    {
        if (isspace(text[i]))
            beginlast=beginlast+i;
    }
    for (int k=0; k!=text.size; k++)
    {
        if (isalpha(text[k]))
            lastword=lastword+lastword[k];
    }
    return lastword;
}
int main()
{ char str[20];
cout<<"enter string";
cin>>str;
getLastWord(str);
return 0;
}

Post Your Reply Here:



Rajeev Ranjan said:Mon, August 14, 2017, 10:44:43 pm

Language:

<p>import java.io.BufferedReader;<br />
import java.io.IOException;<br />
import java.io.InputStreamReader;</p>

<p>public class StringWordIBM <br />
{</p>

<p>    public static void main(String[] args) throws IOException <br />
    {<br />
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));<br />
        String str;<br />
        System.out.println("Enter the String : ");<br />
        str=br.readLine();<br />
        System.out.println(str);<br />
        String s[] = str.split(" ");<br />
        System.out.println("Splitted string : ");<br />
        for(int i=0;i<s.length;i  )<br />
        {<br />
            System.out.println(s[i]);<br />
            <br />
        }<br />
        System.out.println("Second Last word of String is : " s[s.length-2]);<br />
    }<br />
    <br />
}</p>

Post Your Reply Here: