Basic C language for interviews by swetank

Embed Size (px)

DESCRIPTION

Simple and Fruitful

Citation preview

  • for reverse of string without string reverse lib functn

    #include#includevoid main(){char str[50],revstr[50];int i=0,j=0;clrscr();printf("Enter the string to be reversed : ");scanf("%s",str);for(i=strlen(str)-1;i>=0;i--){revstr[j]=str[i];j++;}revstr[j]='\0';printf("Input String : %s",str);printf("\nOutput String : %s",revstr);getch();}

    #include #include

    /* Reverse the string and put the result into allocated memory. */

    char * reverse (const char * string){ int length; int j; char * r;

    /* Get the length of the string. */

    for (length = 0; string[length]; length++) ;

    /* Get memory. */

    r = malloc (length + 1); if (! r) { fprintf (stderr, "Malloc failed.\n"); exit (EXIT_FAILURE); } /* Copy the bytes of the string. */

    for (j = 0; j < length; j++) { r[j] = string[length - j - 1]; } r[length] = '\0'; return r;}

  • int main (int argc, char ** argv){ int i;

    /* Reverse each string on the command line. */

    for (i = 1; i < argc; i++) { const char * string; char * r; string = argv[i]; r = reverse (string); printf ("The reverse of '%s' is '%s'.\n", string, r);

    /* Thanks for the memory. */

    free (r); } return 0;}

    The output of the example, when run like

    ./rs monkey celery madam

    looks like this:

    The reverse of 'monkey' is 'yeknom'.The reverse of 'celery' is 'yrelec'.The reverse of 'madam' is 'madam'.

    ***********************************************

    #include

    main (){int i=0,k=0,j;char a[30],b[30];printf("\nEnter the string:\n");gets(a);while(a[i++]!='\0')k++;for (i = 0,j=k-1; i =0 ; i++,j--)b[i] = a[j];

    printf("%s",b);return(0);

    }***********************************************************************************************************sorting in array in c

  • C array sort examplePosted on: February 5, 2009 at 12:00 AMFor sorting an array, we have used the qsort function. This function provides the implementation of quicksort algorithm to sort the elements of an array.C array sort example

    In this section, you will learn how to sort an array in C.

    For sorting an array, we have used the qsort function. This function provides the implementation of quicksort algorithm to sort the elements of an array.

    Syntax of the function:qsort(array, sizeof(array), sizeof(type), comparison_fn).

    In the example, we have created a comparison function sort to compare two elements. For this we have passed two parameters (x and y ) which are pointers to elements and will return an int value by comparing them. This method compares each pair of elements. This function can be declared as:int sort (const void * x, const void * y );

    (*(int*)x - *(int*)y)- The parameters x and y checks for each pair of elements considering the elements as x and y. If x is found greater than y, then x goes before y otherwise, x goes after y. In case if x = y, then it remains on the same position.

    - This header file stands for standard library which includes functions involving String, Memory, Environment, Sorting and Searching, Math and Multibyte.

    Here is the code:

    ARRAYSOR.C#include #include #include int array[] = { 90, 3, 33, 28, 80, 49, 8, 30, 36, 25 };int sort(const void *x, const void *y) { return (*(int*)x - *(int*)y);}void main() { clrscr(); int i; qsort(array, 10, sizeof(int), sort); for (i=0; i

  • pair of elements and switching their positions if necessary. This process is repeated as many times as necessary, until the array is sorted. Since the worst case scenario is that the array is in reverse order, and that the first element in sorted array is the last element in the starting array, the most exchanges that will be necessary is equal to the length of the array. Here is a simple example:

    Given an array 23154 a bubble sort would lead to the following sequence of partially sorted arrays: 21354, 21345, 12345. First the 1 and 3 would be compared and switched, then the 4 and 5. On the next pass, the 1 and 2 would switch, and the array would be in order.

    The basic code for bubble sort looks like this, for sorting an integer array:

    for(int x=0; x

  • Because a selection sort looks at progressively smaller parts of the array each time (as it knows to ignore the front of the array because it is already in order), a selection sort is slightly faster than bubble sort, and can be better than a modified bubble sort.

    Here is the code for a simple selection sort:

    for(int x=0; x