C++ Programming. Please Help!!!!

Pages: 12
Write a program that accepts a C-string input from a file and reverses the contents of the string. Your program should work by using two pointers. The head pointer should be set to the address of the first character in the string, and the tail pointer should be set to the address of the last character in the string (i.e., the character before the terminating null). The program should swap the characters referenced by these pointers, increment head to point to the next character, decrement "tail" to point to the second-to-last character, and so on, until all characters have been swapped and the entire string is reversed.You are not allowed to use pointer arithmetic.

This is what I did:
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
#include<iostream>  
#include<string>     

using namespace std;  

int main() {
     string str;
     typedef char* CharHead;
     CharHead head; 
     typedef char* CharTail;
     CharTail tail;
     typedef char* CharCstr;  
     CharCstr cstr;
    
     int i = 0;     

     cout << "Enter a string: ";  
     getline(cin, str);  

     cstr = new char[str.size() + 1];  
     strcpy(cstr, str.c_str());  

     head = &cstr[0];  
     tail = &cstr[str.size() - 1];  
     
     cout << "String inverted is: "; 
     
     typedef char* CharTemp;  
     CharTemp temp;
     temp = &cstr[100];

while (head <= tail) // this should be done only halfway of string and not for complete string, as you are swapping them now.  
{  
     temp = cstr; 
     cstr = tail;  
     tail = temp;  
     tail--;  
     head++;  
     i++;  
}  
       
    cout << cstr;  
    cout <<"\n"; 
    system ("PAUSE");
    return 0;  

} 


I get errors:
Input : program
Output: m


How do I fix this? Thanks so much.
Last edited on
closed account (zb0S216C)
On line 33, you're attempting to assign a char to an address. There's two ways to fix this:

1) Assign temp the address of the 100th char in cstr
2) Dereference temp

On line 34, you're attempting to assign an address to a char (tail). To fix this, dereference tail.

Wazzak
Last edited on
I fixed it, but I still get error. What do I have to do to fix it now?
closed account (zb0S216C)
Stephanie I wrote:
cstr[]

This is equivalent to: *(cstr + ??). Remove the brackets.

Wazzak
If I remove the brackets, it just outputs the last letter, it doesn't reverse the whole string.
Please Help!!!! I need this program to work.
closed account (zb0S216C)
1
2
3
4
5
6
temp = cstr; 
     cstr = tail;  
     tail = temp;  
     tail--;  
     head++;  
     i++;  

Here, temp is assigned to the first character of cstr. cstr is then set to point to the address of the object pointed to by tail. tail is then assigned the address of the object pointed to by temp. After this assignment, both tail and temp point to the same location (pointing to the same char).

Wazzak
Last edited on
This is what I did. It still didn't work.
closed account (zb0S216C)
Try simplifying your program. Think about it, you're reversing a string; cstr is a pointer to the start of the string. Make temp point to the end. Then, while temp greater than or equal to the position of cstr, decrement temp. You don't need tail.

Wazzak
I still don't get it.
here:
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
int main()
{
	string str;
     typedef char* CharHead;
     CharHead head; 
     typedef char* CharTail;
     CharTail tail;
     typedef char* CharCstr;  
     CharCstr cstr;   

     cout << "Enter a string: ";  
     getline(cin, str);

     head = &str[0];  
     tail = &str[str.size()];  
     
     cout << "String inverted is: "; 
     
     typedef char* CharTemp;  
     CharTemp temp;
	while ((head!=tail)&&(head!=--tail))
    {
		*temp=*head;
		*head++=*tail;
		*tail=*temp;
	}
       
    cout << str;  
    cout <<"\n";
}

now we can all finaly rest, and it is all working.
but doesn't this use pointer arithmetic?
closed account (zb0S216C)
viliml, you 'avin' a laugh? Don't post solutions. Judging by the way the loop was written, it doesn't even work.

Wazzak
Last edited on
it works on my comp! and, stephanie, yours uses pointer arithetics too!
No it didn't work.
I can't use pointer arithmetic, so how can I do this?
Oh and it needs to read from a file.
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
int main()
{
	string str;

     cout << "Enter a string: ";  
     getline(cin, str);
     int a=0, b=str.size();
     char& head = str[0];  
     char& tail = str[str.size()]; 
     
     cout << "String inverted is: "; 
     
     typedef char CharTemp;
     CharTemp temp;
	while ((&head!=&tail)&&(&head!=(tail=str[--b], &str[b+1])))
    {
		temp=head;
		head=tail;
		tail=temp;
                head=str[++a];
	}
       
    cout << str;  
    cout <<"\n";
}

right now I cant test this, but I think it should work.
It doesn't work
One you are using a c++ string instead of a c-string.

1
2
3
4
5
6
7
8
9
10
11
12
13

char someString[30];  // c-style string.
string strString; // c++ style string.

// init the string to zeros or nulls;
for(int i = 0; i < 30; i++)
{
       someString[i] = (char)0;
}


char *pStartofString = &someString[0];  //points to the first character in the c -style string.
char *pEndofString = &someString[28]; //points to the second to the last character in a c-style string.     


if I wanted to know where the end of a entered string was I would scan the string character by character, either with a simple counter or pointer math, I would search for the first null position from the start of the string. I hope this clears up the issues you are having given the instructions you are given.
Last edited on
Pages: 12