I need to create a program for my friend that accepts a string from the user (not from the command line) and displays:
-The string using upper-case letters, e.g. all lower-case letters are converted to upper-case letters
-The string using lower-case letters, e.g. all upper-case letters are converted to lower-case letter.
-The string where the first letter of each word is upper-case and all other letters in the word are lower-case.
-The string in "camel case", i.e. the first letter in the string is lower case, followed by alternating upper and lower case letters.
-And that's it :P
Here’s what I have so far, I'm completely lost now :*( if anyone could help me finish this I'd greatly appreciate it. )) also I don't want to use string commands, too easy :P
Code:
void toUpper(char *str);
void toFirstCap(char *str);
void toCamelCase(char *str);
int main(int argc, char **argv)
{
char sentence[256];
char ch; int i = 0;
printf("Enter a string: ");
gets(sentence);
printf("\n%s\n",sentence);
toUpper(sentence);
printf("\nUpper case: %s\n",sentence);
toFirstCap(sentence);
printf("\nFirstCap case: %s\n",sentence);
toFirstCapSmartWay(sentence);
printf("\nFirstCap case: %s\n",sentence);
system("pause");
}
void toUpper(char *str)
{
while(*str != '\0')
{
if(*str >= 'a' && *str <= 'z')
/* CHECK whether the current character is a lower case alphabet */
{ *str -= 'd' - 'D';
} str++; }
}
void chngCaseSingleCharacter(char *str, int cAse)
{
cAse = 1 to covert into lower
cAse = 2 to covert into upper */
if(cAse == 2)
{ if(*str >= 'a' && *str <= 'z')
/* CHECK whether the current character is a lower case alphabet */
{
*str -= 'd' - 'D'; /* OR *str = *str - 32;}
}
else if(cAse == 1)
{ if(*str >= 'A' && *str <= 'Z')
/* CHECK whether the current character is an upper case alphabet */
{
*str += 'd' - 'D'; /* OR *str = *str + 32; WHY? WHAT?*/ }
}
}
void toFirstCap(char *str)
{
char b;
while( (b = *str++) != '\0')
{ if(b == ' ' || b == '\t')
if(*str >= 'a' && *str <= 'z')
{ *str -= 'd' - 'D'; }
} else
{
if(*str >= 'A' && *str <= 'Z')
{
*str += 'd' - 'D'; }
}
}
}
void toFirstCapSmartWay(char *str)
{
char b; while( (b = *str++) != '\0')
{ if(b == ' ' || b == '\t') {
chngCaseSingleCharacter(str, 2);
}
else {
chngCaseSingleCharacter(str, 1);
}
}
}
void toCamelCase(char *str)
{ /* char last = 'A'; // or some upper case letter; this serves as a
// tracker to the case of thelast alphabet written read char one by one till end of string
// USINg a loop
{
if(curr char is lower case)
{
if (last is lower case)
{
convert curr to upper case
last = curr
}
//lower case
}
else if(curr char is upper case)
{
.... similar to above
}
read the next character
} */
}