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.
If you did not install the Samples directory during installation, go install them now.
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
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:
- Write a function to remove all trailing blanks from the right end of a string. Assume the prototype is
1void trimRight(char a);
- Write a function to remove all leading blanks from the left end of a string. Assume the prototype is
1void trimLeft(char a);
- 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
1void padRight(char a, int n);
- 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
1void padLeft(char a, int n);
- 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
1void center(char a, int n);
- 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
1bool isPalindrome(char a);
- 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
1void truncate(char a, int n);
- 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
1void capitalizeWords(char a);
Previous #CodingResolution posts:
- Downloading and Installing the FREE C++ Builder Community Edition or Delphi Community Edition
- Getting to Know the IDE
- Programming Language History
- Build Your First Windows App
- Build Your First Mobile App
- Variables and Variable Scope
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Deep Dive Webinar: Boost C++Builder Compile Speed with TwineCompile