my interview ques. in C

rforravi's Avatar, Join Date: Aug 2008
Go4Expert Member
Hi guyz.
I've been asked another question in C.
I was given two different strings s1 and s2.
There's a string stored in s1 and string stored in s2 ( of our choice)
Assuming s2 contains enuf space, v need to concatenate both s1 and s2 in s2..
and print s2.. BUTTT........in the display v need to see only the string tht is contained in s2..
The string in s1 shud not be visible but shud be contained in the concatenated strng s2.

And above all.........
v shud not use any of the string functions.. eg: strcat, strlen().. etc etc.. shud not be used
0
hbilling's Avatar, Join Date: Aug 2008
Light Poster
Hi,
I am not sure what you actually intended to say! but here's a solution. Please see if it helps:
Code:
#include <stdio.h>

int main() {
    char str1[] = "aman";
    char str2[20] = "Vini";
    
    char *ptr2 = str2;
    char *ptr1 = str1;
    int count =0;
    int counter =0;
    while( '\0' != *ptr2 ){
           ptr2++;
           count++;
    }
    
    while( '\0' != *ptr1 ){
           *ptr2 = *ptr1++;
    }

    *ptr2 = '\0';
    
    printf("str2 = %s, Length of first string = %d", str2, count);
    
    ptr2 = str2; // Resetting the pointer
    
    while(counter < count){
                  printf("%c", *ptr2++ );
                  counter++;
    }
    
    return 0;

}

Last edited by shabbir; 30Aug2008 at 23:41.. Reason: Code block
0
rforravi's Avatar, Join Date: Aug 2008
Go4Expert Member
well.. i feel I've understood.. ( not really sure tho' )..

But.. cud u xplain wht xctly r u doing there?? Where r u concatenating both of them?
And does s2 contain s1 also?
Apologies friend.. if I'm wrong!
0
hbilling's Avatar, Join Date: Aug 2008
Light Poster
Hi,
First of all apologies for a small but screwing mistake in the program! and once again no comments which makes it dfifficult! Wasnt feeling well so posted in a hurry! Here are a few comments and an extra addition in RED . Thanks a lot for pointing out the mistake.
Code:
#include <stdio.h>

int main() {
    char str1[] = "aman";   
    char str2[20] = "Vini";
    
    char *ptr2 = str2;
    char *ptr1 = str1;
    int count =0;
    int counter =0;
    while( '\0' != *ptr2 ){   
           ptr2++;
           count++;  // Counting the number of characters in the string here.
    }
    
    while( '\0' != *ptr1 ){
           *ptr2++ = *ptr1++; // Here we are actually concatenating 
                                                                           // the string.
    }

    *ptr2 = '\0';
    
    printf("str2 = %s, Length of first string = %d", str2, count); // This will show the   
                                                                                          //concatenated string.
    
    ptr2 = str2; // Resetting the pointer
    
// Now belolw is a very crude way of printing just "Vini" from "Viniaman". Not sure here if 
// it serves ur purpose.
    while(counter < count){
                  printf("%c", *ptr2++ );
                  counter++;
    }
    
    return 0;

}
Please let me know if you have any doubts on that.

Last edited by shabbir; 31Aug2008 at 10:26.. Reason: Code block
0
rforravi's Avatar, Join Date: Aug 2008
Go4Expert Member
yep.. this i guess.. got it.. was better.
Thx mate... 'll try to contact u if i dont get it l8r..

Appreciate frnd!
Cheerzz!