Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reversing content of a TEdit

Posted on 2014-10-18
5
Medium Priority
?
296 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

718 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