i have to write a code for compression of lines with redundancies. You are supposed to enter a line of code--for example(YES I CAN! YES I CAN! YES I CAN!) and your output should look like this (3YES3I3CAN3!) so the codes job is to count the number of times a word or character appears. i really appreciate the help. thank you very much

Recommended Answers

All 11 Replies

OK, so what is your question?

Maybe start with reading a line and breaking it into words.

i just dont know how to break up the string into smaller parts in order to count the words

Well how do you read a line?

Consider writing a loop which looks for space characters.

so something like..

for(int i=0; i<line.length();i++)
{
if(line=='  ')
*just have something increment to store it?*
}

Yes, something like that.

If 'word' is say char word[20] , then words might be char words[10][20]

okay. so after i separate the words into a 2D array, then houw would i compare those two arrays?

Take a look at strcmp().

Or in C++, why bother with what one may call 'messy' character arrays, when there is a 'nice' string type at our disposal. Look at the .substr() method of strings. I recommend looking at the first word of the sentence, finding the next instance of that letter, checking for equality with sub strings from the first letter to the second instance of the same and the substring from the second instance plus the distance from the first to the second, etc. Know what I mean? If that fails try for the next instance of the first letter until you pass the mid-way point of the string. Then go to the second letter ... sounds like fun, huh?

haha i have no clue what you mean. but i do know how to use string compare. and i am totally f-ed in my a. lolz. this is due tomorrow and i'm clueless to how to do this

char word[50];
char words[40][50];


cin>>word;


for(int i=0; i<50;i++)
{
if(word==' ')
word=words;
}

this is what i have so far.. =/

word[i]=words[i][i];
You can't copy strings like that. You need to copy each character of a string, and to save you the trouble of writing a function like that, there's one provided in the standard library, strcpy()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.