4 functions to implement :S
remove_eoln
cleanup_input
output_introduction
match_template
I need to implement those into a program. And I have no idea... Anyone who can help please. or link me to some tutorial... I tried searching on wikipedia and some pdf. no luck.
I'm new at C. I use cygwin as compiler. where should I put the script file and is the .o file important?
Re: 4 functions to implement :S
I don't recall C++ templates with those names...what language are those again?
Re: 4 functions to implement :S
I'm pretty sure this is a school assignment, and those are the names of the functions that need to be written by yourself. Terrible function names if I do say so myself.. :p
What exactly are you having trouble with?
chem
Re: 4 functions to implement :S
Actually, having looked at your other thread, it appears you're attempting to write a chat bot.
I would suggest sticking to simple things to start off, and learn the language. Chat Bots are hard work, considering theres not only knowledge of the language to understand, but how chat bots and AI work (I mean, Eliza is just a chat bot that turns your question into a response, but still..).
Start with something easier.
chem
Re: 4 functions to implement :S
You're so right chemicalNova.
Unfortunately I don't get to choose my assignment.
the function remove_eoln() scans the nul-byte terminated character array, and replaces the first found newline or carriage return caracter with a nul-byte.
I got something but I'm not sure.
Re: 4 functions to implement :S
Quote:
Originally Posted by yan03
You're so right chemicalNova.
Unfortunately I don't get to choose my assignment.
the function remove_eoln() scans the nul-byte terminated character array, and replaces the first found newline or carriage return caracter with a nul-byte.
I got something but I'm not sure.
That sounds pretty easy to implement:
Code:
for(char* c = search_in; *c != NULL; c++)
{
// if *c is '\r' or '\n', set *c to NULL and break;
}
Re: 4 functions to implement :S
Code:
void remove_eoln(char line[])
{
int i;
i = 0;
while( line[i] != 0)
{
if (line[i] == '\r' || line[i] == '\n')
{
line[i] = 0;
}
i++;
}
}
Do you think mine will work?
Re: 4 functions to implement :S
Quote:
Originally Posted by sunburnt
That sounds pretty easy to implement:
Code:
for(char* c = search_in; *c != NULL; c++)
{
// if *c is '\r' or '\n', set *c to NULL and break;
}
wow. should I use pointers too?
why did you commen your if loop?
Re: 4 functions to implement :S
Quote:
Originally Posted by yan03
wow. should I use pointers too?
why did you commen your if loop?
No, you don't need to use pointers.
Your code looks like it should work fine, with the exception that it will replace every newline or carriage return, not just the first one. If you just want to replace the first one, you just need to add a "break;" inside your if statement.
I commented the line out because it's not actually code, just what you would need to do inside the loop.
Re: 4 functions to implement :S
THE FUNCTION cleanup_input() RECEIVES A NUL-BYTE TERMINATED ARRAY OF
CHARACTERS AND MODIFIES THE ARRAY'S CONTENTS FOR LATER PROCESSING.
ALL ALPHABETIC CHARACTERS ARE CONVERTED TO LOWERCASE, ANY APOSTROPHES
ARE PRESERVED, AND UNNECESSARY SPACES BETWEEN WORDS ARE REMOVED.
FOR EXAMPLE, THE STRING
" This isn't a very LONG sentence."
WOULD BE CONVERTED TO
"this isn't a very long sentence"
THE FUNCTION RETURNS AS SOON AS A FULLSTOP ('.') IS DETECTED OR WHEN
THE END OF THE STRING IS REACHED.
Re: 4 functions to implement :S
Quote:
Originally Posted by sunburnt
No, you don't need to use pointers.
Your code looks like it should work fine, with the exception that it will replace every newline or carriage return, not just the first one. If you just want to replace the first one, you just need to add a "break;" inside your if statement.
I commented the line out because it's not actually code, just what you would need to do inside the loop.
thx you. I would have done a silly mistake.
why did you learn C by the way?
Re: 4 functions to implement :S
how do I convert a string into an integer? and how do I convert it back?
I don't really understand how the atoi function work.
The example I got is;
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
Re: 4 functions to implement :S
I got this.... can someone please check if it is right?
static void cleanup_input(char line[])
{
int i;
i = 0;
while ( i = 0; i < EOF; i++ )
{
while (line[i] >= 'A' || line [i] <='Z')
{
line[i] = line [i] - 'b' + 'B';
i++;
}
while (line[i] == ' ' && line[i + 1] == ' ')
{
line[i + 1] = 0;
}
while (line[i] == ".")
{
break;
}
}
}
Re: 4 functions to implement :S
Quote:
Originally Posted by sunburnt
Your code looks like it should work fine, with the exception that it will replace every newline or carriage return, not just the first one. If you just want to replace the first one, you just need to add a "break;" inside your if statement.
"break stops only the loop it’s in. It doesn’t break out of a nested loop, or a
loop within a loop." in a C for dummies.
it looks like break will end my if loop but the while loop will still go on and consequently the if loop will still search for carriage return or newline, am I right?
Re: 4 functions to implement :S
Quote:
Originally Posted by yan03
"break stops only the loop it’s in. It doesn’t break out of a nested loop, or a
loop within a loop." in a C for dummies.
it looks like break will end my if loop but the while loop will still go on and consequently the if loop will still search for carriage return or newline, am I right?
This is true -- break will only break out of the current loop. However, an "if" block is not considered a loop, so
Code:
while(a)
{
if (b)
break;
}
will cause you to break out of the while loop. Think about it logically -- if "break" applied to if statements, there would be no way to conditionally break out of a while loop.