Program to capitalize every other word (beginning question)

This is a discussion on Program to capitalize every other word (beginning question) within the C Programming forums, part of the General Programming Boards category; Hi! I'm trying to write a program that capitalizes every other word of a user output. (ex: if input is ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    23

    Program to capitalize every other word (beginning question)

    Hi! I'm trying to write a program that capitalizes every other word of a user output. (ex: if input is "i love you", the output is "i LoVe YoU").

    I am using a for loop (although an if loop might be easier?) and I'm assuming that it is possible to write this program using a for loop (although I may be wrong--maybe only if loops can write this program). I have everything down EXCEPT the last step where I have to print the entire sequence.

    What i have so far is:

    Code:
    #include <cs50.h>
    #include <stdio.h>
    #include <string.h>
    
    {
        // get user input
        printf("Type a sentence: ");
        string s = GetString();
    
        char new;
        // make every other letter capital
        if (s != NULL)
        {
            for (int i = 1, n = strlen(s); i < n; i +=2)
            {
                new = (((int) s[i]) - 32);
                printf("%c", new);
            }
            printf("\n");
        }
        return 0;
    }

    The code above basically only prints out the capital letter (ex: input is "hello", output is "EL").

    I have been thinking and thinking about this problem, but I don't know how to print out the entire sequence. What am I missing? Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Wait a second - (ok, it's every other letter, not word, but details... )

    How did you create this?
    the output is "i LoVe YoU")
    It might help to work your magic on several strings with paper and pencil SLOWLY, and see what patterns YOU use, to do this exercise, yourself.

    That pattern is the same pattern that your program can use to solve this. Work it through several times. Light bulbs have been known to suddenly turn on!

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Rubik View Post
    I am using a for loop (although an if loop might be easier?)
    An if-statement is not a loop but a selection/branching statement.

    Code:
    for (int i = 1, n = strlen(s); i < n; i +=2)
    {
        new = (((int) s[i]) - 32);
        printf("%c", new);
    }
    Think about what will happen when the letter is already in uppercase.
    You are only printing every second letter because you increase 'i' by two after every iteration.
    And why do you cast s[i] to int?

    You could also read about the standard library function toupper() if you are allowed to use it.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about word reversing program
    By amadeus111 in forum C Programming
    Replies: 5
    Last Post: 01-16-2013, 05:28 AM
  2. Capitalize first Character in word.
    By duongducthieniu in forum C Programming
    Replies: 10
    Last Post: 01-06-2013, 01:16 AM
  3. Capitalize first letter of a word (function)
    By xwielder in forum C++ Programming
    Replies: 13
    Last Post: 10-06-2011, 12:11 AM
  4. Replies: 3
    Last Post: 02-21-2011, 10:21 PM
  5. Capitalize first letter of every word in .txt file
    By crazygopedder in forum C Programming
    Replies: 9
    Last Post: 10-15-2008, 01:09 PM

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