Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*CS50 pset2 Jason Leone */
- // File initalsFunction.c
- #include <cs50.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- // prototype functions
- char Initials1(string userName);
- char Initials2(string userName);
- int main(void)
- {
- // get userName with GetString
- string userName = GetString();
- // pass argument userName to functions
- Initials1(userName);
- Initials2(userName);
- // gets return value of x (first initial) from function Initials1
- char x = Initials1(userName);
- // gets return value of y (second initial) from function Initials2
- char y = Initials2(userName);
- // prints x and y (first and last initial) and uses toupper to case correct
- printf("%c", toupper(x));
- printf("%c", toupper(y));
- printf("\n");
- }
- // function to get first initial from the array
- char Initials1 (string userName)
- {
- // declare the variable
- char x;
- // loop getting the length of string with strlen
- for (int i = 0, n = strlen(userName); i < n; i++)
- {
- // get the first letter in the array and assign it to x
- if (userName[0])
- {
- x = (userName[0]);
- }
- }
- // return the value of x (the first initial) to main
- return x;
- }
- // function to get the second initial from the array
- char Initials2 (string userName)
- {
- //declare the variable
- char y;
- for (int i = 0, n = strlen(userName); i < n; i++)
- {
- // find the letter in the array after the space and assign it to y
- if (userName[i] == ' ' && userName[i + 1])
- {
- y = (userName[i + 1]);
- }
- }
- // return the value of y (the second initial) to main
- return y;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement