Please Help With Changing Case Program

This is a discussion on Please Help With Changing Case Program within the C++ Programming forums, part of the General Programming Boards category; I need to create a program for my friend that accepts a string from the user (not from the command ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Please Help With Changing Case Program

    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
    } */
    }

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Here’s what I have so far, I'm completely lost now
    A quick glance at the code: everything you have so far works, right? So you just need some direction for toCamelCase?

    Pseudo code:
    Code:
    int current_case = 2;
    
    while(current_char) {
         if (current_char is a letter) {
              chngCaseSingleLetter(current_char, current_case);
              [change current case to 2 if it's 1, or to 1 if it's already 2]
         }
    }
    Note: I'd suggest using 0 for lower case and (not 0) for upper case, then you can write the current_case changing code like this:
    Code:
    current_code = 1;
    current_code = !current_code; //now current_code = 0
    current_code = !current_code; //now current_code != 0
    Code:
    *str += 'd' - 'D'; /* OR *str = *str + 32; WHY? WHAT?*/
    'd' - 'D' = 32. It's just a simple substitution. But 'd' - 'D' is probably better in this case - both are handled by the preprocessor, and 'd' - 'D' better explains what you're trying to do.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    I figured I'd post the entire working code so you can see whats wrong. I've been trying to get "toLower" to work, but every way i use it, it just messes up the rest of them. I want it to display the sentence, then lowercase, then upper, then first case, then camel. i got the sentence, upper and first case to work. the other two i cant. please see if you can fix it, i feel retarded lol....


    Code:
    #include <stdio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    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("\nYour Sentence:%s\n",sentence);
    toUpper(sentence);
    
    printf("\nUpper case: %s\n",sentence);
    toFirstCap(sentence);
    
    printf("\nFirstCap case: %s\n",sentence);
    toFirstCap(sentence);
    
    system("pause");
    }
    
    
    void toUpper(char *str)
    {
    while(*str != '\0')
    {
    if(*str >= 'a' && *str <= 'z')
    { *str -= 'd' - 'D';
    } str++; }
    }
    
    
    void chngCaseSingleCharacter(char *str, int cAse)
    {
    if(cAse == 2)
    { 
    if(*str >= 'a' && *str <= 'z')
    {
    *str -= 'd' - 'D';
    }
    else if(cAse == 1)
    {
    if(*str >= 'A' && *str <= 'Z')
    { 
    *str += 'd' - 'D';
    }}}}
    
    
    
    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)
    //{
    //{ 
    //if(curr char is lower case)
    //{
    //if (last is lower case)
    //{
    //convert curr to upper case
    //last = curr
    //}
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  3. when i want to load the program it gives me an error?
    By wise_ron in forum C Programming
    Replies: 6
    Last Post: 05-05-2006, 04:49 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21