|
My interviewer thrown a question like:
He wants to implement a function where he needs to takes two char* parameters as input at the end it should return a char* by concatenating these two char* values(without using strcat(), strcmp() functions).
required function prototype is
char* concat(const char* c1, const char* c2)
I am unable to solve this one. Please give me the approach
Thanx in advance.....
To invent something, you need a mountain of junk in your mind.
---------------------Thomas alva edison
modified on Thursday, January 21, 2010 2:21 AM
|
|
|
|
|
sampath-padamatinti wrote: I am unable to solve this one. Please give me the approach
David did, here.
However, if you're stuck with the const requirement on the first argument, then:
char * concat(const char * c1, const char * c2)
{
size_t size[2] = {strlen(c1), strlen(c2)};
char * c = new char[size[0]+size[1]+1];
strcpy(c, c1);
strcpy(c+size[0],c2);
return c;
}
if you can't use strlen and/or strcpy :
char * concat(const char * c1, const char * c2)
{
size_t len=0;
char * c;
const char *p;
p = c1;
while (*p++) len++;
p = c2;
while (*p++) len++;
c = new char[len+1];
p=c1;
while (*c=*p++) c++;
p=c2;
while (*c++=*p++) ;
return (c-len-1);
}
Test program:
void main()
{
const char * str1 = "hello, ";
const char * str2 = "folks!";
char * result = concat(str1, str2);
printf("%s\n", result);
delete [] result;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Here is the function as per your requirement:
char* concat(const char* c1, const char* c2)
{
int size1 = 0, size2 = 0;
for (int i = 0; c1[i] != '\0'; i++)
size1++;
for (int j = 0; c2[j] != '\0'; j++)
size2++;
char* result = new char[size1+size2];
for(int i=0; i<size1; i++)
{
result[i] = c1[i];
}
for(int j = 0; j <= size2; j++)
{
result[size1+j] = c2[j];
}
result[size1+size2] = 0;
return result;
}
|
|
|
|
|
Note that in what you are doing, you are dealing with 6 variables. You are talking about the 3 char * variables, but as you said, those are pointers. What you must remember is that pointers are pointers - they are used to point to things, in this case arrays of char. These arrays are not automatically provided. You, the programmer, are responsible for providing them for the pointers to point to.
You have to keep in mind the distinction between pointer and "pointee". Once you do that, you would not say that you want to add or concatenate 2 pointers. Instead, you want to concatenate the 2 strings contained in 2 char arrays and put the result in a third char array. You use the pointers to access the char arrays.
Good luck
|
|
|
|
|
sampath-padamatinti wrote: return c3
Are you really wanting to return a variable that will go out of scope when conc() ends?
sampath-padamatinti wrote: Please tell me how to concat(add) two char*.
Something like:
char *c3 = c1;
while (*c3)
c3++;
while (*c3++ = *c2++)
; While not exact, this is roughly what strcat() does.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
DavidCrow wrote: Something like:
char *c3 = c1;
while (*c3)
c3++;
while (*c3++ = *c2++)
;
MVP 2010 - are they mad?
|
|
|
|
|
Yes, I was wondering too
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
|
|
|
|
|
Why wondering?
It is concise and elegant, functionally equivalent to strcat , here the complete function
char * concat( char * c1, const char * c2)
{
char *c3 = c1;
while (*c3)
c3++;
while (*c3++ = *c2++)
;
return c1;
}
here a test program:
#include <stdio.h>
void main()
{
char buf[100];
sprintf(buf, "hello");
char * str = " folks!";
printf("%s\n", concat(buf, str));
}
He just missed the const reuqirement for the first argument. But this is an OP fault (i.e. David's prototype is better).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
MVP 2010 - are they mad?
|
|
|
|
|
What's that funny, Rick?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
That code is too dangerous for words. As long as the length of buf is greater than the combined length of c1 and c2 plus a null terminator it will work. But as soon as the result overflows all hell breaks loose. This maybe OK for a skilled developer such as yourself, but I would not suggest it as a solution for a newbie.
MVP 2010 - are they mad?
|
|
|
|
|
The code provide what provides strcat , no less no more. I know strcat is a very dangerous function...
God once said: "The newbies should learn C pointers or go to Hell managed".
Moreover, since that was an interview question (see [^]), the OP was expected to have such a skill.
BTW My 5 for the 'skilled developer'.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
MVP 2010 - are they mad?
|
|
|
|
|
Hi,
How can we convert a modal dialog box into modaless dialog box and vice versa by adding some code. This was the one of the interview questions.
Thanks & Regards
msr
|
|
|
|
|
It's all explained in MSDN[^], your first port of call for Microsoft technical details.
MVP 2010 - are they mad?
|
|
|
|
|
MFC? CDialog::Create() for creating a modeless, and CDialog::DoModal() for spawning a modal dialog. But that's well explained in the documentation.
You'd have cleared the interview if you had read at least one good book on MFC. Any half decent book is bound to explain the concept of 'modality' of dialog boxes.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Hi
Please help me choose a component for the following task
I have C++ Win32-application (MS Visual C++ 2005 or 2008) that must:
- periodically (say once in 5 min) check a mailbox via POP3
- retrieve new messages from there, process them (the processing is specific for the my task) and remove the processed messages.
The processing converts body of received mail into plain text (WCHAR* string). I mean: to convert removing formatting elements like HTML tags (when a mail is in HTML format) etc., leaving only text. Now I am not interested in content of attachments - I need only mail's text.
The question is: do you know a component that can solve the task too? Or: one component to read POP3, another one for extracting text from received e-mail. The components may be commercial ones (my employer is rich enough to pay for them
|
|
|
|
|
Asking Google about "c++ pop3" offers many good ideas.
MVP 2010 - are they mad?
|
|
|
|
|
|
Member 2657521 wrote: The components may be commercial ones (my employer is rich enough to pay for them
Our products extensivly use email-crackers (we're doing EDI over Internet). Feel free to send me a private email.
|
|
|
|
|
#include <stdio.h>
struct Element{
int value;
struct Element *next;
};
struct Stack{
int length;
struct Element *head;
};
struct Stack createStack(){
struct Stack s;
s.length = 0;
s.head = NULL;
return s;
}
void push(struct Stack *stack, int value){
if (stack->head == NULL){
struct Element e = {value, NULL};
stack->head = &e;
}
else{
struct Element e = {value, stack->head};
stack->head = &e;
printf("(%d,%d)", stack->head->value, stack->head->next->value);
if (stack->head == stack->head->next)
printf("same");
}
stack->length++;
}
int main(){
struct Stack s = createStack();
int i;
for (i = 0; i < 10; i++)
push(&s, i);
return 0;
}
i m really having trouble while implement stack structure in C using pointer. here the problem is only with push function, lies on 2 lines:
struct Element e = {value, stack->head};<br />
stack->head = &e;
it seems that after i assigned stack->head to &e, the e->next(which was assigned to stack->head before) changes to be same with stack->head. lines bellow print results. can someone explain why? and how to fix it. thanks !
|
|
|
|
|
Firstly you already asked this question below, you should post further queries to the same thread.
Secondly you are using a local variable in the push() function which goes out of scope when you return, thus leaving you with a (potentially) corrupt structure.
Thirdly I think you need to spend some time studying structures, classes, pointers and other elements of C/C++.
MVP 2010 - are they mad?
|
|
|
|
|
|
i changed e to a global variable and using allocation, it runs well, many thanks!
|
|
|
|
|
It does? Have you checked the values in your stack?
You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.
|
|
|
|