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.....

Could you post your source code.

Here's one possible way to get desired string2:

#include<iostream.h>
#include<string.h>
int main()
{
	int len1;
	char *p,*q;
	cout<<"enter string 1 length : ";
	cin>>len1;
	p=new char[len1+1];
	cout<<endl<<"enter string 1:";
	cin>>p;
	//---------------------------------
	q=new char[len1+1];
	cout<<endl<<"enter string 2:";
	cin>>q;
	cout<<endl<<strlen(q);
	//---------------------------------
	int l=strlen(q);
	for(int i=0;i<l;i++);
	for(;*(p+i)!='\0';i++)
	{
		*(q+i)=*(q+(i-4));
	}
	*(q+i)='\0';
	cout<<endl<<"result : "<<q;
	return 0;
}

>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.

Hello :

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

what's (4) mean???

Thanks a lot

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?

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!!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.