Advertisement
jasonleone

initalsFunction.c

Jul 2nd, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. /*CS50 pset2 Jason Leone */
  2. // File initalsFunction.c
  3.  
  4. #include <cs50.h>
  5. #include <ctype.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. // prototype functions
  10. char Initials1(string userName);
  11. char Initials2(string userName);
  12.  
  13. int main(void)
  14. {
  15.     // get userName with GetString
  16.     string userName = GetString();
  17.    
  18.     // pass argument userName to functions
  19.     Initials1(userName);
  20.     Initials2(userName);
  21.    
  22.     // gets return value of x (first initial) from function Initials1
  23.     char x = Initials1(userName);
  24.    
  25.     // gets return value of y (second initial) from function Initials2
  26.     char y = Initials2(userName);
  27.    
  28.     // prints x and y (first and last initial) and uses toupper to case correct
  29.     printf("%c", toupper(x));
  30.     printf("%c", toupper(y));
  31.     printf("\n");
  32. }
  33.    
  34. // function to get first initial from the array
  35.  
  36. char Initials1 (string userName)
  37. {  
  38.     // declare the variable
  39.     char x;
  40.    
  41.     // loop getting the length of string with strlen
  42.     for (int i = 0, n = strlen(userName); i < n; i++)
  43.     {
  44.         // get the first letter in the array and assign it to x
  45.         if (userName[0])
  46.         {
  47.             x = (userName[0]);
  48.         }
  49.     }
  50.    
  51.     // return the value of x (the first initial) to main
  52.     return x;
  53. }
  54.  
  55. // function to get the second initial from the array
  56. char Initials2 (string userName)
  57. {
  58.     //declare the variable
  59.     char y;
  60.    
  61.     for (int i = 0, n = strlen(userName); i < n; i++)
  62.     {  
  63.         // find the letter in the array after the space and assign it to y
  64.         if (userName[i] == ' ' && userName[i + 1])
  65.         {  
  66.             y = (userName[i + 1]);
  67.         }
  68.     }
  69.    
  70.     // return the value of y (the second initial) to main
  71.     return y;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement