Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- /** Jesse T. White
- * Initials (less comfortable)
- * Objective: Implement a program that, given a person’s name, prints a person’s initials.
- */
- int main(void)
- {
- // Prompt the user for their name
- string s = get_string("Enter your name: ");
- // Ensure get_string returned a string
- if (s != NULL)
- {
- // Print the first initial
- printf("%c", toupper(s[0]));
- }
- // Initialise i = 0 and iterate over the string
- for (int i = 0, n = strlen(s); i < n; i++)
- {
- // Identify the first letter of the last name
- if (s[i] == ' ' && (s[i + 1] != '\0' && s[i + 1] != ' '))
- {
- printf("%c", toupper(s[i + 1]));
- }
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement