473,388 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

String Problem

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
Jun 25 '07 #1
6 1514
weaknessforcats
9,208 Expert Mod 8TB
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?
Jun 25 '07 #2
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.
Jun 26 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. bool Compare(const string& first, const string& second)
  2. {
  3.   string::const_reverse_iterator ritr = second.rbegin();
  4.   string::const_iterator itr = first.begin();
  5.   while ((itr != first.end()) && (ritr != second.rend()))
  6.   {
  7.     if (*itr != *ritr) return false;
  8.     ++itr;
  9.     ++ritr;
  10.   }
  11.   return true;
  12. }
  13.  
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.
Jun 26 '07 #4
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.
Expand|Select|Wrap|Line Numbers
  1. bool Compare(const string& first, const string& second)
  2. {
  3.   string::const_reverse_iterator ritr = second.rbegin();
  4.   string::const_iterator itr = first.begin();
  5.   while ((itr != first.end()) && (ritr != second.rend()))
  6.   {
  7.     if (*itr != *ritr) return false;
  8.     ++itr;
  9.     ++ritr;
  10.   }
  11.   return true;
  12. }
  13.  
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.
Jun 27 '07 #5
r035198x
13,262 8TB
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.
Jun 27 '07 #6
Yep, I think the strings should be checked for equal length before the comparison begins.
That's right. !!!

Regards,
Girish.
Jun 27 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
17
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,...
51
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...
18
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...
32
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...
12
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(" &...
4
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...
6
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...
5
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...
1
Atli
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.