0

The goal of my exercise is to produce

The original string is:

silence .is a looking bird:the turning; edge, of life. e. e. cummings

Destination string after swapping: cummings e. e. life. of edge, turning; bird:the looking a .is silence

and what I am getting is:

69The original string is: silence .is a looking bird:the turning; edge, of life. e. e. cummings

Destination string after swapping:

my code: '''

#include<stdio.h>
#include<stdlib.h>
#define MAX_STR_LEN 1024
// DO NOT USE the string library <string.h> for this exercise
void wordSwapper(char *source, char *destination)
{  
    int count = 0;
    while (*(source + count) != '\0')
    {
        count++;
    }
    printf("%d", count);
    for(int i = 0; i < count; i++) 
    {
        *(destination + i) = *(source + (count - i));
    }
}

int main()
{    
    char source[MAX_STR_LEN]="silence .is a looking bird:the turning; edge, of life. e. e. cummings";    
    char destination[MAX_STR_LEN]="I am a destination string and I contain lots of junk 1234517265716572@qsajdkuhasdgsahiehwjauhiuiuhdsj!"; 
    wordSwapper(&source[0], &destination[0]);   
    printf("The original string is: \n%s\n",source);    
    printf("Destination string after swapping: \n%s\n",destination);    
}

'''

2 Answers2

0

The posted code reverse the string - character by character. Two issues:

  • off-by-one, where the terminating NUL character is copied to position 0 of the destination string, therefore the result is empty string. The sec
  • The requirement is to split the string into words, and copy the words in reverse order to the destination string.

Consider the following alternative

void wordSwapper2(char *source, char *destination)
{
    int count = 0;
    while (*(source + count) != '\0')
    {
        count++;
    }

    // Copy words in reverse order
    char *dest = destination ;
    int dest_pos = 0 ;
    // Word End
    int w_end = count ;
    while ( w_end >= 0  ) {
        // Find word start
        int w_start = w_end ;
        while ( w_start > 0 && source[w_start-1] != ' '  ) w_start-- ;
        // Copy word
        for (int i=w_start ; i<w_end ; i++ ) *dest++ = source[i] ;
        // Add space if not first word
        if ( w_start > 0 ) *dest++ = ' ' ;
        // Move to previous word (skip over space)
        w_end = w_start-1 ;
    } ;
    // Terminating NUL
    *dest++ = '\0' ;
}
dash-o
  • 13,723
  • 1
  • 10
  • 37
0

My variant:

void wordSwapper(char *source, char *destination)
{  
    char *start, *end;

    start = source;
    while (*(start++) != '\0')
        destination++;

    // write trailing zero
    *destination = '\0';

    while (*source != '\0')
    {
        // copy spaces
        while (*source == ' ')
            *(--destination) = *(source++);

        // find word bounds
        start = end = source;
        while (*end != '\0' && *end != ' ')
            end++;

        source = end;

        // copy word
        while (end > start)
            *(--destination) = *(--end);
    }
}
Alex Skalozub
  • 2,511
  • 16
  • 15
  • can you please explain what your code is doing im having a hard time understanding – Allp Raplsf Jan 26 '20 at 22:59
  • First it scans source string to the end (incrementing destination pointer) and writes terminating zero. Then in the main loop we first consume all space characters and write them backwards to the `destination` (since order doesn't matter for them). When a non-space character is met, we remember the position in `start` and scan until a next space or zero character is met. After that `start` points to the start of the word and `end` points right after it. We assign `source = end` to continue main loop from that position, and copy word characters backwards from `end - 1` to `start`. – Alex Skalozub Jan 26 '20 at 23:13