String problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 63
Reputation: aminit is an unknown quantity at this point 
Solved Threads: 0
aminit's Avatar
aminit aminit is offline Offline
Junior Poster in Training

String problem

 
0
  #1
Mar 10th, 2008
Hello all:

I create a simple source file in VC++ , I tried something simple,there is two strings:

string1:MICROSOFTCPP
string2:HELLO

I want to write a code to make string2 is equal with string1,so it must be

string1:MICROSOFTCPP
string2:HELLOHELLOHEL

so it seem the string2 its repeat itself until it's length is equal to string1.........Is there any idea to make this?????

Thanks in advance.....
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: String problem

 
0
  #2
Mar 10th, 2008
Could you post your source code.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 30
Reputation: amitahlawat20 is an unknown quantity at this point 
Solved Threads: 2
amitahlawat20's Avatar
amitahlawat20 amitahlawat20 is offline Offline
Light Poster

Re: String problem

 
0
  #3
Mar 10th, 2008
Here's one possible way to get desired string2:
  1. #include<iostream.h>
  2. #include<string.h>
  3. int main()
  4. {
  5. int len1;
  6. char *p,*q;
  7. cout<<"enter string 1 length : ";
  8. cin>>len1;
  9. p=new char[len1+1];
  10. cout<<endl<<"enter string 1:";
  11. cin>>p;
  12. //---------------------------------
  13. q=new char[len1+1];
  14. cout<<endl<<"enter string 2:";
  15. cin>>q;
  16. cout<<endl<<strlen(q);
  17. //---------------------------------
  18. int l=strlen(q);
  19. for(int i=0;i<l;i++);
  20. for(;*(p+i)!='\0';i++)
  21. {
  22. *(q+i)=*(q+(i-4));
  23. }
  24. *(q+i)='\0';
  25. cout<<endl<<"result : "<<q;
  26. return 0;
  27. }
Last edited by Narue; Mar 10th, 2008 at 2:44 pm. Reason: Added code tags, do it yourself next time, please.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,805
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: String problem

 
0
  #4
Mar 10th, 2008
>Here's one possible way to get desired string2:
It's generally considered rude to provide a complete solution when the OP hasn't shown any proof of effort.

>#include<iostream.h>
This header is no longer valid C++. Many newer compilers will refuse to compile it, and some of them have done so for several versions. You'd best get a modern compiler and keep your code standard wherever possible.

>cout<<"enter string 1 length : ";
I don't particularly mind that you're using C-style strings, but it's not the best practice to give your users this kind of control. A user shouldn't have to care about the length of a string; it's your responsibility to deal with what they give you.

>cin>>p;
Well, so much for the safety of asking for a length. What's to stop me from telling you the length is 5 and then typing 5,000 characters?

>int l=strlen(q);
l is a very, very poor variable name because depending on the font, it could be confused with the digit 1. However, kudos for not calling strlen in the condition of your loop.

>for(int i=0;i<l;i++);
This loop does nothing. Note the semicolon.

>*(q+i)=*(q+(i-4));
There's no reason to use pointer notation. It doesn't make you look any smarter and only serves to complicate your code.

Finally, your code doesn't even come close to solving the problem as given by the OP, though you seem to have the right idea. Perhaps you should thoroughly test your solutions before you post them until you're confident in your ability to develop untested code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 63
Reputation: aminit is an unknown quantity at this point 
Solved Threads: 0
aminit's Avatar
aminit aminit is offline Offline
Junior Poster in Training

Re: String problem

 
0
  #5
Mar 10th, 2008
Hello :

Thanks for your answer, but why
*(q+i)=*(q+(i-4));

what's (4) mean???

Thanks a lot
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,835
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: String problem

 
0
  #6
Mar 10th, 2008
Originally Posted by aminit View Post
Hello :

Thanks for your answer, but why
*(q+i)=*(q+(i-4));

what's (4) mean???

Thanks a lot
amitahlawat20 is using pointers. q points to the memory address where a character is being stored and is using that memory address to figure out where other characters are stored and manipulate the contents of those memory locations. Unless you want to use pointers, I would not use amitahlawat20's solution. I don't think you need to use pointers for this problem. Have you ever used them?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 30
Reputation: amitahlawat20 is an unknown quantity at this point 
Solved Threads: 2
amitahlawat20's Avatar
amitahlawat20 amitahlawat20 is offline Offline
Light Poster

Re: String problem

 
0
  #7
Mar 11th, 2008
I am pursuing B.Tech in Computer Science in India, and have only three month experience in programming in C++.
I am using Microsoft Visual C++ 6.0 environment.

Thanks for your advice, I will surely keep those points in back of my mind henceforth.
Thanks again!!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 30
Reputation: amitahlawat20 is an unknown quantity at this point 
Solved Threads: 2
amitahlawat20's Avatar
amitahlawat20 amitahlawat20 is offline Offline
Light Poster

Re: String problem

 
0
  #8
Mar 11th, 2008
I have used the following:

*(q+i)=*(q+(i-4));

very sorry! actually the number 4 should be replaced by strlen of the second string.

I have made a index reach the end of string2 and then repeated it over and over again until I reach a length of string1, so *(q+i)=*(q+(i-4));
is being used to copy values from previous locations into location referenced by i which will result in string 2 in repeating itself.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC