Question
Describe every line of the given code (also describe output) for Viva Sample run: Project code Question: Given a string s consisting of some words
Describe every line of the given code (also describe output) for Viva
Sample run:
Project code Question:
Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only.
Answer:
Project Code :
#include
#include
using namespace std;
//assume this value
int main()
{
//get the string string
str ="hello poly awesome";
//store the length of last word
int lastLength=0;
//store index
int index=0;
//loop for complete string
for(unsigned int i=0;i //if current is sapce if(str[i]==' '){ //if index is not zero if(index!=0){ //set last length to index lastLength = index; } index=0; }else{ index++; } } if(index!=0){ lastLength = index; } cout return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started