Write a program email. c that asks the user for a first and last name, and print
ID: 3926128 • Letter: W
Question
Write a program email. c that asks the user for a first and last name, and prints out the user's UCSD email ID consisting of the first letter of the first name followed by at most 7 letters of the last name. The first name is assumed to consist of a single word and everything after the first space are assumed to be part of the last name. When creating the email ID, convert all letters to lower case, and ignore any hyphens and any spaces in the last name. You can assume that the user will always enter at least two words. (-) $ a. out Name: Hillary Clinton UCSD email ID: hclinton@ucsd.edu (-) $ a. out Name: Elizabeth Bowes-Lyon UCSD email ID: ebowesly@ucsd.edu (-) $ a.out Name: Andrew lloyd Webber UCSD email ID: alloydwe@ucsd.edu (-) $ a. out Name: Jean-Claude Van Damme UCSD email ID: jvandamm@ucsd.edu (-)$Explanation / Answer
Please find the required program along with its output. Please see the comments against each line to understand the step.
#include <stdio.h>
#include <string.h>
int main(void) {
int i=0, j=0;
char name[10];
printf("Enter name: ");
scanf("%[^ ]%*c",&name); //read name string that allows every character except enter
while (name[i] != ' ') { //find the position of first space
i++;
}
int spacePos = i;
char email[15];
char suffix[10] = "@ucsd.edu"; //email suffix
email[0] = tolower(name[0]); //get the first char
for(i=1, j=1; name[i] != '' && j<=7; i++){ //loop till we reach first 7 char or end of name
char c = tolower(name[spacePos+i]); //get char at position i
if(c >= 'a' && c <= 'z') { //if char c is an alphabet between a-z, then append it to email string
email[j++] = c;
}
}
strcat(email,suffix); //at the end, add suffix to the email strings end
printf("UCSD email ID: %s ",email); //print final email
return 0;
}
------------------------------------------------
OUTPUT:
sh-4.3$ main
Enter name: Hillary Clinton
UCSD email ID: hclinton@ucsd.edu
sh-4.3$ main
Enter name: Elizabath Bowes-Lyon
UCSD email ID: ebowesly@ucsd.edu
sh-4.3$ main
Enter name: Andew lloyd webber
UCSD email ID: alloydwe@ucsd.edu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.