|
 |
|
 |
04-14-2004, 09:38 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: cold
Posts: 5
|
combining arrays
Hey everyone, I've tried searching but was unable to come up with a solution that would work for me. I have 3 arrays
Code:
char string1[] = {"My name is "};
char string2[] = {"Fred."};
char string3[50];
And I need to add string1 to string3, then add string 2 to string 3 (everything that was just added from string 1), Using a for or while loop.
Here is what I have so far, given to us by our prof:
Code:
for(int index = '0'; string1[index] != 0; ++index)
string3[index] = string1[index];
for()
As I stated before we have to concatenate the 2 strings (3 with 2 appended) and it has to be generic so that it would work for any text that could fit in those strings, so that it reads "My name is Fred" as if it were all in one array.
TIA
|
|
|
04-14-2004, 10:53 PM
|
#2 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
well, i won't do your homework for you, but if you post up what you tried, maybe we can help you out with it.
first, though, the code you pasted has a bug:
Code:
for(int index = '0'; string1[index] != 0; ++index)
'0' is NOT the same as 0 (in fact, '0' = 48). you probably want index to start from 0, not '0'.
looks like your professor gave you half of the solution. look at that code and think about what it does. what state will your arrays be in once it does a few iterations? when does it stop? what else do you need to do to complete the problem.
|
|
|
04-14-2004, 11:09 PM
|
#3 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Quote:
|
As I stated before we have to concatenate the 2 strings (3 with 2 appended) and it has to be generic so that it would work for any text that could fit in those strings, so that it reads "My name is Fred" as if it were all in one array.
|
What a jolly coincedence! I just finished 25% of a tutorial about operator overloading. I went over here to test the layout so far, and noticed your post. Let me give you an excerpt of this tutorial. Then find out if you can solve it on your own. If you can't then come back again, then I'll gladly show you how in detail.
Tutorial starts here.
About basic charater and pointer handling.
Do you remember that:
char* szMyCharVariable="mytext";is exactly the same as char szMyCharVariable[]="mytext";? You will do this when the size of the assigned string is unknown beforehand. Observe the code below:
Code:
#include <iostream>
using namespace std;
int main()
{
char* szHello = "Hello";
char szHowdy[] = "Everybody!";
return 0;
}
Using the code above, we would like to show "Hello everybody!" to our console output, but first we would like to merge these two strings into a third string called "szGreetAll".
This is what I encounter often when students ask for help:
Code:
#include <iostream>
using namespace std;
int main()
{
char* szHello = "Hello";
char szHowdy[] = "Everybody";
char* szGreetAll = szHello+szHowdy; //It doesn't work this way.
cout<<szGreetAll<<endl;
return 0;
}
The code above doesn't show proper character handling.
First of all szHello+szHowdy;. You are merely trying to add two pointers. That is illegal to start with.
And secondly one shouldn't use the assignment operator on char(s) at all.
Some try to merge strings by using a for-loop and iterating through all the elements. But that is not efficient. There are special methods for assigning characters to characters:
strcpy is the most important one. And adding charracters to form a bigger string offers another method: strcat. I'm sure you've read here and there that characters are represented as arrays. So to add szHello and szHowdy, you need a third character array that can hold both characters. Lets find out how we can calculate the size of szGreetAll. Yes we can count the number of characters in this case, but often we don't have that luxury. So lets review the way to actually calculate the needed size by ourselves:
Code:
#include <iostream>
using namespace std;
int main()
{
char* szHello = "Hello";
char szHowdy[]="Everybody";
//strlen() returns the size in bytes, so we need to add +1 later. Guess why.
int len = strlen(szHello);
int len2 = strlen(szHowdy);
len+=len2;
//So szGreetAll has the size of szHello and szHowdy PLUS 2 for the terminating character and...
//...for the extra space we insert between szHello and szHowdy!
char* szGreetAll=new char[len+2];
//Line below: the way to copy and merge strings.
strcpy(szGreetAll, szHello); //copy szHello to our buffer.
strcat(szGreetAll, " "); //Merge it with an extra space.
strcat(szGreetAll, szHowdy); //And then glue szHello to the end of the buffer.
cout<<szGreetAll<<endl;
return 0;
}
Enough about character handling. We will use these techniques in our custom defined String class later.
Tutorial ends here.
Do you see the point I am making? Try to use: - strlen()
- strcpy()
- strcat()
These form the very basics of proper string handling.
__________________
|
|
|
04-14-2004, 11:22 PM
|
#4 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
Quote:
Originally posted by Valmont
These form the very basics of proper string handling.
|
wow, you seem to have missed the point entirely. the original poster said:
Quote:
|
And I need to add string1 to string3, then add string 2 to string 3 (everything that was just added from string 1), Using a for or while loop.
|
this is obviously a beginning level c/c++ question, in which strcpy is not an option.
btw, your tutorial forgets to check for allocation failure and does not deallocate szGreetAll.
|
|
|
04-14-2004, 11:51 PM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Oh using a for while loop. I missed that entirely (thanks for rubbing it in mate).
Quote:
|
Do you see the point I am making?
|
No, I won't check for allocation failure and I didn't dealllocate yet. The point was taken I think.
Quote:
|
this is obviously a beginning level c/c++ question, in which strcpy is not an option.
|
Are you to decide? I disagree. These basics should be learned asap. Since many problems later arise from a lack of proper character handling routines. How often did we need to adress string handling issues here by now?
__________________
|
|
|
04-15-2004, 04:49 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: cold
Posts: 5
|
Sorry I didn't want you guys to think I was asking for my homework to be done for me. This is a beginning class "Intro to Object Oriented Programing." I did a little research on strcat since we've never used it before and here is my code so far, probably not right but it compiles with no errors lol;
Code:
#include <iostream>
using namespace std;
int main()
{
char string1[] = {"My name is "};
char string2[] = {"Fred."};
char string3[50];
int index;
for(index = 0; string1[index] != 0; ++index)
{
//value of string1 is now stored in string 3
string3[index] = string1[index];
}
for(index = 0; string2[index] != 0; ++index)
{
//attaching string2 to the end of string3
strcat(string3, string2);
cout << string3 << endl; //pretty sure this is wrong, testing
}
return 0;
}
|
|
|
04-15-2004, 06:02 AM
|
#7 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Before we move on, I wonder: is it a MUST for you to implement the code with "for-loops" or are you also allowed to use only typical string handling routines like strcat(), strcpy() and strlen() ?
__________________
|
|
|
04-15-2004, 12:44 PM
|
#8 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: cold
Posts: 5
|
He wanted us to use only a for loop or a while loop. He didn't mention anything about strcat I researched that on my own. Off to class now. Let you know how I finish it out.
|
|
|
04-15-2004, 02:53 PM
|
#9 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Oh I see. Today I won't make it. I hope joe_bruin finds some time or someone else. Tomorrow I've got time again.
__________________
|
|
|
04-15-2004, 05:24 PM
|
#10 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
if i understand the point of this project, it is to learn to use for() or while() loops correctly. if that is the goal, then using strcat() is not what your professor wants.
assuming that, your program is getting there. up until the second "for", it's all good. the first for loop is fine. it copies string1 to string3.
the second loop needs to do the same thing as the first loop, except for one difference. instead of copying string2 to the the beginning of string3, it needs to copy string2 starting at the byte where it stopped copying string1. that is, if your first loop copied 11 characters, your second loop needs to start copying the first character of string2 into the 12th character of string3. this makes the loop more complicated, because there is another index to keep track of.
i've added some printout code to your first loop, just to show what's going on.
Code:
for(index = 0; string1[index] != 0; ++index)
{
string3[index] = string1[index];
cout << "copying string1[" << index << "] to string3[" << index << "] (letter '" << string1[index] << "')" << endl;
}
//value of string1 is now stored in string 3
cout << index << " letters copied" << endl;
|
|
|
04-16-2004, 12:27 AM
|
#11 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
|
Code:
#include <stdio.h>
int main(){
char* hello={"Hello "};
char* world={"world"};
char hello_world[50];
int index_hello=0, index_world=0;
while(hello[index_hello])
hello_world[index_hello]=hello[index_hello++];
while(world[index_world])
hello_world[index_hello++]=world[index_world++];
hello_world[index_hello]='\0';
printf("%s\n", hello_world);
return 1;
}
|
|
|
03-12-2005, 10:23 AM
|
#12 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 651
|
i was searching for the function to merge _UNICODE strings since i forgot the name
Although this topic is very old it is also maybe nice to tell how to use these functions and such in a unicode vs. ansi environment since these days more and more people are coding in UTF.
strcopy() is ANSI only and wcscpy() is UNICODE but what if your app should support both, or you're creating files that are used by several projects ANSI and UNICODE ?
International API routines solve your issues
_tcscpy() = strcpy() / wcscpy()
_tcscat() = strcat() / wcscat()
_tcslen() = strlen() / wcslen()
TCHAR = char / wchar_t
Code:
int main(){
TCHAR* hello = _T("Hello ");
TCHAR* world = _T("world");
TCHAR hello_world[50];
int index_hello=0, index_world=0;
while(hello[index_hello])
hello_world[index_hello]=hello[index_hello++];
while(world[index_world])
hello_world[index_hello++]=world[index_world++];
hello_world[index_hello]='\0';
_tprintf("%s\n", hello_world);
return 1;
}
Code:
int main()
{
TCHAR* szHello = _T("Hello");
TCHAR szHowdy[] = _T("Everybody");
//strlen() returns the size in bytes, so we need to add +1 later. Guess why.
int len = _tcslen(szHello)+_tcslen(szHowdy);
//So szGreetAll has the size of szHello and szHowdy PLUS 2 for the terminating character and...
//...for the extra space we insert between szHello and szHowdy!
char* szGreetAll=new TCHAR[len+2];
//Line below: the way to copy and merge strings.
_tcscpy(szGreetAll, szHello); //copy szHello to our buffer.
_tcscat(szGreetAll, _T(" ")); //Merge it with an extra space.
_tcscat(szGreetAll, szHowdy); //And then glue szHello to the end of the buffer.
cout<<szGreetAll<<endl;
return 0;
}
As you see _T() is used to properly identify the string as char or wchar_t depending if you compile the code using _UNICODE or just plain ANSI.
NOTE: you need to use which is a M$ SDK file, but shure you can create your own.
Just my $0.02
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:29 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|