That's a sure sign of a string without a NULL terminator. Add one in, see what happens.
That's a sure sign of a string without a NULL terminator. Add one in, see what happens.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I see...
exactly where should I include
I tried putting into the loop, but it was causing the conversion part not to work at all.Code:result[i + times] = 0;
If i put it outside, it just says i is not identified.
Aside from the several points that dwks pointed out, this will not work right:
Where are you writing the z? Is that REALLY where you want to write it, assuming your input is "resolved result"? Step through it manually and see what happens [or with a debugger if you know how to use one].Code:if (line [i] == 's') { result[i] = 'z'; }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
These are one thing. You'll also need to null-terminate the result and make sure it is as large as needed.Code:if (line [i] == 's') { result[i + times] = 'z'; } else if (line[i] == 'r') { ...
Of cource, everything would be easier if you used C++ strings all the way:
Code:#include <iostream> #include <string> int main() { std::string source; std::getline(std::cin, source); std::string dest; for (std::size_t i = 0; i != source.size(); ++i) { switch (source[i]) { case 's': dest += 'z'; break; case 'r': dest += "rr"; break; default: dest += source[i]; } } std::cout << source << '\n' << dest << '\n'; }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Oh the light!
You all have been extremely helpful, thanks for all those inputs.
I did what you suggested, and it seems to be working.
here is the updated version:
the next thing I have to do is to replace every "th" in the string with a "z". Would I be able to do this just by include another if stamement or should I create another for loop with result since I will be shifting indexes again?Code:// C-String Lab.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> using namespace std; #define MAX_SIZE 200 int main() { int index = 0; int times = 0; char line[MAX_SIZE]; cin.getline(line, 200); cout << "You entered: " << line << endl; string str(line); cout << "The size of the line is " << str.size() << " characters.\n"; char result [256]; int i; for (i=0; i < str.size(); i++) { if (line [i] == 's') { result[i + times] = 'z'; } else if (line[i] == 'r') { result[i + times] = 'r'; result[i + times + 1] = 'r'; times ++; } else { result[i + times] = line[i]; } } result[i + times] = 0; cout << result << endl; // char* dest = new char[newSize + 1]; return 0; }
One final question:
for the array result,
I tried making it
char result [str. length() * 2] so it would be dynamically allocated. But i got an error saying size of result cannot be 0. I am assuming this is happening because I only get an actual length once the user inputs it. But, how could I accomplish something of that effect?
You could do it in that loop. It wouldn't be too difficult. Something like this:
Also note what I said above about not needing to create an entirely new std::string. You could do this instead:Code:if(array[index] == 't' && array[index+1] == 'h') { result[rindex] = 'z'; }
You'd have to include <cstring>, which is where strlen() is located. And you could drop <string>, as yuo wouldn't be using any std::strings.Code:int i, len = strlen(line); for (i=0; i < len; i++)
[edit]I don't think you can declare variable-sized arrays in standard C++. Try this instead:I tried making it
char result [str. length() * 2] so it would be dynamically allocated. But i got an error saying size of result cannot be 0. I am assuming this is happening because I only get an actual length once the user inputs it. But, how could I accomplish something of that effect?
I'd use str.length()*2+1 for the size, though, because that's the maximum it could be. Don't forget the NULL. [/edit]Code:char *result = new char[str.length() * 2]; // ... delete [] result;
Last edited by dwks; 11-07-2007 at 03:49 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Code:for (k =0; k < size of result; i++) { if (result[k] == 't' && result[k+1] == 'h') newResult[k] == 'z'; }
--> does this seem to be in the right track?
[EDIT] thanks dwks, I guess I had the idea...let me incorporate those things in...
Last edited by Fredir; 11-07-2007 at 03:48 PM.
I don't think the if statement is working... I will see if I can do the above loop later:
Code:// C-String Lab.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> using namespace std; #define MAX_SIZE 200 int main() { int index = 0; int times = 0; char line[MAX_SIZE]; cin.getline(line, 200); cout << "You entered: " << line << endl; string str(line); cout << "The size of the line is " << str.size() << " characters.\n"; char result [256]; int i; for (i=0; i < str.size(); i++) { if (line [i] == 's') { result[i + times] = 'z'; } else if (line[i] == 'r') { result[i + times] = 'r'; result[i + times + 1] = 'r'; times ++; } if (result[i] == 't' && result[i+1] == 'h') { result[i] == 'z'; } else { result[i + times] = line[i]; } } result[i + times] = 0; cout << result << endl; // char* dest = new char[newSize + 1]; return 0; }
See anything different.Code:else if (line[i] == 'r') ... if (result[i] == 't' && result[i+1] == 'h')
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The code doesn't take unicode into mind either. It's good practice to try to use TCHAR functions and _T() macro to make it both unicode/ansi safe. It takes some getting used to, I've found out.
Ok, looks like I completed the code. Here is the final version:
Code:#include "stdafx.h" #include <iostream> #include <cstring> using namespace std; #define MAX_SIZE 200 int main() { int index = 0; int times = 0; char line[MAX_SIZE]; cin.getline(line, MAX_SIZE); cout << "You entered: " << line << endl; cout << "The size of the line is " << strlen(line) << " characters.\n"; int len = strlen(line); char* result = new char[len*2 +1]; int i; for (i=0; i < len; i++) { if (line [i] == 's') { result[i + times] = 'z'; } else if (line[i] == 'r') { result[i + times] = 'r'; result[i + times + 1] = 'r'; times ++; } else if (line[i] == 't' && line[i+1] == 'h') { result[i + times] = 'z'; } else { result[i + times] = line[i]; } } result[i + times] = 0; cout << "Translation: " << result << endl; delete [] result; return 0; }
Thank you all for the help. If anyone wants to give tips as how to make the code more efficient for my own benefit in the future, that would be nice.
Your code is pretty good now. One of my only suggestions is this:
Swap those lines, and you can use the variable len in the cout. strlen() does take a little while to execute, so your program will be marginally more efficient if you do as I suggest.Code:cout << "The size of the line is " << strlen(line) << " characters.\n"; int len = strlen(line);
Both of those are Windows-only functions. You shouldn't recommend them in a program that is otherwise perfectly standard C++. On the other hand, because the OP is including "stdafx.h", they're clearly using a Windows compiler (probably MSVC). Still, when you recommend things like that, at least mention in passing that they're Windows-specific.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.