Thread: manipulating strings

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    23

    manipulating strings

    I am writing a program where the user inputs a string and have to make the string "dance" based on a rhythm they also input.

    rhythm 2 : *_*_*_*_ etc.. (where star is capitalized and _ is lower case)

    This is my code so far:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main (void)
    {
        char line[100];
        int rythym;
        
        char *p;
        int k =0 ;
    
    
    
        printf("Enter a string:");
        gets(line);
        
        printf("Enter a rythym:");
        scanf("%d", &rythym);
    
        printf("Original String: %s\n", line);
    
        for (p = line; *p != '\0'; p++)
        {
           *p = toupper(*p); 
        }
    
        while(line[k] != '\0')
        {
            if(rythym == 2)
            {
                line[k] = tolower(line[k]);
                
                k+= rythym;
            }    
        
        }
    
        printf("After rythym applied: %s\n\n", line);
    
    return 0;
    }
    Code:
    Output:
    Enter a string:Hello World make me dance
    Enter a rythym:2
    Original String: Hello World make me dance
    After rythym applied: hElLo wOrLd mAkE Me dAnCe
    I still have two things to address:

    I need the first word to always be capitalized and I also need to not count whitespaces for the rhythm.

    So my output needs to look like this:
    "HeLlO wOrLd MaKe Me DaNcE"

    I have an idea as to how to make the first word always capitalized but I need tips or advice on not applying the rythym for whitespace.

    Thanks so much in advance

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    All you have to do is start k at 1 instead of 0. If you keep incrementing by the rythym, like you do, it should ensure that all the lower case characters are in odd elements of the string, which is what you want. Because you are using an increment of 2, I would also change the loop condition to make sure that k stays less than the size of the string: k must not exceed the size of the string. You would only end up at the terminating zero when the string is an even size.

    There is an isspace function so that you don't apply the rule on space characters.

    Also, beware of gets. FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    Last edited by whiteflags; 05-02-2012 at 07:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Strings? (tolower, toupper, ucfirst)
    By Atolar in forum C Programming
    Replies: 4
    Last Post: 02-10-2010, 08:00 AM
  2. Manipulating C strings
    By black_spot1984 in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 10:23 AM
  3. Help manipulating int
    By Canucklehead in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:07 PM
  4. manipulating strings
    By student2005 in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2004, 03:21 AM
  5. manipulating strings
    By rain_e in forum C Programming
    Replies: 14
    Last Post: 03-05-2002, 03:15 PM