Word Reverser using C-Strings

I'm pretty much done with this assignment for my class.
Although, in my final c-string "test" in which the words of
c-string are supposed to be reversed it removes the first word.
How can i fix this? Why did this happen? Thanks for looking.
I'm also accepting suggestions for making my code more elegant

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//Ronan Sullivan
//C-Strings

#include<iostream>
#include<cstring>

using namespace std;
//Declaration of Prototypes

int lastIndexOf(char *s, char);
void reverse(char *s);
int replace(char *s, char target, char replacementChar);
int findSubstring(char *s, char substring[]);
bool isPalindrome(char *s);
void WordReverser(char *s, int, int);
void reverseWords(char *s);
int main()
{
	const int MAX_SIZE = 300;
	int IndexOf;	

	char s[MAX_SIZE];
	char filler[2];
	char temp;
	char charTarget;
	cout << "Please enter in your string.\n";
	cin.getline(s, MAX_SIZE);
	cout << "\nNext, enter your target character.\n";
	cin >> charTarget;
	cin.getline(filler, 2);
	IndexOf = lastIndexOf(s, charTarget);
	cout << IndexOf << endl;


	cout << "Please enter a string you would like to reverse\n";
	cin.getline(s, MAX_SIZE);
	reverse(s);
	
	cout << "Please enter an array you'd like to replace a letter of.\n"
	cin.getline(s, MAX_SIZE);
	char letter, letter2;
	cout << "\nPlease enter a letter you would like to replace.\n";
	cin>> letter;
	cin.getline(filler, 2);
	cout << "\nPlease enter a letter you would like to place.\n";
	cin>> letter2;
	cin.getline(filler, 2);
	replace(s, letter, letter2);


	
	cout << "\nPlease enter a new string, with a substring you'd like to find.\n";
	cin.getline(s, MAX_SIZE);
	cout << "\nPlease enter a substring which you would like to find.\n";
	char substring[30];
	cin.getline(substring, 30);
	int place;
	place = findSubstring(s, substring);
	cout << "Your substring starts at "<<  place << endl;
	
	cout << "Please enter a string that might be a Palindrome.\n";
	cin.getline(s, MAX_SIZE);
	if(isPalindrome(s))
		cout<< "It's a palindrome!\n";
	else
		cout<<"It's not a palindrome...\n";
	
	cout<< "Please enter a string whose words you would like to reverse.\n";
	cin.getline(s, MAX_SIZE);
	reverseWords(s);
	cout << s;

	
	
		
return 0;
}


int lastIndexOf(char *s, char target)
{
	int counter = 0;
	for (int i = 0; i < strlen(s); ++i)
	{
		if (s[i] == target)
			counter = i;
		
	}
	if (counter == 0)
			return -1;
	return counter;
}

void reverse(char *s)
{
	int temp;
	char temp2;
	temp = (strlen(s) - 1);
	for (int i = 0; i < (strlen(s) / 2.0); ++i)
	{
		temp2 = s[i];
		s[i] = s[temp];
		s[temp] = temp2;
		temp--;
	}cout << s << endl;
}

int replace(char *s, char target, char replacementChar)
{
	int counter;
	for (int i = 0; i < strlen(s); ++i)
	{
		if (s[i] == target)
			s[i] = replacementChar;
		counter++;
	}
	cout<< s;
	return counter;
}

int findSubstring(char *s, char substring[])
{
	int counter = 0;
	int temp;
	int j = 0;
	for (int i = 0; i < strlen(s); ++i)
	{
		if(s[i] == substring[j])
		{
			for (j = 0; j < strlen(substring); ++j)
			{
				cout << s[i+j] << "   " << substring[j] << endl;
				if (s[i + j] == substring[j])
					{
					counter++;
					cout << counter << endl;
					temp = i;
					}			
				if (counter == strlen(substring))
					return i;
			}
		}	
	}
return -1;
}
	

bool isPalindrome(char *s)
{

	int temp = strlen(s) - 1;
	int counter = 0;
	for (int i = 0; i < (strlen(s)); ++i)
	{	if (s[i] == s[temp])
		{
			counter++;
		}
	temp--;
	}
	if (counter == strlen(s))
		return true;
	else
		return false;
}

void reverseWords(char *s)
{
	reverse(s);
	for (int i = 0; i < strlen(s); ++i)
	{	
		for (int j = 0; j < strlen(s) +1; ++j)
		{
			if (s[j] == ' ' || s[j] == '\0')
			{WordReverser(s, i, j);
				i = j + 1;
			}
		}
	}

}

void WordReverser(char *s, int i, int j)
{
	int temp;
	char temp2;
	temp = j;
	
	for (; i < j; ++i)
	{
		temp2 = s[j];
		s[j] = s[i];
		s[i] = temp2;
		j--;
	}
}

Topic archived. No new replies allowed.