Reputation:
In C++, I need to:
main()
, it displays that original array with the newly reversed characters.I'm having trouble creating the function that actually does the reversing because I have some restrictions:
My function is only passing in the original array ie:
void reverse(char word[])
EDIT: Here's my code base so far:
void reverse(char word[]);
void main()
{
char word[MAX_SIZE];
cout << endl << "Enter a word : ";
cin >> word;
cout << "You entered the word " << word << endl;
reverse(word);
cout << "The word in reverse order is " << word << endl;
}
void reverse(char myword[])
{
int i, temp;
j--;
for(i=0;i<(j/2);i++)
{
temp = myword[i];
myword[i] = myword[j];
myword[j] = temp;
j--;
}
}
Upvotes: 5
Views: 40646
Reputation: 390
I can offer the solution of your problem, using the algorithm Depth first search.
#include <iostream>
#include <vector>
void showContentVector(std::vector<int>& input)
{
for(int i=0; i<input.size(); ++i)
{
std::cout<<input[i]<<", ";
}
return;
}
void dfs(int current, int previous, std::vector<int>& input, std::vector<int>& visited)
{
if(visited[current]==1)
{
return;
}
visited[current]=1;
int item=input[current];
for(int next=(current+1); next<input.size(); ++next)
{
if(next==previous)
{
continue;
}
dfs(next, current, input, visited);
}
if(current==input.size()-1)
{
input.clear();
}
input.push_back(item);
return;
}
void solve()
{
const int maximumSize=5;
std::vector<int> values={1, 2, 3, 4, 5};
std::vector<int> visited(maximumSize, 0);
std::cout<<"Before, values <- ";
showContentVector(values);
dfs(0, -1, values, visited);
std::cout<<std::endl<<"After, values <- ";
showContentVector(values);
std::cout<<std::endl;
return;
}
int main()
{
solve();
return 0;
}
Here is the result:
Before, values <- 1, 2, 3, 4, 5,
After, values <- 5, 4, 3, 2, 1,
Upvotes: 0
Reputation: 1
//this program helps you to find maximum and minimum number in an array
#include<iostream.h>
#include<conio.h>
int main()
{int max;
int a[5]={12,1,0,4,5};
int min=a[0];
int i=1;
max=a[0];
while(i<=4)
{
if (max < a[i])
max=a[i];
if (min > a[i])
min=a[i];
i++;
}
cout << min <<"\n";
cout << max;
getch();
return 0;
}
Upvotes: -2
Reputation: 992707
Since this is homework, I'll point you toward a solution without just giving you the answer.
Your reverse
function can modify the word
that is passed in. One thing you'll need to know is how long the word is (so you'll know how many letters to reverse), you can get this from the strlen()
function. If you're not permitted to use pointers, then you can use a local int
index variable.
Upvotes: 3
Reputation: 45104
If we are talking C-Strings, then your function should be
void reverse(char word[],size_t wordlen)
First answer from the same question (this is a dupe from Reverse a sentence in C?)
This doesn't do what you are looking for, but gets you quite close!
int ReverseString(char *rev)
{
if(*rev!='\0')
{
ReverseString(rev + 1);
putchar(*rev);//change this.
}
return 1;
}
Credits to @devinb.
Upvotes: -1
Reputation: 4174
Despite this looking quite homeworky, may I suggest:
void reverse(char word[])
{
int len=strlen(word);
char temp;
for (int i=0;i<len/2;i++)
{
temp=word[i];
word[i]=word[len-i-1];
word[len-i-1]=temp;
}
}
or, better yet, the classic XOR implementation:
void reverse(char word[])
{
int len=strlen(word);
for (int i=0;i<len/2;i++)
{
word[i]^=word[len-i-1];
word[len-i-1]^=word[i];
word[i]^=word[len-i-1];
}
}
Upvotes: 10