Maybe take my mind off of things...

Nov 03, 2004 22:34

I was working on some code last week, just for the sake of learning. The program takes a sentence and reverses each word within the sentence. Eg: "This is a test" becomes "sihT si a tset". I originally tried passing variables to functions, back and forth, but I eventually realised such operations were beyond my limited skills. The code is behind the following cut:


/* OK, the aim of this program is to take a sentence from the user, and reverse each word in the sentence. Eg: "This is a test" will become "sihT si a tset". It's more complicated than just reversing the whole sentence, so I figured it would offer a decent challenge. */ #include #include // needed for gets(array-name); #include // needed for strlen(); using namespace std; void FlipDaWord(int intLen, int intTotal, char *strFlipMain); char strFlipTemp[101] = ""; int main () { // Set up some variables int intCurrentLen = 0, intTotalLen = 0, intCurrentTotal = 0; int Count = 0; char *strMain = ""; cout << "Enter a sentence of no more than 100 characters: "; gets(strMain); //cin >> strMain; cout << "\n"; intTotalLen = strlen(strMain); cout << "intTotalLen is " << intTotalLen << '\t'; cout << "strMain is " << strMain << "\n"; for (Count = 0; Count < intTotalLen + 1; Count++) { if (strMain[Count] == ' ' || !strMain[Count]) { intCurrentLen = intTotalLen - (intTotalLen - Count) - intCurrentTotal; intCurrentTotal += intCurrentLen; intCurrentTotal++; cout << "strFlipTemp BEFORE calling FlipDaWord = \t--" << strFlipTemp << "--\n"; cout << "intCurrentLen, intTotalLen, Count, intCurrentTotal "; cout << intCurrentLen << " - " << intTotalLen << " - " << Count << " - " << intCurrentTotal << "\n"; FlipDaWord(intCurrentLen, intCurrentTotal, strMain); //for (int xyz=0; xyz9) cout << X-10; else if (X<10) cout << X; } cout << "\n"; X = 0; for ( ; intLen > 0; intLen--) { cout << "\nintLen in for loop is " << intLen << "\t and X is " << X << "\t and intTotal is " << intTotal << "\n"; int intFlipTemp; intFlipTemp = intTotal - 2 - (intLen - 1); int intFlipMain; intFlipMain = intTotal - (2 + X); X++; cout << "intFlipTemp & intFlipMain are \t" << intFlipTemp << "\t" << intFlipMain << "\n"; strFlipTemp[intFlipTemp] = strFlipMain[intFlipMain]; cout << "strFlipTemp's = " << strFlipTemp[intFlipTemp] << ' '; if (intLen==1 && intTotal != strlen(strFlipMain) + 1) strFlipTemp[intFlipTemp+1] = ' '; } cout << "\nstrFlipTemp in FlipDaWord = \t--" << strFlipTemp << "--\n"; }

Now, there is one thing that still doesn't really work. For some whacked out reason, it waits for the input before it displays the message requesting the input. I'm thinking it's some kind of bug. I've compiled this in Borland's C++BuilderX for Win32, and it runs OK besides that small bug. However, the same code compiles on Fedora Core 1 (Linux), but crashes with a "segmentation fault". I didn't bother trying to find out why, although I did manage to confirm the message prompt displays before it asks for input when compiled under Linux (in a smaller program I wrote just to test this).

It should also be noted, I've left all my debugging code in tact. As far as I'm concerned, the program still isn't 100% what I want it to be, so the debug code stays until it is!
Previous post Next post
Up