Practice Program







  • Program to Concate a String to other String.
  • Solution:
    void main()
    {
     int i,j,k;
    
    char s1[20],s2[20];
    
    printf("Enter the first string: ");
    gets(s1);
    
    printf("Enter second string: ");
    gets(s2);
    
    
    printf("first string is  ");
    puts(s1);
    
    printf("Second string is  ");
    puts(s2);
    
    
    
    
        for(i=0;s1[i] !='\0';i++)
        {
            //calculating the length of the string
        }
    k=0;
        for(j=i ; s2[k] != '\0';j++)
        {
            s1[j] = s2[k];
            k++;
        }
        s1[j] = '\0';
    
    printf("After concatting the second string to first: ");
    puts(s1);
    }
    

    Output1
    Enter the first string: hello

    Enter second string: world

    first string is hello
    Second string is world

    After concating the second string to first: helloworld

    Output2
    Enter the first string: programmer

    Enter second string: douts

    first string is programmer
    Second string is douts

    After concatting the second string to first: programmerdouts

    Output3
    Enter the first string: Good

    Enter second string: bye

    first string is Good
    Second string is bye

    After concating the second string to first: Good bye








    defining a function 'concate' which will concate a string

    void concate(char s1[],char s2[])
    {
        int i,j,k;
    
    
        for(i=0;s1[i] !='\0';i++)
        {
            //calculating the length of the string
        }
    k=0;
        for(j=i ; s2[k] != '\0';j++)
        {
            s1[j] = s2[k];
            k++;
        }
        s1[j] = '\0';
    
    }
    

    Using above define function 'concate()':
    void main()
    {
    
        char s[20] = "Good";
        char s2[20] = "bye";
    
        concate(s,s2);
        printf("Concated String is");
    puts(s1);
    }
        puts(s);
    }
    

    Output1
    Concated String is Goodbye

    Using above define function 'concate()':
    void main()
    {
    
        char s[20] = "programmer";
        char s2[20] = "douts";
    
        concate(s,s2);
    printf("Concated String is");
        puts(s);
    }
    

    Output2
    Concated String is programmerdouts

    Using above define function 'concate()':
    void main()
    {
    
        char s[20] = "hello";
        char s2[20] = "world";
    
        concate(s,s2);
         printf("Concated String is");
        puts(s);
    }
    

    Output3
    Concated String is helloworld