SHARE
TWEET

Untitled

a guest Dec 8th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //-----------------------------------------------------------------------------
  2. // ass-b1.c
  3. //
  4. // The program takes a string and interprets it firstly. The first thing which
  5. // the program does is checks whether the string is either
  6. // a) Empty
  7. // b) Too short
  8. // Should it be either of these two, it returns the values -1 and -2
  9. // respectively.
  10. // After that, the program takes the string defined and turns it around
  11. // with each word being inverted. This is done through the function
  12. // reversePrint. The output value is a reversed string with each word
  13. // being reversed separately, as well as starting letters being capitalised
  14. // and every other letter being lower-case.
  15. //
  16. // Group: Group 18, study assistant Kevin Innerebner
  17. //
  18. // Authors: Vedad Misirlic 11833774
  19. //-----------------------------------------------------------------------------
  20. //
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. //-----------------------------------------------------------------------------
  25. ///
  26. /// This is the reversePrint function. It checks whether the values are
  27. /// okay. If they are, the reversePrint is performed.
  28. ///
  29. /// @parameter *name  This is the paramater which is given into the program
  30. /// in order to perform the whole function.
  31. ///
  32. /// @return 0 for success, -1 for for empty string, -2 for too short input
  33. //
  34.  
  35. int reversePrint(char *name)
  36. {
  37.   int begin_counter = 0;
  38.   int end_counter = 0;
  39.   int main_counter = 0;
  40.   int space_counter = 0;
  41.   int return_value = 0;
  42.   int check_done = 0;
  43.  
  44.   //This part starts the check and goes throughout the whole program.
  45.   // The check is actually performed just once.
  46.   for (check_done = 0; check_done < 1; check_done++) {
  47.     if (*name == 0)
  48.     {
  49.       //This part returns the value -1 and ends the whole loop, skipping the
  50.       //implementation of the reverse action.
  51.       return_value = -1;
  52.       break;
  53.     }
  54.     for (main_counter = 0; (*(name + main_counter)) != 0; main_counter++) {
  55.       if (*(name + main_counter) == 32 && *(name + main_counter + 1) != 0)
  56.         space_counter++;
  57.     }
  58.     //This part returns the value -2 and ends the whole loop, skipping the
  59.     //implementation of the reverse action.
  60.     if (space_counter == 0) {
  61.       return_value = -2;
  62.       break;
  63.     }
  64.     else
  65.       //This part includes and reverses the whole string.
  66.       {
  67.       //We start the for loop buffering until the end of the input
  68.       for (main_counter = 0; (*(name + main_counter)) != 0; main_counter++)
  69.       {
  70.         //Should we find a space, then we print the first word.
  71.         if (*(name + main_counter) == 32)
  72.         {
  73.           //The end counter is defined as the main counter -1, pointing to the
  74.           // end of the word.
  75.           for (end_counter = main_counter - 1; end_counter >= begin_counter; end_counter--)
  76.           {
  77.             //This prints the first letter as uppercase.
  78.             if (end_counter == main_counter - 1)
  79.             {
  80.               printf("%c", toupper(*(name + end_counter)));
  81.             }
  82.             //This prints all other letters as lowercase
  83.             else
  84.               {
  85.               printf("%c", tolower(*(name + end_counter)));
  86.               }
  87.           }
  88.           //adds a space at the end of the word to make space for the next word
  89.           //and sets the begin counter to be equal to the next value of the
  90.           //first next word, which is on the position of the space +1
  91.           printf(" ");
  92.           begin_counter = main_counter + 1;
  93.         }
  94.         //This prints out the last word in the string, as it detects whether
  95.         //it is the null byte.
  96.         if ((*(name + main_counter + 1)) == 0)
  97.         {
  98.           for (end_counter = main_counter; end_counter >= begin_counter; end_counter--)
  99.           {
  100.             if (end_counter == main_counter)
  101.             {
  102.               printf("%c", toupper(*(name + end_counter)));
  103.             } else
  104.               {
  105.               printf("%c", tolower(*(name + end_counter)));
  106.             }
  107.           }
  108.         }
  109.       }
  110.       return_value = 0;
  111.     }
  112.   }
  113.   //returns the value to the function
  114.   return return_value;
  115. }
  116.  
  117. //-----------------------------------------------------------------------------
  118. ///
  119. /// This is the main function which calls the reversePrint function.
  120. ///
  121. /// @parameter is empty
  122. ///
  123. /// @return always 0
  124. //
  125. int main() {
  126.   char name[] = "Vedad Misiric";
  127.   reversePrint(name);
  128.   return 0;
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top