473,378 Members | 1,447 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,378 software developers and data experts.

String Concatenation Programe in C/C++

OraMaster
135 100+
Hi All,

I just want to know the different approch for this program to build from experts. Program is like we provide 2 strings as input and output string should contain both input strings as substrings.
for e.g
1. Input=> str1: abcd str2: bcde Output: abcde
2. Input=> str1: bcdf str2: dfyn Output: bcdfyn
3. Input=> str1: ytmsqp str2: msqps Output: ytmsqps

Also pls provide me some tough combinations of input and output.
I did this program so pls don't consider this is my HW :)
Aug 26 '09 #1
3 2172
Banfa
9,065 Expert Mod 8TB
I would think the algorithm would be something like

Expand|Select|Wrap|Line Numbers
  1. LOOP n = 1 to length(String1)
  2.     IF character n of String1 == character 1 of String2
  3.         IF last characters of String1 == first characters of String2
  4.             Terminate String1 at n
  5.             Concatinate String2 onto String1
  6.             Force End of Loop
  7.         ENDIF
  8.     ENDIF
  9. ENDLOOP
  10.  
  11. IF LOOP ended naturally rather than being forced
  12.     Concatinate String2 onto String1
  13. ENDIF
  14.  
Try the following combinations

abcde and fghij producing abcdefghij
abcde and cdfgh producing abcdecdfgh
abcdecd and cdfgh producing abcdecdfgh
Aug 26 '09 #2
OraMaster
135 100+
@Banfa
Thanks! Sounds Good.
Aug 26 '09 #3
JosAH
11,448 Expert 8TB
If you want your two string S1 and S2 to be S1 == Xp and S2 == pY and the output to be XY where |p| (the length of substring p) to be maximal have a look at the 'failure function' used in the Boyer-Moore algoritm as well as the Knuth-Morris-Pratt algorithm. You can find substring p in linear time, i.e. O(|p|) instead of quadratic time as shown by Banfas algorithm. (Google is your friend).

kind regards,

Jos
Aug 26 '09 #4

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

Similar topics

5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
9
by: Justin M. Keyes | last post by:
Hi, Please read carefully before assuming that this is the same old question about string concatenation in C#! It is well-known that the following concatenation produces multiple immutable...
16
by: Mark A. Sam | last post by:
Hello, I am having a problem with imputting into a string variable: Dim strSQL As String = "INSERT INTO tblContactForm1 (txtName, txtCompany, txtPhone, txtEmail, txtComment, chkGrower,...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
6
by: Calros Lo | last post by:
Dear all: I develop a programe that need when I get a string , such as "123" or "ABC",if I get string "123" and the system will help me to create new string "124" , if I get string "ABC" and the...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.