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
-O’X... all sequences starting with O’ are capitalized and the following character is capitalized. eg. O’Brien