You are on page 1of 5

for reverse of string without string reverse lib functn

#include<stdio.h>
#include<string.h>
void 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 <stdio.h>
#include <stdlib.h>
/* 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 <stdio.h>
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 <k,j>=0 ; i++,j--)
b[i] = a[j];
printf("%s",b);
return(0);

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

C array sort example


Posted on: February 5, 2009 at 12:00 AM
For sorting an array, we have used the qsort function. This function provides th
e 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 th
e 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 elemen
ts. For this we have passed two parameters (x and y ) which are pointers to elem
ents and will return an int value by comparing them. This method compares each p
air 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 sam
e position.
<stdlib.h> - This header file stands for standard library which includes functio
ns involving String, Memory, Environment, Sorting and Searching, Math and Multib
yte.
Here is the code:
ARRAYSOR.C
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
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<10; i++) {
printf("%d ", array[i]);
}
getch();
}
******
Bubble sort
The simplest sorting algorithm is bubble sort. The bubble sort works by iteratin
g down an array to be sorted from the first element to the last, comparing each

pair of elements and switching their positions if necessary. This process is rep
eated as many times as necessary, until the array is sorted. Since the worst cas
e 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 parti
ally 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<n; x++)
{
for(int y=0; y<n-1; y++)
{
if(array[y]>array[y+1])
{
int temp = array[y+1];
array[y+1] = array[y];
array[y] = temp;
}
}
}
Notice that this will always loop n times from 0 to n, so the order of this algo
rithm is O(n^2). This is both the best and worst case scenario because the code
contains no way of determining if the array is already in order.
A better version of bubble sort, known as modified bubble sort, includes a flag
that is set if an exchange is made after an entire pass over the array. If no ex
change is made, then it should be clear that the array is already in order becau
se no two elements need to be switched. In that case, the sort should end. The n
ew best case order for this algorithm is O(n), as if the array is already sorted
, then no exchanges are made. You can figure out the code yourself! It only requ
ires a few changes to the original bubble sort.
Selection sort and insertion sort are two simple sorting algorithms; they are mo
re often efficient than bubble sort, though they aren't the top of the class alg
orithmically.
Selection sort
Selection sort is the most conceptually simple of all the sorting algorithms. It
works by selecting the smallest (or largest, if you want to sort from big to sm
all) element of the array and placing it at the head of the array. Then the proc
ess is repeated for the remainder of the array; the next largest element is sele
cted and put into the next slot, and so on down the line.

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 orde
r), 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<n; x++)
{
int index_of_min = x;
for(int y=x; y<n; y++)
{
if(array[index_of_min]>array[y])
{
index_of_min = y;
}
}
int temp = array[x];
array[x] = array[index_of_min];
array[index_of_min] = temp;
}

The first loop goes from 0 to n, and the second loop goes from x to n, so it goe
s from 0 to n, then from 1 to n, then from 2 to n and so on. The multiplication
works out so that the efficiency is n*(n/2), though the order is still O(n^2).
Insertion Sort
Insertion sort does exactly what you would expect: it inserts each element of th
e array into its proper position, leaving progressively larger stretches of the
array sorted. What this means in practice is that the sort iterates down an arra
y, and the part of the array already covered is in order; then, the current elem
ent of the array is inserted into the proper position at the head of the array,
and the rest of the elements are moved down, using the space just vacated by the
element inserted as the final space.
Here is an example: for sorting the array the array 52314 First, 2 is inserted b
efore 5, resulting in 25314 Then, 3 is inserted between 2 and 5, resulting in 23
514 Next, one is inserted at the start, 12354 Finally, 4 is inserted between 3 a
nd 5, 12345
***********

You might also like