characterCases.cpp

{EAV_BLOG_VER:a6b6a0065a96b431}

The Problem
Think CS C++
7.14

As an exercise, character classification and conversion library to write functions named apstringToUpper and apstringToLower that take a single apstring as a parameter, and that modify the string by converting all the letters to upper or lower case. The return type should be void.

The Code

/* For my benefit, given that my compiler does not have apstring.cpp,
 I will be using an array of characters. */

#include <iostream>
#include <ctype.h>

using namespace std;

int index;

void stringToUpper(string s){
 index = 0;
 while (index <= s.length()){
 if (isalpha(s[index])){
 char l = toupper(s[index]);
 cout<<l;
 //cout<<toupper(s[index]);
 }
 else {
 cout<<s[index];
 }
 index++;
 }
 cout<<"\n";
}

void stringToLower(string s){
 index = 0;
 while (index <= s.length()){
 if (isalpha(s[index])){
 char l = tolower(s[index]);
 cout<<l;
 //cout<<tolower(s[index]);
 }
 else {
 cout<<s[index];
 }
 index++;
 }
 cout<<"\n";
}

int main()
{
 // Test cases
 string string1 = "somewhere";
 string string2 = "SOMETIME";
 string string3 = "someHOW386";

 stringToUpper(string1);
 stringToUpper(string2);
 stringToUpper(string3);

 stringToLower(string1);
 stringToLower(string2);
 stringToLower(string3);

 return 0;
}

/* OK, I used string. When I tried using arrays, the compiler gives
 me an error which I do not yet understand. */

Advertisement
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.