Respuesta :
Corrected (Modified) Question:
i. Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is a String containing the last character of the value of name. So if the value of name were "Smith" the expression's value would be "h".
ii. Given a String variable named sentence that has been initialized, write an expression whose value is the number of characters in the String referred to by sentence.
Explanation of the corrected question
The whole question has been numbered (i) and (ii) just to separate or divide it into clearer sub questions.
Some parts of the question has also been removed since they are repetition of a part of the question.
Answer:
(i) name.charAt(name.length() - 1)
(ii) sentence.length()
Explanation:
No language has been specified in the question for the code to be written in. However, I have chosen to write it in Java.
(i) In Java, to get a particular character in a string str, the function, charAt(x) is used, where x in the function represents the index of the character to be fetched. This is written as str.charAt(x).
In our case, x is the index of the last character in our string. To get the index of the last character in a string say str, the length of the string itself is used. However, indexing starts at zero. Therefore, to get the index, it will be the 1 subtracted from the length of the string as follows:
str.length() - 1
But note that, to get the length of a string, the method length() is used. For example if,
String str = "omobowale";
str.length() will return 9
Now to the question at hand, our string variable name is name.
Therefore to get its last character, we write:
name.charAt(name.length() - 1)
So if;
name = "Smith";
name.charAt(name.length() - 1) will return "h"
(ii) As explained in (i) above, to get the length (number of characters) of the variable string sentence, we simply write;
sentence.length()
PS: The length of a string is the number of characters present in the string.
Hope this helps!