Solved

Reversing content of a TEdit

Posted on 2014-10-18
5
261 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 8

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 8

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 8

Expert Comment

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now