Hi everyone,

So I am writing this program that is supposed to do the following:

Create a program that accepts a string from the user (not from the command line) and displays:

1. The string using upper-case letters, e.g. all lower-case letters are converted to upper-case letters
2. The string using lower-case letters, e.g. all upper-case letters are converted to lower-case letters
3. The string where the first letter of each word is upper-case and all other letters in the word are lower-case.
4. The string in "camel case", i.e. the first letter in the string is lower case, followed by alternating upper and lower case letters

I have done everything but number 4, the first letter in the string is lower case, followed by alternating upper and lower case letters.
I could really use some help. Any tips, hints, or help is much appreciated. Thank you.

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define N 128




   int main()

      {

      char input[N];
      char c,d,j,e;
      char *ptr = input;

      int x,z,y;
 
       x=0;
       z=0;
       y=0;
       
       printf("Enter a string: ");
       gets(input);

       /*Changes user input to all lower case*/
       do
       {
                   d = input[z];
                   d = tolower(d);
                   input[z] = d;
                   z++;
       }
       
       while(d); 
       
       printf("Lower case: ");
       puts(input);
       
       /*Changes user input to all upper case*/
       do
       {
                    c = input[x];
                    c = toupper(c);
                    input[x] = c;
                    x++;
       }
       while(c);
       
       printf("Upper case: ");
       puts(input);
       
       /*Capitalizes first letter of each word*/
        do
       {
                   e = input[y];
                   e = tolower(e);
                   input[y] = e;
                   y++;
       }
       
       while(e); 


    
{
    size_t j = 0;

    /* Strip the optional newline */
    input[strcspn ( input, "\n" )] = '\0';
    
    for ( ; ; ) 
    {
      /* Ignore spans of whitespace */
      while ( input[j] != '\0' && isspace ( input[j] ) )
        ++j;

      if ( input[j] == '\0' )
        break;

      input[j] = (char)toupper ( input[j] );

      /* Ignore spans of non-whitespace */
      while ( input[j] != '\0' && !isspace (input[j] ) )
        ++j;
    }

    printf ( "FirstCap case: ");
    puts(input);

  
}   

    printf("Camel case: ");
    puts(input);
       
       system("pause");
       return(0);

}

Recommended Answers

All 11 Replies

In a loop just use the mod operator to switch between upper and lower case

int main()
{
    char str[] = "Hello World";
    int i;
    for(i = 0; str[i] != 0; i++)
    {
        if( (i % 2) == 0)
            str[i] = tolower(str[i]);
        else
            str[i] = toupper(str[i]);
    }
    printf("%s\n", str);
}
commented: Thanks for your kind support. This thread is driving me nuts! +4

Here are just some "tidy up" suggestions:

1. Your j (of type char) and ptr variables are never used.

2. Rather than declaring separate variables to store each character as you read the characters of the input string for each translation, just use one. The same goes for the variables used as array subscripts. You just need to reset the subscript to 0 before moving on to the next translation.

/*Changes user input to all upper case*/
    pos = 0;
    do {
        c = input[pos];
        c = toupper(c);
        input[pos] = c;
        pos++;
    } while (c);

    printf("Upper case: %s\n", input);

    /*Capitalizes first letter of each word*/
    pos = 0;
    do {
        c = input[pos];
        c = tolower(c);
        input[pos] = c;
        pos++;
    } while (c);

3. The gets() function is dangerous - use fgets() instead.

4. You can print your output for each translation using one printf statement:

printf("Upper case: %s\n", input);

Cheers,
JD

commented: very good suggestions :) +36

So when the camelcase is printed, it counts the spaces as characters. How do i get it to ignore the whitespace?

So when the camelcase is printed, it counts the spaces as characters. How do i get it to ignore the whitespace?

Some minor tweaks to Ancient Dragon's code snippet should do it. You need to detect the start of a word and calculate the modulus based on the offset into the word - like so:

#include <stdio.h>
#include <ctype.h>

int main() {

    char str[] = "Twenty First Century Breakdown - Green Day";
    int i, wordoffset = 0;

    for (i = 0; str[i] != 0; ++i, ++wordoffset) {

        if (str[i] == ' ') {
            wordoffset = -1;
            continue;
        }

        if ((wordoffset % 2) == 0)
            str[i] = tolower(str[i]);
        else
            str[i] = toupper(str[i]);
    }

    printf("%s\n", str);
    return 0;
}

There are probably better ways of doing it - but that's the way my brain is wired this evening.;)

Thank you so much for you help!

The camel case isn't quite coming out right. I've tried to fiddle around with it to make it come out but I can't figure it out.

Right now it comes out as:
Camel case: tHiS iS a tEsT oF sTrInGs

I would like it to come out as:
Camel case: tHiS iS a TeSt Of StRiNgs

Replace wordoffset = -1; with wordoffset--; . :P

Thank you so much for you help!

The camel case isn't quite coming out right. I've tried to fiddle around with it to make it come out but I can't figure it out.

Right now it comes out as:
Camel case: tHiS iS a tEsT oF sTrInGs

I would like it to come out as:
Camel case: tHiS iS a TeSt Of StRiNgs

Well looking at your so called expected output - nothing makes sense. Do you want alternate lower-upper casing based on the position of the character in the string (including spaces) or do you want the first letter of each word to begin with a lowercase letter? If it's the former, you have wasted my time and AD's as well, as he provided you with a solution on his first response.

Going on the expected output in your last post - it makes no sense - it doesn't match any of the proposed outputs from the above two alternatives. What are you actually looking for? Your expected output makes NO SENSE unless you have an algorithm in mind that you haven't communicated appropriately in your thread.

Replace wordoffset = -1; with wordoffset--; . :P

What are you getting at with this response?

Rightly or wrongly, I did mean -1!!!

Did you actually take the time to copy, change and compile the OP's, AD's or my code before posting this response? I think not. Read what's going on in this thread before responding with smart ass one off code liners! ;)

What are you getting at with this response?

Rightly or wrongly, I did mean -1!!!

Did you actually take the time to copy, change and compile the OP's, AD's or my code before posting this response? I think not. Read what's going on in this thread before responding with smart ass one off code liners! ;)

Wow sorry, I really didn't mean to get anyone mad. :( Yes, I have compiled everyone's code. Here's what the output looked like on the modified version of yours:
tWeNtY fIrSt CeNtUrY bReAkDoWn - gReEn DaY
tHiS iS a TeSt Of StRiNgS
Did I get it wrong? If so, I'm sorry.

Wow sorry, I really didn't mean to get anyone mad. :( Yes, I have compiled everyone's code. Here's what the output looked like on the modified version of yours:
tWeNtY fIrSt CeNtUrY bReAkDoWn - gReEn DaY
tHiS iS a TeSt Of StRiNgS
Did I get it wrong? If so, I'm sorry.

The output you've posted is incorrect for the corresponding code I posted. That's the output you get if you change wordoffset = -1 to wordoffset--. If you run my ORIGINAL code you'll get:

tWeNtY fIrSt cEnTuRy bReAkDoWn - gReEn dAy

The OP "complained" that the output from Ancient Dragon's first response included the count of spaces for the mod calculation. From that response I concluded that the OP wanted the first letter of each word in the string to be lowercase and up-low thereon until the next word came along. That's the reason I posted the code changes that I did (based on AD's original code).

As I said in my previous post, I now "know nothing" of what the OP really expects. Until the OP posts exactly what is expected in the ouput I'm classing this thread null and void.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.