#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

char your_string[256];
char new_string[256];
void ReverseString();

int main()
{
cin.getline(your_string, 256);
ReverseString();
cout << new_string << "\n";
return 0;
}

void ReverseString()
{
int x = strlen(your_string)-1;
for(int y = x; y >= 0; y--)
{
 new_string[x-y] = your_string[y];
}
}

I need the program to ask the user two times a string. Then the function should join the string and return the joined string to main.

Thank you very much!

Recommended Answers

All 6 Replies

Do some research on strcat.

I need a seperate function.
Use two strings provided in main and then the function is called to add the rwo strings.

>>Use two strings provided in main and then the function is called to add the rwo strings.

That's what strcat() does when using C style strings. Do you mean you need to write your own version of strcat() for educational purposes, that is, you are not allowed to used strcat()?

Exactly. I need my own version. Its a function i must create.
Thanks

Then think about the task. Say I give you one word with 4 letters and another word with 3 letters and I want you to create a new word by attaching the 3 letter word to the end of the 4 letter word. How much memory is the new word going to require? Do you know how to assign memory if you don't know how much memory you are going to require until run time? Do you know how to loop through a word looking at each char of the word and how to assign one char to another? If so, you can probably write a function to do the task. There may be other ways, too, but this always seemed the most straightforward way.

Creating your own strcat is nothing, it takes literally a couple of lines.

void my_strcat(char *dest, char *source) {
  // Find null-terminator
  while ( *++dest );

  // Add new string
  while ( *dest++ = *source++ );
}

Making it safe would be the next problem. But think about what Lerner is telling you, you're assuming what the user inputs will be shorter than 128 characters, if you can't use std::strings, try learning how to use dynamic memory, or validating your input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.