Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX_STR_LEN 1024
  4.  
  5. // DO NOT USE the string library <string.h> for this exercise
  6.  
  7. void stringLength(char *str, int *len)
  8. {
  9.   int counter = 0;
  10.   char letter = *(str + counter);
  11.  
  12.   while(letter != '\0')
  13.   {
  14.     counter++;
  15.     letter = *(str+counter);
  16.   }
  17.   *len = counter;
  18. }
  19.  
  20. void wordSwapper(char *source, char *destination)
  21. {
  22.   // This function takes a pointer to a 'source' string (an array
  23.   // of chars, with maximum size MAX_STR_LEN), and
  24.   // makes a new string inside the 'destination' string
  25.   // (which is also passed as a pointer) so that the order
  26.   // of the words in the destination string is the reverse
  27.   // of the order in the source string.
  28.   //
  29.   // e.g. if:
  30.   // The 'surce' string contains "Hello I Am A String!"
  31.   //
  32.   // then the destination string will contain
  33.   //
  34.   // 'String! A Am I Hello"
  35.   // (notice that we add a space between each word in the
  36.   // destination string - these are *added*, not *copied*)
  37.   //
  38.   // The only character to be used in separating words is
  39.   // a blank space, so any other characters are considered
  40.   // part of a word, for example "Hello, I; Am! A. *String*"
  41.   // will becoe "*String* A. Am! I; Hello,"
  42.   //
  43.   // You *MUST* use pointers and pointer addressing to complete
  44.   // this exercise (that is, no array notation with brackets []),
  45.   // and you can not assume the 'destination' string is empty,
  46.   // it could (and likely does) contain junk.
  47.   //
  48.   // If you decide to write helper functions, make sure they
  49.   // are placed *above* this function's code.
  50.  
  51.   // TO DO: Complete this function
  52.   destination[0] = '\0';
  53.  
  54.   int len1 = 0;
  55.   stringLength(source, &len1);
  56.   printf("Source is %d characters long\n",len1);
  57.  
  58.   for (int i=len1-1; i>=0; i--){
  59.     if(source[i] == ' '){
  60.       source[i] = '\0';
  61.  
  62.       printf("%s ", &(source[i]) + 1);
  63.       //destination[] = &(source[i]) + 1;
  64.     }
  65.   }
  66.   printf("%s \n", source);
  67.   //destination[] = source;
  68.  
  69. }
  70.  
  71. #ifndef __TESTING
  72. int main()
  73. {
  74.     char source[MAX_STR_LEN]="silence .is a looking bird:the turning; edge, of life. e. e. cummings";
  75.     char destination[MAX_STR_LEN]="I am a destination string and I contain lots of junk 1234517265716572@qsajdkuhasdgsahiehwjauhiuiuhdsj!";
  76.  
  77.     printf("The original string is: \n%s\n",source);
  78.  
  79.     // Call word swapper with the address of the first character in 'source' and the first character in 'destination'
  80.     wordSwapper(&source[0],&destination[0]);
  81.     // You could also call wordSwapper like this: wordSwapper(source,destination) since, as we will have seen in
  82.     // lecture this week, the array's name can be used to pass into a function the address of the first entry
  83.     // in the array.
  84.  
  85.     printf("Destination string after swapping: \n%s\n",destination);
  86. }
  87. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement