Hello all,
this question was asked in an interview..
you have a string xCy consists only 'A' and 'B' , where x,C and y all are string but y is in reverse order of x,i.e., if x is 'ABAAB' then y should be 'BAABA'.
I was to find out whether a particular string is of the form of xCy or not. I could only access next character of string. ( so it was not possible to compare first element to last one, then second to second last and so on)
Thanx and Regards,
Suyash
6 1514
I'm not clear as to your question.
I understand that a string contains on 'A' and 'B' characters.
However, xCy cannot be a string if x is a string. Do you mean an array of 3 strings??
And, is this a C++ answer you want or a C answer?
I'm not clear as to your question.
I understand that a string contains on 'A' and 'B' characters.
However, xCy cannot be a string if x is a string. Do you mean an array of 3 strings??
And, is this a C++ answer you want or a C answer?
Yes, I mean array of 3 strings.
Answer in C or C++ both are OK with me, because I am confident in both languages.
Here is a compare function using string objects that returns true if the first string is 1234 and the second string is 4321 and false otherwise. That is, the strings are identical but reversed. -
bool Compare(const string& first, const string& second)
-
{
-
string::const_reverse_iterator ritr = second.rbegin();
-
string::const_iterator itr = first.begin();
-
while ((itr != first.end()) && (ritr != second.rend()))
-
{
-
if (*itr != *ritr) return false;
-
++itr;
-
++ritr;
-
}
-
return true;
-
}
-
Just create you array of strings and call this function with two of the array members.
The function uses forward and reverse iterators instead of naked pointers.
Here is a compare function using string objects that returns true if the first string is 1234 and the second string is 4321 and false otherwise. That is, the strings are identical but reversed. -
bool Compare(const string& first, const string& second)
-
{
-
string::const_reverse_iterator ritr = second.rbegin();
-
string::const_iterator itr = first.begin();
-
while ((itr != first.end()) && (ritr != second.rend()))
-
{
-
if (*itr != *ritr) return false;
-
++itr;
-
++ritr;
-
}
-
return true;
-
}
-
Just create you array of strings and call this function with two of the array members.
The function uses forward and reverse iterators instead of naked pointers.
I think the above code cannot be the answer, because if one of the two string
reaches the end and the other has still not reached the end; then while loop ends and returns TRUE but actually it is not TRUE.
Regards,
Girish.
I think the above code cannot be the answer, because if one of the two string
reaches the end and the other has still not reached the end; then while loop ends and returns TRUE but actually it is not TRUE.
Regards,
Girish.
Yep, I think the strings should be checked for equal length before the comparison begins.
Yep, I think the strings should be checked for equal length before the comparison begins.
That's right. !!!
Regards,
Girish.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Forecast |
last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully.
: // proj2.cc: returns a dynamic vector and prints out at main~~
: //
: #include <iostream>
: #include <vector>
:
: using...
|
by: Olivier Bellemare |
last post by:
I've tried to make a function that returns the middle of a string. For
example:
strmid("this is a text",6,4); would return "is a".
Here is my code:
char *strmid(char *texte, int depart,...
|
by: Alan |
last post by:
hi all,
I want to define a constant length string, say 4
then in a function at some time, I want to set the string to a constant
value, say a
below is my code but it fails
what is the correct...
|
by: Steve Litvack |
last post by:
Hello,
I have built an XMLDocument object instance and I get the following string
when I examine the InnerXml property:
<?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
|
by: tshad |
last post by:
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","something3",0)
or would you have to do something like:
if...
|
by: Jeff S |
last post by:
In a VB.NET code behind module, I build a string for a link that points to a
JavaScript function. The two lines of code below show what is relevant.
PopupLink = "javascript:PopUpWindow(" &...
|
by: MooMaster |
last post by:
After some google searching on the forum I couldn't find any topics
that seemed to relate exactly to my problem, so hopefully someone can
help me out...
I'm running python 2.4.1 on a local Win2K...
|
by: tommaso.gastaldi |
last post by:
Hi,
does anybody know a speedy analog of IsNumeric() to check for
strings/chars. I would like to check if an Object can be treated as a
string before using a Cstr(), clearly avoiding the time...
|
by: ThatVBGuy |
last post by:
Hello All,
I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and...
|
by: Atli |
last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling.
The original Problem was posted as follows:
As...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |