Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------------------------------------------
- // ass-b1.c
- //
- // The program takes a string and interprets it firstly. The first thing which
- // the program does is checks whether the string is either
- // a) Empty
- // b) Too short
- // Should it be either of these two, it returns the values -1 and -2
- // respectively.
- // After that, the program takes the string defined and turns it around
- // with each word being inverted. This is done through the function
- // reversePrint. The output value is a reversed string with each word
- // being reversed separately, as well as starting letters being capitalised
- // and every other letter being lower-case.
- //
- // Group: Group 18, study assistant Kevin Innerebner
- //
- // Authors: Vedad Misirlic 11833774
- //-----------------------------------------------------------------------------
- //
- #include <stdio.h>
- #include <ctype.h>
- //-----------------------------------------------------------------------------
- ///
- /// This is the reversePrint function. It checks whether the values are
- /// okay. If they are, the reversePrint is performed.
- ///
- /// @parameter *name This is the paramater which is given into the program
- /// in order to perform the whole function.
- ///
- /// @return 0 for success, -1 for for empty string, -2 for too short input
- //
- int reversePrint(char *name)
- {
- int begin_counter = 0;
- int end_counter = 0;
- int main_counter = 0;
- int space_counter = 0;
- int return_value = 0;
- int check_done = 0;
- //This part starts the check and goes throughout the whole program.
- // The check is actually performed just once.
- for (check_done = 0; check_done < 1; check_done++) {
- if (*name == 0)
- {
- //This part returns the value -1 and ends the whole loop, skipping the
- //implementation of the reverse action.
- return_value = -1;
- break;
- }
- for (main_counter = 0; (*(name + main_counter)) != 0; main_counter++) {
- if (*(name + main_counter) == 32 && *(name + main_counter + 1) != 0)
- space_counter++;
- }
- //This part returns the value -2 and ends the whole loop, skipping the
- //implementation of the reverse action.
- if (space_counter == 0) {
- return_value = -2;
- break;
- }
- else
- //This part includes and reverses the whole string.
- {
- //We start the for loop buffering until the end of the input
- for (main_counter = 0; (*(name + main_counter)) != 0; main_counter++)
- {
- //Should we find a space, then we print the first word.
- if (*(name + main_counter) == 32)
- {
- //The end counter is defined as the main counter -1, pointing to the
- // end of the word.
- for (end_counter = main_counter - 1; end_counter >= begin_counter; end_counter--)
- {
- //This prints the first letter as uppercase.
- if (end_counter == main_counter - 1)
- {
- printf("%c", toupper(*(name + end_counter)));
- }
- //This prints all other letters as lowercase
- else
- {
- printf("%c", tolower(*(name + end_counter)));
- }
- }
- //adds a space at the end of the word to make space for the next word
- //and sets the begin counter to be equal to the next value of the
- //first next word, which is on the position of the space +1
- printf(" ");
- begin_counter = main_counter + 1;
- }
- //This prints out the last word in the string, as it detects whether
- //it is the null byte.
- if ((*(name + main_counter + 1)) == 0)
- {
- for (end_counter = main_counter; end_counter >= begin_counter; end_counter--)
- {
- if (end_counter == main_counter)
- {
- printf("%c", toupper(*(name + end_counter)));
- } else
- {
- printf("%c", tolower(*(name + end_counter)));
- }
- }
- }
- }
- return_value = 0;
- }
- }
- //returns the value to the function
- return return_value;
- }
- //-----------------------------------------------------------------------------
- ///
- /// This is the main function which calls the reversePrint function.
- ///
- /// @parameter is empty
- ///
- /// @return always 0
- //
- int main() {
- char name[] = "Vedad Misiric";
- reversePrint(name);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement