| | |
String problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.....
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.....
Here's one possible way to get desired string2:
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Narue; Mar 10th, 2008 at 2:44 pm. Reason: Added code tags, do it yourself next time, please.
>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.
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.
•
•
Join Date: Jan 2008
Posts: 3,835
Reputation:
Solved Threads: 503
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 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.
*(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.
![]() |
Similar Threads
- Newbie string problem (C)
- continuous string (C)
- string problem (IMPORTANT) :(( (C)
- A string problem (C)
- Is there a simplest way to work this array problem? (C++)
- String Problem (Visual Basic 4 / 5 / 6)
- ANSI string problem (C++)
Other Threads in the C++ Forum
- Previous Thread: user input stops loop
- Next Thread: problem with class and switch
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






