Advertisement
JeremyDwayne

String-Case.c

Feb 6th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. /* string-case.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7.  
  8. void helpMessage(){
  9.     printf("usage: ./string-case [-luh] <<< <String>\n");
  10.     printf("-l Convert to lower case\n");
  11.     printf("-u Convert to upper case\n");
  12.     printf("-h Print help message\n");
  13. }
  14.  
  15. void stringMod(char c,char *string){
  16.     for(int i = 0; i < strlen(string); i++)
  17.     {
  18.         if(c == 'l'){
  19.             putchar(tolower(string[i]));   
  20.         }
  21.         if(c == 'u'){
  22.             putchar(toupper(string[i]));
  23.         }
  24.     }
  25.             printf("\n");
  26. }
  27.  
  28. int
  29. main(int argc, char *argv[])
  30. {
  31. int c; //Switch Controller
  32. int o = -2; //Used to check if no option was provided, will print help message
  33. char string[100];
  34.  
  35.     while ( (c = getopt (argc, argv, ":hul")) != -1)
  36.     {
  37.         switch (c)
  38.         {
  39.             case 'h':
  40.                 helpMessage();
  41.             break;
  42.             case 'l':
  43.                 gets(string);
  44.                 stringMod('l', string);
  45.             break;
  46.             case 'u':
  47.                 gets(string);
  48.                 stringMod('u', string);
  49.             break;
  50.  
  51.         }
  52.         o = 0;
  53.     }
  54.     if (o == -2){
  55.         helpMessage(); //prints help message if no option was chosen
  56.     }
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement