How could i write functions for these?

This is a discussion on How could i write functions for these? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #define SIZE 100 void convertToLowercase ( char *sPtr); char *enforce_rules( char ...

  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235

    Question How could i write functions for these?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define SIZE 100
    
    void convertToLowercase ( char *sPtr);
    
    char *enforce_rules( char *in );
    
    int main (void)
    
    {
             
            char string[SIZE];
            char string2[SIZE];
    
            printf("\nEnter A String of Characters (99 Characters MAX!!)!\n");
         
             gets(string);
            
            convertToLowercase (string);
            printf("%s",string);
            
            printf("\n\nEnter A String of Characters (99 Characters MAX!!)!\n");
         
            gets(string2);
            
            enforce_rules (string2);
            printf("%s",string2);
            
                    
            return 0;
    }
    
    void convertToLowercase ( char *sPtr)
    {
    
         while (*sPtr != '\0') {
               if ( isupper (*sPtr)){
                    *sPtr = tolower (*sPtr);
                    }
                    ++sPtr;
                    }
    }
    
    
    
    char *enforce_rules( char *in )
    {
    char out[SIZE]; /* max size of converted */
    char *op; /* pointer into output buffer */
    int need_upper = 0; /* flagto indicate next char must be uppercase */
    int need_space = 0; /* add space if next char is not */
    
    *op = *out;
    while( *in != '\0' )
    {
    *op = 0;
    if( ispunct( *in ) )
    {
    if( *in != '\'' && *in != '-' ){ /* next must be space */
    /*need_space = 1;
    need_upper = 1;*/
    putchar (' ');
    while (*in != in[1]) {
               if ( islower (in[0])){
                    in[0] = toupper (
                    in[0]);
                    }
                    in=in - 1;
                    }
    
    }
    
    else
    {
    /*need_space = 0;
    need_upper = 1;*/
    while (*in != in[1]) {
               if ( islower (in[0])){
                    in[0] = toupper (
                    in[0]);
                    }
                    in=in - 1;
                    }
    }
    
    *op++ = *in++; /* add character */
    }
    else
    if( isspace( *in ) )
    {
    /*need_space = 0;
    need_upper = 1;  next must be upper */
    while (*in != in[1]) {
               if ( islower (in[0])){
                    in[0] = toupper (
                    in[0]);
                    }
                    in=in - 1;
                    }
    *op++ = *in++;
    }
    else
    {
    if( need_space )
    *op++ = putchar (' '); /* add space if needed */
    if( /*need_upper*/ islower(*in) ) /* xlate to upper if needed */
    *op++ = toupper( *in++ );
    else
    *op++ = *in++; /* copy char */
    
    need_space = need_upper = 0; /* off flags */
    }
    }
    *op = 0;
    return strdup( out ); /* return a nice sized buffer; user must free */
    
    }

    I have done some coding a bit so far, but im so lost since im new at this language, wanted to write a program that does the following, think anyone could help me edit my code, or jst simply help me out, its supposed to do this..

    Write functions to:
    *void tolower(char *) /* Change all characters within the string str to lower case. */
    *void makeproper(char *) /* defined below */
    makeproper will:
    1. Capitalize the first character of a string.
    2. Ensure that a space follows all punctuation (with the exception of the instances listed below)
    o No spacing around a dash (-)
    No spacing around a quote ()
    3. Capitalize all text immediately following whitespace.
    - No spacing around a dash (-) but the following character is capitalized.
    - No spacing around a quote () but the following character is capitalized.
    4.with the following exceptions: (consider using a switch to check)
    -McX.... all sequences starting with mc are capitalized and the following character is capitalized. eg. McAlindon
    -MacX... all sequences starting with mac are capitalized and the following character is capitalized. eg. MacDonald
    -DuX... all sequences starting with du are capitalized and the following character is capitalized. eg. DuPont
    -DeX... all sequences starting with de are capitalized and the following character is capitalized. eg. DeLeon
    -OX... all sequences starting with O are capitalized and the following character is capitalized. eg. OBrien

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    * tolower() is already in the standard, see ctype.h
    * Work on your indenting, your code hurts my eyes
    * Don't use gets(), use fgets() instead -- See the FAQ
    * Words are mearly letters together, use that to your advantage (perhaps a for loop)?
    * avoid strdup()

    It might seem a little complex, but perhaps create a linked list of words?
    eg,
    Code:
    Hello good world, this is me!
    might look like:
    Code:
    Hello->good->world->,->this->is->me->!
    In a linked list. Once you have the words split up you can simply walk through the list comparing, modifiying as you will and rebuild the string at the end.
    Last edited by zacs7; 02-28-2008 at 08:24 PM.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    The code truly needs indenting. Try looking at the example at http://cpwiki.sf.net/Indentation and see if you can better hang of it. Or get Visual Studio express and auto-indent with Alt+F8.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
               if ( isupper (*sPtr)){
                    *sPtr = tolower (*sPtr);
    You don't actually need to check if it is upper before converting to lower - the tolower function will perform the right thing anyways (not doing anything if the character is NOT upper).

    Edit: Same obviously applies to toupper().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  2. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  3. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. functions to write a file
    By subbuy2k in forum C++ Programming
    Replies: 1
    Last Post: 12-27-2001, 09:59 AM

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