LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-29-2007, 08:17 PM   #1
ambitious88
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Rep: Reputation: 0
C/C++ Programming Problem




I would like somebody to help me with this programming question:

I am supposed to code the following C/C++ function:

int match( char str[ ], char sentence[ ], int word_no );

This function returns a true value if the string "str" matches a certain part of
the string "sentence." The matching starts at the word whose word number is
defined by "word_no." Otherwise it returns a false value.

There are special rules for having a match:

(1) The match is case insensitive. For example, 'Z' matches both
'z' and 'Z'.

(2) The word number("word_no") in a sentence is an integer value that is
between 1 and the total number of words in the sentence.
If a sentence has 3 words, the word number of the first word is 1 and
the word number of the third word is 3. If "word_no" is an invalid word number
for "sentence", then no matching will be found.

(3) The last character of "str" must not fall in the middle of a word in
"sentence". If this happens, it will not be considered as a match.

(4) If there are whitespaces embedded in "str", then all these whitespaces
must have an exact match in "sentence".
(Note: If "str" begins with a whitespace, it will not match anything).


Example 1:

match("left", "Proceed three units forward and turn left", 7)

returns a true value

Example 2:

match("left", "Proceed three units forward and turn left", 3)

returns false.

Example 3:

match("*r2-d2*",
"Good morning \n\t *R2-D2* & c3-p0 ", 3)

returns true.

Example 4:

match("*r2-d2* & C3-P0",
"Good morning \n\t *R2-D2* & c3-p0 ", 3)

returns true.

Example 5:

match("robot", "Welcome to the \n \t\t\t robotics department", 4)

returns false.

Reason: The string "robot" falls in the middle of the word "robotics" in the sentence.

Example 6:

match("H.A.L. 9000", "Hello H.A.L. 9000", 2)

returns false.

Reason: The whitespace in the string "H.A.L. 9000" is not an exact match
with the whitespace in the string "H.A.L. 9000".

IMPORTANT NOTES

1. You must not declare any temporary arrays when you code these functions.

2. You must not change any of the strings that are passed into the function

here is my function so far:

int match( char str[], char sentence[], int word_no )
{

int ret = getwords( sentence,word_no,str );

return ret;

}


int getwords( char s[], int w_no, char st[] )
{
int size;
int num;
int count = 0;
int length = 0;
int i = 0;

size = strlen( s );

num = words( s );

char sen[size+1];

strcpy( sen,s );

char list[num][size];

char w[size];

//Find words in list
while(sen[i] != '\0')
{
//Skip over spaces
while(sen[i] == ' ')
i++;

//Copy characters that are not space or \0 as part of a word
while(sen[i] != ' ' && sen[i] != '\0')
list[count][length++] = sen[i++];

list[count++][length] = '\0'; /* Append terminator */
length = 0; /* Reset ready for next word */
}

//List the words that were found
printf("\nThe words in the list are:\n");

for(i = 0 ; i < count ; i++)
printf( "->%s\n",list[i] );

i = w_no - 1;

printf( "str->%s\n",list[i] );

if((!strcasecmp(st,list[i])) )
{
return 1;
}

else
return 0;
}


here is the main:

#include <stdio.h>
#include <string.h>

#include "command.h" // header file

#define NUM 10

void format(struct info *);
void print_str(const char [ ]);

struct info {
char type, phrase[81], act_word[21], com_word[21];
int word_pos, total, word_num, act_word_num;
};

