Can someone explain to me how this is helpful. Im a self taught C++ programmer. The chapter I'm on now is arrays. I understand them, but now the chapters going into so much depth with "Manipulating Strings with Characters" and Concatenating two strings with a - in the middle. I find this doesn't help at all. It's very confusing and it's like a whole leap from one chapters to the back of the book. The next chapter is pointers. Here's the example:
// Concatenate - concatenate two strings
// with a " - " in the middle
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
// read first string...
char szString1[256];
cout << "Enter string #1:";
cin.getline(szString1, 128);
// ...now the second string...
char szString2[128];
cout << "Enter string #2:";
cin.getline(szString2, 128);
// ...concatenate a " - " onto the first...
concatString(szString1, " - ");
// ...now add the second string...
concatString(szString1, szString2);
// ...and display the result
cout << "\n" << szString1 << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
// concatString - concatenate the szSource string
// onto the end of the szTarget string
void concatString(char szTarget[], const char szSource[])
{
// find the end of the first string
int targetIndex = 0;
while(szTarget[targetIndex])
{
targetIndex++;
}
// tack the second onto the end of the first
int sourceIndex = 0;
while(szSource[sourceIndex])
{
szTarget[targetIndex] =
szSource[sourceIndex];
targetIndex++;
sourceIndex++;
}
// tack on the terminating null
szTarget[targetIndex] = '\0';
}
And then it talks about adding library functions? Like int strlen(string) , char* strcat(target, source) or char* strstr(string, pattern).
I just dont want to learn it for nothing, or skip it and miss something big. But I really can't see how I'll learn it. Please help.
All the book is trying to do is give you a concrete example of array manipulation by providing a (somewhat)
real-world example. The point is to learn about arrays, and you should focus on that.
In C++, one typically uses the std::string class instead of char arrays. std::string allows you to perform
complex manipulation operations with a single function call rather than having to write algorithms for them.
Yes, it is important to learn about arrays and such -- you have to learn to walk before you run.
Here's a common scenario I've found myself in lately. Someone will give me a giant CSV file (full of strings and numbers)and ask me to manipulate the data in some way. Typically this involves the following:
1) Loading the data from the CSV file into an array/vector.
2) Iterating through the container to access the data and manipulate it.
3) Write the contents of the file out to some new file.
Think of it like Excel. Every cell in an Excel spreadsheet corresponds to a cell in your array, and so you can start to see how an array would be a useful way to appropriately organize data.
In the case I mentioned before of loading a CSV file, converting all of the incoming information to strings allows me the ease of manipulation/searching/etc. I personally don't think the string manipulation business is super important, but is more something that may come up in a project somewhere down the road.
Hope this helps. Please let me know if there's still some confusion.
Well, should you ever happen write a program you will probably want it to have a user interface. And it is quite likely that the interface will be based on words. Thats all..
It's an interesting book you've got there. It uses both C and C++ standard I/O libraries.. and system("pause")..
Anyway, I believe that pointers is a lot more important subject than string operations. Though I don't think they're any easier.
However you will eventually have to learn this. It's really not that difficult. If you'd ask specific questions about the code, maybe we could help you understand.
This isn't written in any other books or tutorials. I've searched this site, nada. Even the newer versions. Unless someone can find something similar to this, it doesn't seem to make any sense :S
It's not a question of whether you need string operations or not. You need to learn to understand C++ code.
I'll try to explain:
1 2 3 4
int targetIndex = 0;
while(szTarget[targetIndex]){
targetIndex++;
}
This code iterates through the first string until a 0 is found. 0 is always the last char in the string, so when you find 0 you targetIndex equals the length of the string. (note that while(szTarget[targetIndex]) is the same as while(szTarget[targetIndex] != 0))
Now targetIndex points to the end of your first string and sourceIndex points to the beginning of the second one. You want to copy the contents of szSource and put them after the end of szTarget. Note that this loop is a lot like the first one. It stops when 0 in szSource is reached. At every iteration targetIndex points to the end of szTarget and szSource points to the appropriate char in szSource.
szTarget[targetIndex] = '\0';
Every string has to end with 0 ( '\0' == 0 ) to mark its end.