Solved

Reversing content of a TEdit

Posted on 2014-10-18
5
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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