int main( ) {
struct info x;

char phrases[NUM][81] = {
" R2! \t\t Where \n\n are you?",
"Non-sequitur! You facts are uncoordinated...\n",
" What are you doing now \n\nHal?\t\t \n \n \t \t ",
"enERGizE",
"Going somewhere SOLO?",
"\n\n\tKlatuu\tBaradas\tNiKto\t\n",
"My god... It's full of \nSTarS!\t ",
" \t \n \t \n\n\t\t \n\t \t ",
" The morloks live below, the ELOIlive above\n\n\n ",
"Proceed \n three units forward and turn leFT & RIgHT test"
};

char correct_words[NUM][21] = {
"R2!", "Non-sequitur!", "Hal?", "enERGizE", "SOLO?", "NiKto",
"STarS!", "", "ELOI", "leFT & RIgHT "
};

char test_words[NUM][21] = {
"r2!", "Non_sEQUitur!", "haL?", "ENERGIZE", "soLo?", "nIkTO",
"sTaRs!", "", "eloi", "LEft & rIGht "
};

char sentence[81], str[21];
int word_pos[NUM] = { 1, 1, 6, 9, -3, 3, 7, 6, 6, 7 };
int result[NUM] = { 1, 0, 1, 0, 0, 1, 0, 0, 0, 1 };
int num_of_words[NUM] = { 4, 5, 6, 1, 3, 3, 6, 0, 7, 10 };
int i, j, valid=1, rc, rc2;

for(i=0; i<NUM && valid; i++) {
strcpy(sentence, phrases[i]);
strcpy(x.phrase, phrases[i]);
rc = words(sentence);
if(rc != num_of_words[i]) {
x.type = 'w';
x.word_num = rc;
x.act_word_num = num_of_words[i];
x.total = i + 1;
format(&x);
valid = 0;
}
else {
printf("You have passed test %d...\n\n", i + 1);
printf("Press the enter key to continue...");
getchar( );
}
}

for(j=0; j<NUM && valid; j++) {
strcpy(sentence, phrases[j]);
strcpy(str, test_words[j]);

strcpy(x.phrase, phrases[j]);
strcpy(x.act_word, correct_words[j]);
strcpy(x.com_word, test_words[j]);
rc = match(str, sentence, word_pos[j]);
rc2 = rc ? 1 : 0;
if(rc2 != result[j]) {
x.type = 'm';
x.word_pos = word_pos[j];
x.total = j + NUM + 1;
format(&x);
valid = 0;
}
else {
printf("You have passed test %d...\n\n", NUM + j + 1);
printf("Press the enter key to continue...");
getchar( );
}
}

if(valid) {
printf("\nCONGRATULATIONS! YOU HAVE PASSED ALL THE TESTS.\n");
}
return 0;
}

void format(struct info *p) {
printf("Your '%s' function has failed on test... %d\n",
p->type == 'w' ? "words" : "match", p->total);

switch(p->type) {
case 'w':
printf("Phrase------> ");
print_str(p->phrase);
printf("Number of words you isolated----> %d\n", p->word_num);
printf("Actual number of words----------> %d\n", p->act_word_num);
break;
case 'm':
printf("Phrase----------> ");
print_str(p->phrase);
printf("Word number-----> %d\n", p->word_pos);
printf("Actual word-----> '%s'\n", p->act_word);
printf("Test word-------> '%s'\n", p->com_word);
break;
}

printf("\nYour program has passed %d test(s)...\n", p->total);
switch(p->total) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
printf("Your program still needs considerable work!\n");
break;
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
printf("Your program still needs some work!\n");
break;
case 16:
case 17:
case 18:
case 19:
printf("Your program is close to passing all the tests!\n");
break;
}
printf("Keep working on it!\n\n");
}

void print_str(const char s[ ]) {
int i, len = strlen(s);
printf("'");
for(i=0; i<len; i++) {
switch(s[i]) {
case '\n':
case '\t':
printf("\\");
printf("%c", s[i] == '\n' ? 'n' : 't');
break;
default:
printf("%c", s[i]);
}
}
printf("'\n");
}
 
Old 09-30-2007, 01:40 PM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
I know this might be overkill, but can you use the boost programming libraries? If so then use their regular expressions, and it becomes an easy one. The regex would be ^.*(\s*WORDVAR\s*).*$. If match is found it's in the sentence and is a complete word, if not then it's not in there. If you can't use other libraries I'll look harder next time.

Regards,

Alunduil
 
  


Reply

Tags
manipulation, string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C programming problem kentmaster Linux - Newbie 2 02-18-2007 04:49 AM
Qt Programming Problem chanwing Programming 7 04-20-2005 05:18 AM
another c programming problem Randall Programming 8 01-29-2003 05:45 PM


All times are GMT -5. The time now is 04:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration