Posts

String functions in C

1. C Program to Find the Frequency of Characters in a String 2 . C Program to Reverse a String by Passing it to Function Source Code 1: #include <stdio.h> int main (){ char c [ 1000 ], ch ; int i , count = 0 ; printf ( "Enter a string: " ); gets ( c ); printf ( "Enter a characeter to find frequency: " ); scanf ( "%c" ,& ch ); for ( i = 0 ; c [ i ]!= '\0' ;++ i ) { if ( ch == c [ i ]) ++ count ; } printf ( "Frequency of %c = %d" , ch , count ); return 0 ; } Output Enter a string: md. ahsan arif Enter a frequency to find frequency: a Frequency of a = 3   2:   #include <stdio.h> #include <string.h> void Reverse ( char str []); int main (){ char str [ 100 ]; printf ( "Enter a string to reverse: " ); gets ( str ); Reverse ( str ); printf ( "Reversed string: " ); puts ( s

Sorting and Searching(Data Structures)

Click for: Sorting and Searching Using C

Array : Dynamic

/* A dynamically-allocated 1-D array */ #include <stdio.h> int main (void) { double* array; /* declare a pointer only */ int i, size; /* ask user for size of array */ printf ("How large do you want your array? "); scanf ("%d", &size); /* allocate the array in the heap */ array = (double *) calloc (size, sizeof(double)); /* printing the array for verification surprise! a dynamic array is automatically initialized with zeros! */ for (i = 0; i < size; ++i) printf ("%6.2lf", array[i]); /* freeing the memory allocation */ free (array); return (0); }

Array : Search

/* THE BASIC SEARCH ALGORITHM FOR A 1-D ARRAY */ #include <stdio.h> int search ( int arraytosearch[], int valuetosearch, int size ) { int i, found; /* initialize found at -1, if value not found, stays at -1 */ found = -1; /* search until found or until end of array */ i = 0; while (found<0 && i<size) { if (arraytosearch[i] == valuetosearch) found = i; /* I have found it! */ else i = i + 1; } return (found); } int main (void) { int x[] = {12,67,56,60,88,34,123}; int value = 60; int pos, i; pos = search (x, value, 7); if (pos >= 0) printf ("%d was found at position %d.\n", value, pos); else printf ("%d was not found in the array.\n", value); return (0); } Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments 60 was found at position 3.

Array : Adding Two Arrays

/* Problem: Add the corresponding values from two arrays of the same size. */ #include <stdio.h> /* the function that adds the two arrays a1 and a2. it "returns" a3 back */ void addarrays ( int a1[], int a2[], int a3[], int n ) { int i; /* do the adding of every corresponding cells */ for (i=0; i<n; ++i) a3[i] = a1[i] + a2[i]; } int main (void) { int x[] = {1,2,3,4}, i; int y[] = {10,20,30,40}; int z[4]; /* call the function */ addarrays (x, y, z, 4); /* print a report */ for (i=0; i<4; ++i) printf ("%3d", x[i]); printf ("\n + \n"); for (i=0; i<4; ++i) printf ("%3d", y[i]); printf ("\n-------------\n"); for (i=0; i<4; ++i) printf ("%3d", z[i]); return (0); }

Array : Filling Partially

/* Problem: This program partially fills an array from a file until the end of file (EOF). We get the actual number of data read */ #include <stdio.h> int array_from_file ( double a[], int size ) { int i; FILE* in; in = fopen ("data_array.dat", "r"); i=0; /* the first cell */ /* filling the array cell by cell */ /* until it is full or until the EOF */ while (i < 100 && fscanf (in, "%lf", &a[i]) != EOF) { i=i+1; } fclose (in); /* the actual number of values in the array */ return (i); } int main (void) { double array[100]; int actual_size, i; actual_size = array_from_file (array, 100); for (i=0; i < actual_size; ++i) printf ("%3.1lf ", array[i]); printf ("\nThe array contains %d values ", actual_size);

Array : With Pointer

/* Problem: This programs fills an array with a value submitted by the user. */ #include <stdio.h> /* array parameter can be expressed as a pointer */ /* *list is the same thing as list[] */ void fill_array ( int *list, int n, int in_value ) { int i; for (i=0; i<n; ++i) list[i] = in_value; } int main (void) { int x[100]; int i; /* &x[0] is the address of the x[0] */ /* which is the same thing as x */ fill_array (&x[0], 100, 5); /* printing the array for verification */ for (i=0; i<100; ++i) printf ("%d ", x[i]); return (0); }