![a1supernut37owozkj](/avatars/4319.jpg)
a1supernut37owozkj
05.11.2020 •
Computers and Technology
Write code which takes a sentence as an input from the user and then prints the length of the first word in that sentence.
Hint: think about how you know where the first word ends - you may assume that the sentence contains more than one word.
Input a code in the following:
/* Lesson 3 Coding Activity Question 4 */
import java.util.Scanner;
public class U2_L3_Activity_Four{
public static void main(String[] args){
/* Write your code here */
}
}
Solved
Show answers
More tips
- O Other How to Accidentally Get a Rare Coin with Your Change and How to Know Its Value?...
- S Society and Politics How Could Nobody Know About the Dead Mountaineers?...
- D Dating, Love, Relationships Why do we feel shame?...
- F Family and Home Why Does God Punish Us Constantly and How Can We Fix It?...
- F Family and Home Do Lullabies Help Babies Sleep or Is it Just a Myth?...
- B Business and Finance How to Get Your Money Back When Lending? Top 7 Ways...
- S Society and Politics Выборы: Смысл, Значение и Отражение...
- D Dating, Love, Relationships Why Should the Man be Active and the Woman Passive during Foreplay?...
- P Philosophy How did the concept of module arise in computer science?...
- S Society and Politics Why are thugs called gopniks ? A fascinating journey through Russian subculture...
Answers on questions: Computers and Technology
- C Computers and Technology Assuming myvalues is an array of int values, and index is an int variable, both of the following statements do the same thing. cout myvalues[index] endl; cout *(myvalues...
- C Computers and Technology Which action is a safety precaution for a camera? Jeff loves to record nature with his prosumer camera. To charge his camera, he uses any available charger. When...
- C Computers and Technology need help plz. wouldn t lst me post the question since it has forbidden language. Here is the picture. Thank You! This is in C#....
- M Mathematics Someone please help me ASAP...
- B Business Has anybody took financial literacy on edmentum? Please reply to this....
- C Chemistry Hey and who wants to be my friend...
- H History What s good might come from more government programs and regulations...
- F French Lisez la lettre et répondez aux question....
- M Mathematics If 10x=40 then what is x...
- M Mathematics Two teachers took out loans from a bank. Mr. Ramirez took out a 3-year loan for $7,500 and paid 4.8% annual simple interest. Mr. Vasquez took out a 5-year loan...
Ответ:
Following are the java code to the given question:
import java.util.Scanner;//import package
public class U2_L3_Activity_Four //defining a class U2_L3_Activity_Four
{
public static void main(String[] args) //defining main method
{
String s;//defining String variable
int x,v=0;//dclaring integer variable x,v
Scanner obx = new Scanner(System.in);//creating Scanner class Object
System.out.println("Enter a sentence.");//print message
s = obx.nextLine();//input string value
for (x = 0; x < s.length(); x++)//defining loop to count string length
{
char c = s.charAt(x);//defining char variable that hold char value
if (c != ' ')//defining if that check space value
{
v++;
}
}
System.out.println("The first word is " + v +" letters long");//print length of first word
}
}
Output:
Please find the attached file.
Program Explanation:
Import package.Defining a class U2_L3_Activity_Four, inside the class main method is declared.Defining a string variable "s" and an integer variable "x,v=0", and a scanner class object is created that inputs string value.In the next step, a for loop is defined that checks the first word in the string value and prints its length value.Learn more:
link
Ответ:
import java.util.Scanner;
public class U2_L3_Activity_Four {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence.");
String sent = scan.nextLine();
int count = 0;
for (int i = 0; i < sent.length(); i++){
char c = sent.charAt(i);
if (c != ' '){
count++;
}
else{
break;
}
}
System.out.println("The first word is " + count +" letters long");
}
}
We check to see when the first space occurs in our string and we add one to our count variable for every letter before that. I hope this helps!
Ответ: