Advertisement
Guest User

Initials

a guest
Nov 20th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. /** Jesse T. White
  7.  * Initials (less comfortable)
  8.  * Objective: Implement a program that, given a person’s name, prints a person’s initials.
  9.  */
  10.  
  11. int main(void)
  12. {
  13.     // Prompt the user for their name
  14.     string s = get_string("Enter your name: ");
  15.  
  16.      // Ensure get_string returned a string
  17.     if (s != NULL)
  18.     {
  19.         // Print the first initial
  20.         printf("%c", toupper(s[0]));
  21.     }
  22.  
  23.     // Initialise i = 0 and iterate over the string
  24.     for (int i = 0, n = strlen(s); i < n; i++)
  25.     {
  26.         // Identify the first letter of the last name
  27.         if (s[i] == ' ' && (s[i + 1] != '\0' && s[i + 1] != ' '))
  28.         {
  29.             printf("%c", toupper(s[i + 1]));
  30.         }
  31.     }
  32.     printf("\n");
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement