Write a program that reads a sentence from the user. The sentence must contain a
ID: 3628693 • Letter: W
Question
Write a program that reads a sentence from the user. The sentence must contain at least 3 words and upto 4 words. It then creates a password for the user by taking the kth letter from each word at position k and then concatenates an integer based on the length of each word in the sentence. If the kth word does not contain a letter at position k, then the program starts counting from the beginning. For example, (user responses are underlined)Enter a sentence> My name is Sameer
Your password is Maie2426.
Here the password is constructed is as follows:
My: 1st letter of the 1st word is M
name: 2nd letter of the 2nd word is a
is: There is no 3rd letter of the 3rd word, so we recount from the beginning and get i
Sameer: 4th letter of the 4th word is e.
The length of My => 2, length of name => 4, length of is => 2, length of Ahmed => 6
Hence the password Maie2426.
This is really really hard for me. it's not a homework nor exam question.
Explanation / Answer
please rate - thanks
I think I deserve a helpful for the first answer.
I did double work due to your error. I messaged you
asking what language, and you didn't respond
import java.util.*;
public class main
{public static void main(String [] args)
{Scanner in=new Scanner(System.in);
int numWords,i,j;
int []beg=new int[4];
int[] end=new int[4];
char n;
String password="",input;
do
{
System.out.print("Enter a sentence: ");
input=in.nextLine();
numWords=countWords(input,beg,end);
}while(numWords<0);
for(i=0;i<numWords;i++)
if(end[i]-beg[i]>=i)
password=password+input.charAt(beg[i]+i);
else
password=password+input.charAt(beg[i]+(end[i]-beg[i]+1)%i);
for(i=0;i<numWords;i++)
{ j=end[i]-beg[i]+1+48;
password=password+(char)j;
}
System.out.println("your password is "+password);
}
public static int countWords(String input,int b[],int e[])
{int n=0,i,c=0;
b[n]=0;
for(i=1;i<input.length();i++)
if(input.charAt(i)==' ')
{e[n]=i-1;
n++;
b[n]=i+1;
}
e[n]=i-1;
n++;
if(n<3||n>4)
{System.out.println("must be 3 or 4 words ");
return -1;
}
return n;
}
}