LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 10:09 PM   #1
ambitious88
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Rep: Reputation: 0
Big C/C++ String Manipulation 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 );
which doesn't work properly

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-29-2007, 10:48 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 09-30-2007, 06:01 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Ok homework questions will not be done for you as per the forum rules, but ...
use source tags to display code with correct formatting.
choose which language you are using, C or C++.

Quote:
I would like somebody to help me with this programming question:
What is the problem you are having?

Quote:
here is my function so far:
And what is wrong with it(ie does it work)

You break your own rules
Quote:
IMPORTANT NOTES

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


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];
 
  


Reply



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
string manipulation in BASH ovince Programming 4 04-16-2007 07:15 PM
String manipulation Manuel Alvarado Linux - Newbie 2 09-06-2006 09:49 PM
string manipulation mannahazarika Programming 6 12-31-2005 11:02 AM
bash + string manipulation dave bean Programming 7 02-16-2005 11:16 AM
String manipulation with a script? philipina General 1 03-15-2004 12:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:45 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration