Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++

#CodingResolutions: Strings and the RegExpDemo Sample App

Author: Rebekah D

In this installment of the #CodingResolutions series, let’s take a look at Strings and how to manipulate them. While today still has a fair amount of reading, we’ll finish off by playing with a sample application that demonstrates String validation. 

Prerequisites: 

You should already have downloaded and installed either the RAD Studio 30 Day FREE Trial, Delphi Community Edition, or C++ Builder Community Edition.

If you did not install the Samples directory during installation, go install them now. 

Need help installing the product? Read the documentation or ask for assistance in the forums.

What Is a String Anyway?

“Hello, World!” is an example of a string. 

Learn how C++ strings are used and declared in this article: https://embt.co/IntroductionToString

Learn about #Delphi String types with https://embt.co/DelphiStringTypes

Let’s Practice

The RegExpDemo sample is available for both C++ and Delphi and is included in the Samples directory installed with your IDE. 

This application allows the user to test the validity of a string with regards to a regular expression. 

The form contains a list box, a memo, an edit box, and a button. From the list box the user can choose to validate an e-mail address, an IP address, or a date in either of two formats. When one of the options is selected from the TListBox, the regular expression that is used for validation is displayed in the TMemo component. Next, the user can enter a string in the TEdit and click Evaluate to see whether the input is valid according to the regular expression.

Learn more about the RegExpDemo sample here:  https://embt.co/RTLRegExpressionVCL

More String Exercises

Write your own functions that manipulate strings.  Here’s a list of ideas from http://www.fredosaurus.com/notes-cpp/strings/string-exercises2.html: 

  1. Write a function to remove all trailing blanks from the right end of a string. Assume the prototype is
  2. Write a function to remove all leading blanks from the left end of a string. Assume the prototype is
  3. Write a function to add extra blanks to the right end of a string to make it at least length n. If the string is already n characters or longer, do not change the string. Assume the prototype is
  4. Write a function to add extra blanks to the left end of a string to make it at least length n. If the string is already n characters or longer, do not change the string. Assume the prototype is
  5. Write a function to center a string by adding blanks to the front and back. If the string is already n characters or longer, do not change the string. If an odd number of blanks have to be added, put the extra blank on the right. You can use the functions in the previous problems (padRight and padLeft) to solve this problem. Assume the prototype is
  6. Write a function which returns true if the string parameter is a palindrome. A palindrome is any “word” which is the same forward and backward, eg, “radar”, “noon”, “20011002”, … The function should return false if the argument is not a palindrome. Assume the prototype is
  7. Write a function which shortens a string to n characters. If the string is already shorter than n, the function should not change the string. Assume the prototype is
  8. Write a function which capitalizes the first letter in every word. Assume the first letter is any letter at the beginning or preceded by a blank. All other letters should be turned into lowercase. You can use the library toupper and tolower functions. Assume the prototype is

Previous #CodingResolution posts: 

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES