C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 29th, 2005, 12:17 PM
hessa hessa is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 11 hessa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 54 m 7 sec
Reputation Power: 0
Question Mixing strings !!

The output I need is
AA*aa*BB*bb*CC*cc*DD*dd*

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
char s1[]="AA BB CC DD";
char s2[]="aa bb cc dd";
char *token1Ptr;
char *token2Ptr;

//cout << "Enter first sentence : " ;
//cin >> s1;
//cout << "\n\nEnter second sentence : ";
//cin >> s2 ;

token1Ptr = strtok ( s1 , " ");
token2Ptr = strtok ( s2 , " ");
while (token1Ptr != NULL && token2Ptr != NULL )
{
cout <<token1Ptr << "*"<< token2Ptr << "*";
token2Ptr = strtok ( NULL , " ");
token1Ptr = strtok ( NULL , " ");

}
cout<< endl<<endl;
return 0;
}

Reply With Quote
  #2  
Old April 29th, 2005, 02:40 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
What gets output when you run this script as you have it?

Reply With Quote
  #3  
Old April 29th, 2005, 03:19 PM
hessa hessa is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 11 hessa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 54 m 7 sec
Reputation Power: 0
my output is
AA*aa*bb*cc*

Reply With Quote
  #4  
Old May 2nd, 2005, 11:59 AM
ShadowCoder ShadowCoder is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: Ottawa
Posts: 20 ShadowCoder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 12 sec
Reputation Power: 0
You cannot use the strtok function the way you are. The strtok function uses a static var for parsing the string into tokens. If multiple, or simultaneous calls, are made there is a good chance you will get inaccurate results. So you cannot use this function simultaneously on different strings.
Code:
char s1[]="AA BB CC DD\0";
char s2[]="aa bb cc dd\0";
char *ptr1 = s1;
char *ptr2 = s2;
char *output = new char[256];
int index1=-1;
int index2=-1;
memset(output, '\0', 256);
index1 = strcspn(ptr1, " ");
index2 = strcspn(ptr2, " ");
 
while(index1 > 0 && index2 > 0)
{
strncat(output, ptr1, index1);
strcat(output, "*");
ptr1 += index1 + 1;
 
strncat(output, ptr2, index2);
strcat(output, "*");
ptr2 += index2 + 1;
 
index1 = strcspn(ptr1, " ");
index2 = strcspn(ptr2, " ");
}
 
cout<<output<<endl<<endl;
delete[] output;

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Mixing strings !!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT