Question

In: Computer Science

How do I get the first initial of a first, middle, and last name? Also when...

How do I get the first initial of a first, middle, and last name? Also when I look to count characters in the name I want to be abel to count the spaces in-between the names how can i do this?

Solutions

Expert Solution

In order to get the the initials of a name, i.e., the first letters of first name, middle name and last name, we have to find the positions of the space. Let's see how it can be done with an example.

Say, the name is "Dennis MacAlistair Ritchie". Here we can see that space is present before M and R. We will have to position of the space and then choose the letter which is just after the space to get the initials of the middle name and last name. In order to find the initial of the first letter we just have to pick the first letter of the given name, here D.

Now the question arise how to find the spaces and also how to count it which is the second part of your question. Todo that all we have to is check whether each character is letter or not, i.e., whether each character lies between a-z or A-Z, if it does not lie there then it means that the character is space (since in general name contains only letters spaces). This process enables you to detec whether a given character is letter or not. If you just want to identify space, then simply check whether the given character is space or not. In order to count the number of spaces, just increment a counter whenever a space in found.

Here is a Java code for better understanding:

public class Main
{
   public static void main(String[] args) {
       Initial("Dennis MacAlistair Ritchie");
   }
   public static void Initial(String a)
   {
   String s=a.charAt(0)+""; //initial letter of first name
   int c=0; //to count the number of spaces
   for(int i = 0; i < a.length(); i++)
   {
   if(a.charAt(i) == ' ')
   {
   s=s+a.charAt(i+1); //to add the initial letters of middle and last name
   c++; //incremented when space is found
   }
   }
   System.out.println("The initials of "+a+" is "+s+ "\nThe space count is : "+c);
   }
}

Please refer to the screenshot of the code to understand the indentation of the code:

Output:

For any doubts or questions comment below.


Related Solutions

java programming write a program with arrays to ask the first name, last name, middle initial,...
java programming write a program with arrays to ask the first name, last name, middle initial, IDnumber and 3 test scores of 10 students. calculate the average of the 3 test scores. show the highest class average and the lowest class average. also show the average of the whole class. please use basic codes and arrays with loops the out put should look like this: sample output first name middle initial last name    ID    test score1 test score2...
1. How do I write a query that displays the name (concatenate the first name, middle...
1. How do I write a query that displays the name (concatenate the first name, middle initial, and last name), date of birth, and age for all students? Show the age with no decimal places, and only include those students who are 21 or older. Order by age, as shown below: (Hint: Use the TRUNC function. The ages may be different, depending on the date that the query is run.) SELECT S_FIRST || ' ' || S_MI || ' '...
write a regular expression that will, using capturing groups, find: last name first name middle name...
write a regular expression that will, using capturing groups, find: last name first name middle name (if available) student ID rank home phone work phone (if available) email program (i.e., S4040) grade Replace the entire row with comma-delimited values in the following order: first name,middle name,last name,program,rank,grade,email,student ID,home phone,work phone Example substitution string for the first student Jane,V,Quinn,S4040,SO,B,[email protected],Q43-15-5883,318-377-4560,318-245-1144,Y
Create a table with two columns. Name the table First Initial _ Last Name (e.g. John...
Create a table with two columns. Name the table First Initial _ Last Name (e.g. John Dow will create table j_dow). You have to audit all DML statements on your table. To do this you write two triggers: 1. To log any DML statements that users might run on this table. The results must be stored in the First Initial _ Last Name _ Log table (e.g. John Dow will create table j_dow_log). The table should have unique event ID,...
c++ How do I get it before a particular string when I use <ifstream>? For example,...
c++ How do I get it before a particular string when I use <ifstream>? For example, when it's 'Apple: fruit', I only want to get the previous ':' text (Apple). And then I want to move on to the next row. How can i do?
You have been asked to write program that allows the user to input a first name, middle initial (without the period)
You have been asked to write program that allows the user to input a first name, middle initial (without the period), and last name of a user and then display that person’s name with the first, middle initial followed by a period, and last name last.BEFORE creating the program, use structured programming principles to document how you are going to develop the program. Use Microsoft Word to complete this part of the assessment. Answer each of the following areas:Solve a...
Who get a performance appraisal? How do you get a performance appraisal? When when do you get a performance appraisal?
Who get a performance appraisal?How do you get a performance appraisal?When when do you get a performance appraisal?
When a variable is declared, it may also be initialized (given its initial – meaning first...
When a variable is declared, it may also be initialized (given its initial – meaning first – value) using the assignment operator (an equals sign). What is the initial value of cupSize?
In the following paragraphs, the first and last sentences are correct. Rewrite the middle sentences of...
In the following paragraphs, the first and last sentences are correct. Rewrite the middle sentences of to put the theme at the beginning and the rheme at the end of the sentences. Paragraph 1 Atoms of all elements consist of a central nucleus surrounded by a "cloud" containing one or more electrons. We can think of these electrons as occupying a series of well-defined shells. The number of electrons in its outermost shells determines the behaviour of a particular element....
How do you get the annual payment amount to this question? I was able to get...
How do you get the annual payment amount to this question? I was able to get the monthly payment amount which is $86,136.50 home / study / business / finance / finance questions and answers / a client of mr. richards wants to purchase a large commercial building. the building cost $20 ... Question: A client of Mr. Richards wants to purchase a large commercial building. The building cost $20 mil... A client of Mr. Richards wants to purchase a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT