Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My solution to CS50 pset2 - "Initializing"
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
/**
*
* Write, in a file called initials.c, a program that prompts a user for
* their name (using GetString to obtain their name as a string) and then
* outputs their initials in uppercase with no spaces or periods,
* followed by a newline (\n) and nothing more.
*
* */
int main(void)
{
string name = GetString();
//print the first initial as an uppercase letter
printf("%c", toupper(name[0]));
//loop and find space characters.
for (int i = 0; i < strlen(name); i++)
{
if (name[i] == ' ')
{
// print next character as an uppercase char
printf("%c", toupper(name[i + 1]));
}
}
//print new line
printf("\n");
}
@CraigRodrigues

This comment has been minimized.

Copy link
Owner Author

commented May 31, 2016

Steps:

  1. Get input from the user.
  2. Put name into an array (didn't actually need to do this. String already does this).
  3. Print out the first string's first char as a capital letter.
  4. Scroll through the string and find the space.
  5. Print the next character after the space as a capital letter.
  6. Go back to 4-5 until at end of string.
  7. Print new line.

Notes:

  • I can use the library ctype.h to call the function toupper to return an uppercase version of the character!
  • int toupper (int ch);
  • Need to increment through a loop to find the null terminator and print the first character after it? (nope)
  • How would I account for the last null terminator then?
  • Didn't actually need to look for the null terminator, just the spaces...duh.
  • When I did the first loop I forgot to initialize i to anything and it threw an error.
  • for (i = 0) instead of for (int i = 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.