Thread: how do I change the case of a string?

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

    how do I change the case of a string?

    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 I figured I'd post the entire working code so you can see what’s wrong. I've been trying to get Lower case to work, but every way I use it, it just messes up the rest of them. I want it to display the sentence you entered, then in lowercase, then in uppercase, then in first case, then in camel case. I got the sentence, upper and first case to work. The other two I can’t. 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)
    //put code 4 camel case here//

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Start by INDENTING!!!

    After that, loop through each string and compare each char ch for 'a' <= ch <= 'z', and if it is, subtract 'a' and add 'A' to it.

    That would be the way for upstring.

    Really, though. You need to learn to indent. That is really ugly up there.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by eligilbert12 View Post
    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
    Hopefully your friend tries running it on a mainframe and sees your program fail miserably. Changing case the way you're doing it is not portable.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    All you need to do, is run through the string. Find the spaces and remove those elements of the char array, then just turn the next character ToUpper.
    The keyboard is the standard device used to cause computer errors!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Continued - Camel Case!!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM

Tags for this Thread