pointers and strings

So I'm writing a program that asks for a string, then assigns pointers to the head and tail, then reverses them and goes through the line until your string is reversed then outputs the reversed string. I've more or less got it however during the while statement I'm getting error 2440 cannot convert from char * to character. Essentially my head and tail pointer won't store their character in my non pointer "temp" character variable.
Help!
Thanks.
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

#include <iostream>
#include <cstdlib>
#include <cstring>

int main()
{
  char myStr[80];
  char temp;
  char *head = NULL, *tail = NULL;

  std::cout << "Enter a string less than 80 characters long." << std::endl;
  std::cin.getline(myStr,80);
  
  head = &myStr[0];
  tail = &myStr[strlen(myStr) - 1];


  while ( tail >= head, ++head, --tail)
  {
     tail = temp;
	 head = tail;
	 temp = head;
  } 

  std::cout << "Your string reversed is :" << myStr << std::endl;

  return 0;

} 
1
2
3
	 tail = temp;
	 head = tail;
	 temp = head;


There are 3 things wrong with this.

#1 is that all of your assignments are backwards. Remember that the var on the left side of the = is what gets changed.

#2 is that temp is a char while tail is a char*. These are two different types (one is a character and one is a pointer) and therefore you cannot directly assign them like this.

You need to "dereference" the pointer which gives you the value that the pointer points to. You can do this with the * operator:

temp = *tail;

This tells the compiler "I want to assign whatever tail points to into temp". Vs. what you were doing, which was trying to assign tail into temp.


#3 is this line:

head = tail;

While this is syntactically legal, it is not what you want to do, for the same reason as #2. Here you are just changing the pointer, not the data that the pointer points to.
Last edited on
closed account (Dy7SLyTq)
temp needs a starting value and your while loop is weird
This code

1
2
  head = &myStr[0];
  tail = &myStr[strlen(myStr) - 1];


is already wrong. myStr can be an empty string. So its length will be equal to 0. In this case expression strlen(myStr) - 1 will give you an incorrect result.
It is much better when the initial value of tail points out after the last element of a sequence that is in your case it will point out the terminating zero. So it would be much better to write

1
2
  head = myStr;
  tail = myStr + strlen( myStr );



This loop

while ( tail >= head, ++head, --tail)

will be executed until --tail will be equal to 0.

Also the body of the loop is incorrect. You are trying to assign an object of type char to a pointer.

1
2
3
     tail = temp;
	 head = tail;
	 temp = head;


I would rewrite the code the following way

1
2
3
4
5
6
7
8
for ( char *head = myStr, *tail = myStr + std::strlen( myStr );
       head != tail && head != --tail;
       ++head )
{
    char tmp = *head;
    *head = *tail;
    *tail = tmp;
}
I took a look at your code and screamed.
This works.
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
#include <iostream>
#include <cstdlib>
#include <cstring>

int main()
{
  char myStr[80];
  char newStr[80];
  
  char *head = NULL, *tail = NULL;

  std::cout << "Enter a string less than 80 characters long." << std::endl;
  std::cin.getline(myStr,80);
  
  head = &myStr[0];
  tail = &myStr[strlen(myStr) - 1];

  int i=0;
  while ( tail >= head) {
     newStr[i] = *tail;
	 i++;
	 tail--;
  } 
  newStr[i] = '\0';
  std::cout << "Your string reversed is :" << newStr << std::endl;
  
  return 0;

} 


Good luck.
Good looking out, everyone! I'm going to apply some of this advice and report back!
@Manga


It would be good if before to suggest some code you test it. Try your code for example for string "12" and see what you will get.

#include <iostream>
#include <cstdlib>
#include <cstring>

int main()
{
char myStr[80];
char temp;
char *head = NULL, *tail = NULL;

std::cout << "Enter a string less than 80 characters long." << std::endl;
std::cin.getline(myStr,80);



head = myStr;
tail = myStr + strlen( myStr );


for ( char *head = myStr, *tail = myStr + std::strlen( myStr );
head != tail && head != --tail;
++head )
{
temp = *tail;
*tail = *head;
*head = temp;
}

std::cout << "Your string reversed is :" << myStr << std::endl;

return 0;

}

So this actually works and is an amalgamation of everyone's input. I've tried altering the for statement to make it a while and mostly goofed it. Can you guys think of a way to make this while statement function?
Thanks again for all the helpful advice!
As long as we're posting other solutions...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string myStr;

    std::cout << "Enter a string" << std::endl;
    std::getline(std::cin,myStr);

    std::reverse(myStr.begin(),myStr.end());
    std::cout << "Your string reversed is :" << myStr << std::endl;

    return 0;
} 
while ( *head <= *tail)
{
temp = *tail;
*tail = *head;
*head = temp;
++*head;
--*tail;
}
This is the best functioning one that i have. It doesn't function.
it outputs the string but doesn't reverse it and then outputs the unreversed string.
while ( *head <= *tail, ++*head && --*tail)
{
temp = *tail;
*tail = *head;
*head = temp;

}
Or this things which outputs the output message but no string.
In this code you may remove some lines

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
#include <iostream>
// #include <cstdlib> remove this line
 #include <cstring>

 int main()
 {
 char myStr[80];
// char temp; remove this line
// char *head = NULL, *tail = NULL; remove this line

 std::cout << "Enter a string less than 80 characters long." << std::endl;
 std::cin.getline(myStr,80);



 //head = myStr; remove this line
 //tail = myStr + strlen( myStr ); remove this line


 for ( char *head = myStr, *tail = myStr + std::strlen( myStr );
 head != tail && head != --tail;
 ++head )
 {
 temp = *tail;
 *tail = *head;
 *head = temp;
 } 

 std::cout << "Your string reversed is :" << myStr << std::endl;

 return 0;

 }


If you want to substitute the for loop for a while loop then the code can look the following way

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
#include <iostream>
 #include <cstring>

 int main()
 {
 char myStr[80];

 std::cout << "Enter a string less than 80 characters long." << std::endl;
 std::cin.getline(myStr,80);



 char *head = myStr;
 char *tail = myStr + strlen( myStr );


 while ( head != tail && head != --tail )
 {
 temp = *tail;
 *tail = *head;
 *head++ = temp;
 } 

 std::cout << "Your string reversed is :" << myStr << std::endl;

 return 0;

 }

Thanks a million, vlad!
Topic archived. No new replies allowed.