C++: C-Strings - Programming Exercises 2

Assumptions

Programming Exercises

  1. Write a function to remove all trailing blanks from the right end of a string. Assume the prototype is
        void trimRight(char a[]);
  2. Write a function to remove all leading blanks from the left end of a string. Assume the prototype is
        void trimLeft(char a[]);
  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
        void padRight(char a[], int n);
  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
        void padLeft(char a[], int n);
  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
        void center(char a[], int n);
  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
        bool isPalindrome(char a[]);
  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
        void truncate(char a[], int n);
  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
        void capitalizeWords(char a[]);
  9. Write a function, capitalize, which changes the first letter of the argument string to uppercase. You can use the library toupper function which works on one char. Note that the toupper function returns a value without changing its parameter. Assume the prototype is
        void capitalize(char a[]);
  10. Write a function, isAlpha, which returns true if all characters in the parameter are alphabetic (ie, if there are no non-alphabetic characters), and false otherwise. You can use the library isalpha function to test the individual characters. If there are no characters in the argument, this function should succeed. Assume the prototype is
        bool isAlpha(char a[]);
  11. Write a function, reverse, which reverses the characters in the argument. Assume the prototype is
        void reverse(char a[]);
  12. Write a function, count, which counts the occurrences in the first argument, a, of the single character which is the second argument, c. Assume the prototype is
        int count(char a[], char c);
    For example,
        i = count("abracadabra", 'a');
    would return 5.
  13. Write a function, count, which counts the occurrences in the first argument, a, of the characters in the second argument, b. Assume the prototype is
        int count(char a[], char b[]);
    For example,
        i = count("abracadabra", "bax");
    would return 7.
  14. Write a function, deleteChar, which deletes any occurrence in the first argument, a, of the single character which is the second argument, c. Assume the prototype is
    void deleteChar(char a[], char c);