Showing posts with label Tips. Show all posts
Showing posts with label Tips. Show all posts

Sunday, December 22, 2013

String manipulation

#include <stdio.h>
#include <string.h>
#define MAX 100


int main(){
    int I, J, K;
    char A[MAX][MAX];
    K = 0;
    while(gets(A[K]) != NULL && strcmp(A[K++], "0") != 0);
    K--;
    for(I = 0; I < strlen(A[0]); I++){
        for(J = 0; J < K; J++)
            printf("%c", A[J][I]);
           printf("\n");
    }  
    return 0;
}

Saturday, December 21, 2013

Swap two values

Write a C program to interchange values of two numbers.
#include <stdio.h>

int main(){
    int X, Y, Z;
  
    /*Using third variable Z*/
    X = 5;
    Y = 10;
    Z = X;
    X = Y;
    Y = Z;
    printf("X:%d Y:%d\n", X, Y);

    /*Without using third variable Z*/
    X = 5;
    Y = 10;
    X = X + Y;
    Y = X - Y;
    X = X - Y;
    printf("X:%d Y:%d\n", X, Y);
  
    /*Using bitwise operation*/
    X = 5;
    Y = 10;
    X = X ^ Y;
    Y = X ^ Y;
    X = X ^ Y;
    printf("X:%d Y:%d\n", X, Y);
  
    /*char swap using bitwise operation*/
    char x = 'x';
    char y = 'y';
    x = x ^ y;
    y = x ^ y;
    x = x ^ y;
    printf("x:%c y:%c\n", x, y);
    /*char swap using bitwise operation*/
    long a = 123456;
    long b = 654321;
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    printf("a:%ld b:%ld\n", a, b);

    return 0;
}