1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

my interview ques. in C

Discussion in 'C' started by rforravi, Aug 29, 2008.

  1. rforravi

    rforravi New Member

    Joined:
    Aug 24, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. hbilling

    hbilling New Member

    Joined:
    Aug 30, 2008
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Aug 30, 2008
  3. rforravi

    rforravi New Member

    Joined:
    Aug 24, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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!
     
  4. hbilling

    hbilling New Member

    Joined:
    Aug 30, 2008
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    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[COLOR=Red]++[/COLOR] = *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 a moderator: Aug 31, 2008
  5. rforravi

    rforravi New Member

    Joined:
    Aug 24, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    yep.. this i guess.. got it.. was better.
    Thx mate... 'll try to contact u if i dont get it l8r..

    Appreciate frnd! :)
    Cheerzz!
     

Share This Page