Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   my interview ques. in C (http://www.go4expert.com/forums/interview-ques-c-t13403/)

rforravi 29Aug2008 21:22

my interview ques. in C
 
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

hbilling 30Aug2008 22:45

Re: my interview ques. in C
 
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;

}


rforravi 31Aug2008 08:39

Re: my interview ques. in C
 
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!

hbilling 31Aug2008 10:00

Re: my interview ques. in C
 
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.

rforravi 31Aug2008 22:19

Re: my interview ques. in C
 
yep.. this i guess.. got it.. was better.
Thx mate... 'll try to contact u if i dont get it l8r..

Appreciate frnd! :)
Cheerzz!


All times are GMT +5.5. The time now is 19:48.