Solved

Reversing content of a TEdit

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

734 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