Posted By

bhart on 10/27/13


Tagged


Versions (?)

The Rage


 / Published in: C
 

It is the rage

  1. /********************************************************
  2. * Author: Brendan Hart
  3. * Date: 07/07/13
  4. *
  5. * Objective: Take a user inputed string and print out a
  6. * new string that altrenates between upper and lowercase
  7. * letters
  8. *
  9. * Demostrates: Arrays, For Loops, C Functions, User Input
  10. *
  11. ***********************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <cs50.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17.  
  18. int main(void)
  19. {
  20. // get a string from the user
  21. printf("Give me a string that you would like to become enraged: ");
  22. string s = GetString();
  23.  
  24.  
  25. // turn string into an array of characters
  26. if (s != NULL)
  27. {
  28.  
  29. // for loop to go through all the characters
  30. // and print them out
  31. // strlen function to see the end of the for loop
  32. // create a new varaible for strlen so function doesn't call itself every time
  33.  
  34. for (int i = 0, n = strlen(s); i < n; i++)
  35. {
  36. // print uppercase letter
  37. // modulo to assign letter placement (0, 1, ex..) a number
  38. // this number determines if we can capitalize it or not
  39. // the number that i is deterines which letter in the string gets capitalized
  40. if (i % 2 == 0)
  41. {
  42. printf("%c", toupper(s[i]));
  43. }
  44. // print lowercase letter
  45. else
  46. {
  47. printf("%c", s[i]);
  48. }
  49. }
  50.  
  51. // make sure the letters are all on one line
  52. printf("\n");
  53. }
  54.  
  55. }

Report this snippet  

You need to login to post a comment.