Reverse the order of a series of words

Hello! I'm trying to learn pointers and I have this exercise that I'm having trouble with. I'm trying to reverse the order of a series of words using a pointer of pointers
Ex: If my sentence was "This is me", the result would be "me is This"

This is my test function which has a main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  char* r0 = "reverse";
  char* r1 = "me";
  int numStrings = 2;
  char** sentence = (char**)malloc(numStrings * sizeof(char*));
  sentence[0] = r0;
  sentence[1] = r1;
  reverse(sentence, 2);

  if(strcmp(sentence[0], r0) == 0){
      printf("Error! reverse failed\n");
  }
  if(strcmp(sentence[1], r1) == 0){
      printf("Error! reverse failed\n");
  }
    
  free(sentence);
  return 0;
Reverse

This is my reverse function -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void reverse(char** input, int inputSize);

void reverse(char** input, int inputSize) {
  int i = 0;
  int firstIndex = 0;
  int lastIndex = inputSize -1;
  char* temp;

  for(i = 0; i <= (inputSize/2) - 1; i++) {
    *input[i] = temp;
    input[i] = input[lastIndex];
    *input[lastIndex] = temp;
    lastIndex --;
  }
  return;
}


Thanks in advance!
Last edited on
Hello ivar5000,

My rearrangement of your program. Read the comments in the code. Most of the changes were just to get the program to compile.

The line of code I have a question on is line 4. I will have to look that one up, but I am thinking that "malloc" is being used wrong. Especially the part sizeof(char*). This value is likely to be 4 or maybe 8, depends on the computer used. Not what you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//  Little rusty on C code. I think these are the right header files.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reverse(char** input, int inputSize);

int main()
{
	char* r0 = "reverse";
	char* r1 = "me";
	int numStrings = 2;
	char** sentence = (char**)malloc(numStrings * sizeof(char*));
	sentence[0] = r0;  // <--- sentence is a pointer to a pointer. Seams to me it should be sentence[0] = &r0 not the entire string.
	sentence[1] = r1;
	reverse(sentence, 2);

	if (strcmp(sentence[0], r0) == 0)
	{
		printf("Error! reverse failed\n");
	}
	if (strcmp(sentence[1], r1) == 0)
	{
		printf("Error! reverse failed\n");
	}

	free(sentence);
	return 0;
}

void reverse(char** input, int inputSize)
{
	int i = 0;
	int firstIndex = 0;
	int lastIndex = inputSize - 1;
	char* temp{ nullptr };  // <--- temp needs to be set to something.

	for (i = 0; i <= (inputSize / 2) - 1; i++)
	{
		//  These changes allowed the program to compile, but it still didi not work.
		*input[i] = *temp;  // <--- Changed "temp". Since temp" has no value I believe these vaiables are backwards.
		input[i] = input[lastIndex];
		*input[lastIndex] = *temp;  // <--- Changed "temp". Since temp" has no value I believe these vaiables are backwards.
		lastIndex--;
	}
	return;
}


My thoughts for now.

Hope that helps,

Andy
Topic archived. No new replies allowed.