Computer problems,Computer help
*AX SOFT>>>Programming & Design

Can anyone help with this C function?


Ok i had posted a help thing with this earlier, but everyone thinks im asking them to do the entire program for me. I got most of the program working but this section is confusing me a bit in the coding, if anyone could help me with ideas in the coding, im supposed to ask the user to enter a string of characters, and part of it is supposed to do what it says below with a made function.

1.Ensure that a space follows all punctuation (with the exception of the instances listed below)

No spacing around a dash (-)
No spacing around a quote (鈥?

2.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

I will assume that you have read a string into a buffer. You can pass that string to a function like this which will return a pointer to a string that has been converted to meet your requirements:


/* you will need to add error checking for buffer overrun if in is >2048 or whatever size you select for output */
#include "ctype.h"
char *enforce_rules( char *in )
{
char out[2048]; /* 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 )
{
*op = 0;
if( ispunct( *in ) )
{
if( *in != '\'' && *in != '-' ) /* next must be space */
need_space = 1;
else
{
need_space = 0;
need_upper = 1;
}

*op++ = *in++; /* add character */
}
else
if( isspace( *in ) )
{
need_space = 0;
need_upper = 1; /* next must be upper */

*op++ = *in++;
}
else
{
if( need_space )
*op++ = ' '; /* add space if needed */
if( need_upper ) /* 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 */

}

--------- edit ------------
Forgot to set the need_upper flag if dash or quote encountered. Modified to add that.

for this you will need iostream.h to be included, and im "using namespace std;"

//
cout >> "Enter string of charaters";
char x;
cin << x;
//

this will prompt the user to input a value and then it will be stored in variable x. This is c++ so im not sure if it will work. But if you wish to applie those rules you listed so a user is not able to disobay them then you need to do that on your own because that will take about 60 lines of code, lol, have fun.

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • Why so many systems development projects fail?

    Poor project management. Sorry to say, but IT developers are not always the best at seeing the big picture - they can be brilliant at technology but having someone who manages the scope, timeline, ...

  • How do I get Binary(.Bin)Extension back???

    Right click, then 'Open With', then 'Choose Program', then select the program you want to use in the list, or Browse and find the .exe (your flashing program). Make sure '...

  • HTML help? What's the code to make a link open up in a different window?

    <a href="linkedpage.html" target="_blank">Click here</a>

    ...
  • Hi, does anyone know the best way to create a javascript code, for a password between 5 and 10 characters?

    <html> <head> <!-- No matter which you select as "best answer" - pick one. --> <script> function get(eid) { var d = document; var r = d.getElementByI...

  • Is C++ important to know in Engineering?

    C++ is being used more and more with embedded systems. Even some real-time systems depend on it. Off hand, if you really know Java, I'd say stick with that language until you it becomes more...

  • Oracle 10g - Environment Variable - WINDOWS?

    What SID did you use when you set up Oracle? I think it defaults to ORCL. Try set ORACLE_SID=ORCL at the command prompt and if that works set it permanently via your control panel. If you can�...

  • Staring your own website?

    Since you don't' have much knowledge you could start with a blog. Try wordpress ...

  • Java Programming: List/Itterated Merge Sort?

    Just replace the arrays in this code with a java.util.List and then use the List's iterator in the for-loops. public class Mergesort { /** * The main method illustrates the use of ...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster