Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reversing content of a TEdit

Posted on 2014-10-18
5
Medium Priority
?
308 Views
Last Modified: 2014-10-20
Hi folks, I'm studying C++ and I couldn't find on the net any reference by about how I would go about doing this, so any help would be appreciated.

What I need is to have the data typed into the text edit box  inverted and shown on another box when one pressed a button.

So if one typed on one box "123456" when pressed the button the other box would show "654321".

Thanks.
0
Comment
Question by:Rodric MacOliver
  • 4
5 Comments
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 40389755
Get the content into string and do a reverse of it and then display it on another editbox.
What IDE you are using. I hope you are using Builder XE.
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 40389845
for reversing you can use std::reverse(),
String text = textbox1.text;
std::string str = text.c_str();
std::reverse(str.begin(), str.end());

onclick() event, add the reversed string to text property of textbox2
i.e,
textbox2.text = reversedString;

I don't have Builder XE. So can't give you exact code. But the given code can help you to proceed.
0
 

Author Comment

by:Rodric MacOliver
ID: 40389883
Subrat, I'm using XE6 C++ Builder. I couldn't make it work, I'm getting two errors:

[bcc32 Error] Unit1.cpp(26): E2285 Could not find a match for 'string::basic_string(const string&)'
  Full parser context
    Unit1.cpp(24): parsing: void _fastcall TForm1::Button1Click(TObject *)


and

[bcc32 Error] Unit1.cpp(27): E2316 'reverse' is not a member of 'std'
  Full parser context
    Unit1.cpp(24): parsing: void _fastcall TForm1::Button1Click(TObject *)


Here is my code:
//---------------------------------------------------------------------------

#include <vcl.h>
#include <string>
#include <cstdlib>
#include <sys/types.h>
#include <stdio.h>
#include <cstring>
#pragma hdrstop

#include "Unit1.h"
using namespace std;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	String textVariable = Edit1->Text;
	std::string str = textVariable.c_str();
	String reversedString = std::reverse(str.begin(), str.end());
	Edit2->Text = reversedString;
}
//---------------------------------------------------------------------------

Open in new window

0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 40391265
I will post you the code after 2hrs.
0
 
LVL 9

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 2000 total points
ID: 40391365
1. TEdit->Text returns UnicodeString
2. I don't think there is any inbuild API to reverse the string in UniCodeString class.
3. So we converts UnicodeString to std::wstring/std::string by using _wstr() or c_str().
4. Now we can reverse the string using reverse algorithm. i.e std::reverse(). You need to include "string" and "algorithm" to make the whole thing to work.
5. Now string is reversed and we will convert it to UnicodeString by using wcstombs(). Because TEdit->Text needs UniCodeString.
6. Here I have created separate function ToUniStr() to convert wstring to UniCodeString. This can be reusable where ever needed.

#include "string"
#include "algorithm"


// Convert wstring to UnicodeString
String ToUniStr(const std::wstring &str)
{
   char *cBuf = new char[str.length()+1];
   memset( cBuf, 0, str.length()+1);

   wcstombs(cBuf, str.c_str(), str.length());

   String aBuf = cBuf;
   delete[] cBuf;
   return aBuf;
}


//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
   String str1 = Edit1->Text;
   std::wstring wstr = str1.w_str(); // str1.c_str() is also OK here

   // Reverse the string
   std::reverse(wstr.begin(), wstr.end());

   String str2 =  ToUniStr(wstr);
   Edit2->Text = str2;

}

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question