// WE WILL WRITE OUR OWN STRING HELPER METHODS
// DO NOT IMPORT ADDITIONAL LIBRARIES FOR THIS EXERCISE
#include <stdio.h>
#include <cs50.h>
// TODO: update helper prototypes with parameter and return types
int isAlpha(char txt);
int strLen(string str);
char toUpper(char txt);
string toUpperString(string txt);
int main(void)
{
// TODO: write tests for each of your methods as you complete them
printf("output of isAlpha is %i\n", isAlpha('&'));
printf("output of toUpper is %c\n", toUpper('c'));
string str = get_string("test string: ");
printf("length of string is %i\n", strLen(str));
printf("output of toUpperString is %s\n", toUpperString(str));
}
int isAlpha(char txt)
{
// return 1 if char variable named txt is an alpha char
if ((txt >= 'A' && txt <= 'Z') || (txt >= 'a' && txt <= 'z'))
{
return 1;
}
// can think of this as an implied else
return 0;
}
int strLen(string str)
{
// count chars until we find '\0' special char
int len = 0;
while (str[len] != '\0'){
len++;
}
return len;
}
int strLenFor(string str)
{
// count chars until we find '\0' special char
int len = 0;
for (int i = 0; str[i] != '\0'; i++){
len = i;
}
// len has scope within the entire function, i has scope only within the for loop
return len + 1;
}
char toUpper(char txt)
{
// return 1 if char variable named txt is an alpha char
if (txt >= 'a' && txt <= 'z')
{
return txt - ('a' - 'A');
}
// can think of this as an implied else
return txt;
}
string toUpperString(string txt)
{
// declare new string to store updated values
for (int i = 0; i < strLen(txt); i++)
{
// use our new helper method!
txt[i] = toUpper(txt[i]);
}
return txt;
}