How do i change a letter out of a of a word
Hi guys
I'm doing a program for one of my class but the teacher asked us to write a language mangler to modfiy a sentence and make it nonsentence
And he want us :
- If any word has length 9 or more characters, insert into the word after the sixth letter either "-er-" or "-uh-" or "-um-". Cycle through these inclusions in a regular fashion.
- If a word ends in 's', add 'es' to it.
- Change any instance of "is" to "are", "he" to "him", "she" to "her".
Everything without using pointers and strings
we have to us NTCA
thank you for your help
jack
Jacky1
Junior Poster in Training
51 posts since Nov 2007
Reputation Points: 10
Solved Threads: 2
>Everything without using pointers and strings
By "string" you mean the std::string class? Assuming you can store the characters for a word somewhere (like an array or a vector), it's fairly easy to follow those rules. However, without knowing what level of C++ you're at, I'll probably end up giving you ideas that are too advanced to be useful. So please post your most recent attempt to solve the problem so that we can better help you.
>we have to us NTCA
The North Texas Cricket Association? No no, the Northumbrian Tent Camping Association! :D Don't just throw out acronyms unless they're pretty much guaranteed to be well known or easily researched.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Thank you for you replay
Actully he told us to choice randomly from a text file so I'm trying to use this function
void randPick(istream input, char phrase[])
{
int rad;
rad = rand()% 11;
input.open("input.txt");
for (int i=0; i <= rad; i++)
{
getline(input,phrase);
}
return;
}
but it's not working
if I can use arrays to get the hole phrase I think it'll be good
NTCA : null-terminated character arrays :)
Thank you for your help
Jacky1
Junior Poster in Training
51 posts since Nov 2007
Reputation Points: 10
Solved Threads: 2
>rad = rand()% 11;
How do you know there are ten lines in the file? How do you know that's always going to be true? It's fine for a single project where the size of the file is a hard limit, but in any reasonably robust code you'd want to count the lines first, or find a way of getting a random line without getting the size of the file first.
>but it's not working
My doctor hates it when I tell him that "something's not right", just like I hate it when you tell me that "it's not working". How is it not working?
>NTCA : null-terminated character arrays
I've been programming for over ten years. I've probably read more code and technical papers than most professional programmers with similar experience, and this is the first time I've seen that particular acronym. A search for that particular acronym on google produces exactly one hit, and that hit is a homework assignment. In other words, don't expect anyone to know what the hell you're talking about when you say NTCA. :icon_rolleyes:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Thank you
I used rad = rand()% 11; becuase he gave us a single file to work with so
I test the function but it's not doing any thing just stuck until I use CTRL + C but I see it's creat an output.txt file but nothing on it :(
Thank you again
Jacky1
Junior Poster in Training
51 posts since Nov 2007
Reputation Points: 10
Solved Threads: 2
No one here is going to give you code to do your homework. You need to think about the problem some bit more than you have so far, post all the code you've got and any compiler errors or i/o errors, and then we'll help you fix it.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
>input.open("input.txt");
input is an object of type std::istream. std::istream doesn't have an open member function. Your code shouldn't compile.
>getline(input,phrase);
The non-member function form of getline is for std::strings only. phrase is a pointer to char, so this shouldn't compile.
Maybe something more like this (still not ideal, but oh well):
bool randPick ( const char *filename, char phrase[], int size )
{
std::ifstream in ( filename );
bool rc = false;
if ( in ) {
int line_number = rand() % 11;
int i;
for ( i = 0; i < line_number; i++ ) {
if ( !in.getline ( phrase, size ) )
break;
}
rc = i > 0;
}
return rc;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>They make the instructions a challenge to read in itself. No joke
I've got bad news for you, slick. In professional projects, you often don't get specifications that are even as good as this one. When you do get good specifications, they're often so detailed that it's like deciphering encrypted codes (see the language standard for an excellent example). No joke.
>to make it harder than necissary in order to reinforce habits/ideas.
More likely to make the exercise in any way challenging as std::string has member functions that neatly solve the problem without any thought from you. With C-style strings you have to actually put some effort into a working solution.
>Which unfortunately if I understand it does not allow
>you to stream the information into an array of strings
I don't see that restriction anywhere. In fact, while you're prohibited from using std::string, there doesn't seem to be a restriction on std::vector, so you can greatly simplify how you handle the array of strings by making it a vector of strings.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>and they *deeply* frown upon using things we eh 'shouldn't' know yet.
Probably because they don't want you to discover that your teachers, who should be experts on what they teach, are actually only about half a chapter ahead of you in learning C++. ;)
>Ultimately, I was clarifying the instructions for the other student
I know, but I feel the need to stick my finger in most of the pies on this forum.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>Which unfortunately if I understand it does not allow
>you to stream the information into an array of strings
I don't see that restriction anywhere. In fact, while you're prohibited from using std::string, there doesn't seem to be a restriction on std::vector, so you can greatly simplify how you handle the array of strings by making it a vector of strings.
:icon_rolleyes:>and they *deeply* frown upon using things we eh 'shouldn't' know yet.
Probably because they don't want you to discover that your teachers, who should be experts on what they teach, are actually only about half a chapter ahead of you in learning C++. ;)I can think of other possibilities :icon_wink:
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>I can think of other possibilities
I can't[1]. As a teacher[2] I have no problem with students going above and beyond. The only good excuse for holding someone back when he's willing and capable of more is being embarrassed at not being able to keep up.
[1] Though I haven't given it much thought.
[2] Not as my primary occupation, of course. I actually work for a living. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401