├── FindAll.c ├── README.md ├── arrays ├── BubbleSort.c ├── ColSummary.c ├── Compare Arrays ├── InterchangeRows.c ├── Interchange_array.c ├── Marks.c ├── Matrix Addition ├── MatrixMultiplication.c ├── MatrixTranspose,c ├── Search Double Dimension Array ├── Search in Sorted Array ├── Sorting Demo ├── array_delete.c ├── array_insert.c ├── array_reverse_search.c ├── big_small_array.c ├── largest_value_in_2d_array.c ├── reverse_array.c ├── reverse_array_inplace.c ├── row_total.c ├── search_2d_array.c ├── search_2d_use_goto.c ├── search_double_dim_array.c ├── search_two_dim_array.c ├── search_two_dim_array_use_goto.c ├── selection_sort.c └── sort_2d_array.c ├── basics ├── Net Salary Calculation ├── net_amount_calculation.c ├── net_amount_with_discount_and_tax.c ├── net_price.c ├── net_salary.c ├── net_salary_calculation.c └── wage_calculation.c ├── chars ├── Password check ├── ValidatePin.c └── validate_password.c ├── conditional └── no_days.c ├── files ├── Binary File Demo with Books ├── Binary File Examples ├── CommonLines.c ├── CommonLines2.c ├── ContactsMenu.c ├── ConvertFileToUpper.c ├── CopyFile.c ├── Courses.c ├── Customers Operations ├── Display Files With Line Numbers ├── Dynamic and Args Demo ├── Employee Operations ├── EncDec.c ├── Encrypt.c ├── File Utilities ├── List of Employees ├── Menu Template ├── Merge.c ├── NonDigitCopyFile.c ├── Product Database Using Binary Files ├── Products Binary File ├── Products Binary File Example ├── Read Names From File ├── Remove Blank Lines ├── RemoveBlankLines.c ├── ShowFileWithLineNumbers.c ├── Students Data Process ├── Students Database Operations ├── UpdateTime.c ├── Valdiate Mobile Numbers ├── Write Names to File ├── Write and read marks ├── char_count.c ├── compare_files.c ├── customers_manager.c ├── delete_marks_from_file.c ├── file_char_types_count.c ├── file_utilities.cs ├── manager_products.c ├── marks_manager.c ├── print_file_uppercase.c ├── print_long_names.c ├── print_nonblank_lines.c ├── printfile.c ├── printfiles.c ├── products_manager.c ├── read_contacts.c ├── readstudents.c ├── remove_all_blank_lines.c ├── remove_blank_lines.c ├── remove_blank_lines_from_file.c ├── removeblanklines2.c ├── search_contacts.c ├── search_file.c ├── search_product.c ├── search_string_using_cmd_args.c ├── search_update_mobile.c ├── show_source_code.c ├── students_menu.c ├── write_contacts_to_file.c └── writestudents.c ├── linkedlist ├── ConvertFileToUpperWithLinkedList ├── Display Names Using Linked List ├── Linked List of Names from File ├── Linked List with Large names and short names ├── Marks List ├── Products_Linked_List.c ├── Reverse print using stack ├── SortListByPointers.c ├── Sorted Linked List ├── delete_marks_from_file_with_list.c ├── manage_students.c ├── marks_linked_list.c ├── remove_blank_lines_with_linked_list.c ├── sort names using linked list ├── sort_numbers_from_file.c ├── stack.c ├── stack_data_structure.c ├── std_of_marks.c └── std_of_marks_from_file.c ├── loops ├── ArmStrongNumber.c ├── FlyodTriangle.c ├── GCD.c ├── Perfect.c ├── Prime.c ├── Strong Number ├── get_single_digit_sum.c ├── guess_number.c ├── palindrome.c ├── print_digits_in_reverse.c ├── single_digit_sum.c └── table.c ├── strings ├── Count common chars in two string ├── Largest_word_in_string.c ├── Print Digits in Words ├── concate_names.c ├── largest_word.c ├── number_to_words.c ├── print_number_in_words.c ├── print_words_reverse.c └── string_count.c ├── structs ├── Contacts.c ├── DeleteBook.c ├── Largest Time Of 10 Times ├── Month_sales.c ├── Print Total Seconds ├── Sort Struct Time Array ├── biggest_time.c ├── digits_to_words.c ├── largest_time.c ├── sort_students_struct.c ├── struct time operations └── time_operations.c └── upload.bat /FindAll.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main(int argc, char * argv[]) 4 | { 5 | FILE * fp; 6 | char line[100], * p; 7 | int i; 8 | 9 | if(argc < 3) 10 | { 11 | printf("Usage : find file ...\n"); 12 | printf("\n is the string that you want to search for"); 13 | printf("\nfile... is one or more files in which you want to search for string"); 14 | printf("\n\nExample : find this myfile1.txt myfile2.txt\n\n"); 15 | exit(0); 16 | } 17 | 18 | for(i=2; i < argc; i ++) 19 | { 20 | fp = fopen(argv[i],"r"); 21 | if (fp == NULL) 22 | { 23 | printf("\nSorry! File %s could not be opened!\n", argv[i]); 24 | continue; 25 | } 26 | 27 | printf("\n-------------------------"); 28 | printf("\n%s",argv[i]); 29 | printf("\n-------------------------\n\n"); 30 | 31 | while(1) 32 | { 33 | p = fgets(line,100,fp); 34 | if (p == NULL) 35 | break; 36 | 37 | if (strstr(line,argv[1]) != NULL) 38 | printf("%s",line); 39 | } 40 | 41 | fclose(fp); 42 | } // for 43 | 44 | } 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # c_language 2 | C Language Programs 3 | -------------------------------------------------------------------------------- /arrays/BubbleSort.c: -------------------------------------------------------------------------------- 1 | /* Bubble Sort */ 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int array[10], n, i, j, temp, swapped; 9 | 10 | // Initialize random seed 11 | srand(time(0)); 12 | 13 | for (i=0;i < 10;i++) 14 | array[i]=rand()%100; 15 | 16 | printf("Array before sorting : "); 17 | 18 | for (i=0;i < 10;i++) 19 | printf("%5d", array[i]); 20 | 21 | for (i=0;i < 9;i++) 22 | { 23 | swapped = 0; // False 24 | for (j=0;j < 9-i;j++) 25 | { 26 | if (array[j] > array[j+1]) 27 | { 28 | temp = array[j]; 29 | array[j] = array[j+1]; 30 | array[j+1] = temp; 31 | swapped = 1; // True 32 | } 33 | } 34 | 35 | // if no swapping was done then array is sorted and quit outer loop 36 | if (!swapped) 37 | break; 38 | } 39 | 40 | printf("\nArray after sorting : "); 41 | 42 | for (i=0;i < 10;i++) 43 | printf("%5d", array[i]); 44 | } 45 | -------------------------------------------------------------------------------- /arrays/ColSummary.c: -------------------------------------------------------------------------------- 1 | 2 | main() 3 | { 4 | int a[6][5]; 5 | int i,j; 6 | 7 | // initialize last row 8 | for(i = 0; i < 5; i ++) 9 | a[5][i] = 0; 10 | 11 | // init random seed 12 | srand(time(0)); 13 | 14 | // fill array with random numbers and also get totals to last row 15 | for(i=0; i < 5; i ++) 16 | { 17 | 18 | for(j=0; j < 5; j++) 19 | { 20 | 21 | a[i][j] = rand() % 100; 22 | a[5][j] += a[i][j]; 23 | printf("%d\t", a[i][j]); 24 | } 25 | printf("\n"); 26 | } 27 | 28 | 29 | // print last row, which contains totals 30 | for(i = 0; i < 5; i ++) 31 | printf("%d\t", a[5][i]); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /arrays/Compare Arrays: -------------------------------------------------------------------------------- 1 | // Compare two arrays and displays where the don't match, otherwise displays message saying they are same 2 | #include 3 | 4 | main() 5 | { 6 | int i, mismatch = 0; 7 | 8 | int a[] = {10,20,30,35,50}; 9 | int b[] = {10,20,30,40,50}; 10 | 11 | for(i=0; i < 5; i++) 12 | { 13 | if (a[i] != b[i]) 14 | { 15 | printf("Elements didn't match at %d position",i); 16 | mismatch = 1; // true 17 | break; 18 | } 19 | } 20 | 21 | if(!mismatch) 22 | printf("Arrays are same!"); 23 | } 24 | -------------------------------------------------------------------------------- /arrays/InterchangeRows.c: -------------------------------------------------------------------------------- 1 | 2 | main() 3 | { 4 | int a[6][6]; 5 | int i,j,temp; 6 | 7 | srand(time(0)); 8 | for (i = 0; i < 6; i ++) 9 | { 10 | for(j=0; j < 6; j ++) 11 | { 12 | a[i][j] = rand() % 100; 13 | printf("%5d", a[i][j] ); 14 | } 15 | printf("\n"); 16 | } 17 | 18 | 19 | for (i = 0; i < 3; i ++) 20 | { 21 | for(j=0; j < 6; j ++) 22 | { 23 | temp = a[i][j] ; 24 | a[i][j] = a[5-i][j]; 25 | a[5-i][j] = temp; 26 | } 27 | } 28 | 29 | printf("\nAfter Interchange\n"); 30 | 31 | for (i = 0; i < 6; i ++) 32 | { 33 | for(j=0; j < 6; j ++) 34 | { 35 | printf("%5d", a[i][j] ); 36 | } 37 | printf("\n"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /arrays/Interchange_array.c: -------------------------------------------------------------------------------- 1 | 2 | main() { 3 | int a[12],i,j,temp; 4 | 5 | srand(time(0)); 6 | printf("\nOriginal Array\n\n"); 7 | for(i = 0; i < 12; i ++) 8 | { 9 | a[i] = rand() % 100; 10 | printf("%d ", a[i]); 11 | } 12 | 13 | printf("\n\nPress any key to continue..."); 14 | getch(); 15 | 16 | for(i = 0, j = 11; i < 6; i ++, j --) 17 | { 18 | temp = a[i]; 19 | a[i] = a[j]; 20 | a[j] = temp; 21 | } 22 | 23 | 24 | printf("\n\nAfter Interchange\n\n"); 25 | for(i = 0; i < 12; i ++) 26 | { 27 | printf("%d ", a[i]); 28 | } 29 | 30 | printf("\n\nPress any key to continue..."); 31 | getch(); 32 | } 33 | -------------------------------------------------------------------------------- /arrays/Marks.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | int marks[10]; 6 | int i; 7 | 8 | srand( time(NULL)); 9 | for(i=0; i < 10; i ++) 10 | { 11 | marks[i] = rand() % 100; 12 | } 13 | 14 | 15 | printf("\nPassed List\n"); 16 | 17 | for(i=0; i < 10; i ++) 18 | { 19 | if( marks[i] >= 50) 20 | printf("%2d %2d\n", i + 1, marks[i]); 21 | } 22 | 23 | printf("\nFailed List\n"); 24 | 25 | for(i=0; i < 10; i ++) 26 | { 27 | if( marks[i] < 50) 28 | printf("%2d %2d\n", i + 1, marks[i]); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /arrays/Matrix Addition: -------------------------------------------------------------------------------- 1 | /* Matrix Addition */ 2 | 3 | int main() 4 | { 5 | int a[3][3], b[3][3], i, j; 6 | 7 | srand(time(0)); 8 | 9 | // Fill and print matrix A 10 | printf("\nMatrix A\n"); 11 | for(i=0;i<3;i++) 12 | { 13 | for(j=0;j<3;j++) 14 | { 15 | a[i][j]=rand()%10; 16 | printf("%5d", a[i][j]); 17 | } 18 | printf("\n"); 19 | } 20 | 21 | // fill and print matrix B 22 | printf("\nMatrix B\n"); 23 | for(i=0;i<3;i++) 24 | { 25 | for(j=0;j<3;j++) 26 | { 27 | b[i][j]=rand()%10; 28 | printf("%5d", b[i][j]); 29 | } 30 | printf("\n"); 31 | } 32 | 33 | printf("\nMatrix Addition [A+B]\n"); 34 | 35 | for(i=0;i<3;i++) 36 | { 37 | for(j=0;j<3;j++) 38 | { 39 | printf("%5d", a[i][j] + b[i][j]); 40 | } 41 | printf("\n"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /arrays/MatrixMultiplication.c: -------------------------------------------------------------------------------- 1 | /* Matrix Multiplication - Praneeth Pragada*/ 2 | #include 3 | #include 4 | int main() 5 | { 6 | int a[3][3], b[3][3], c[3][3], i, j, k, sum=0; 7 | srand((unsigned int)time(NULL)); 8 | 9 | for(i=0;i<3;i++) 10 | { 11 | for(j=0;j<3;j++) 12 | { 13 | a[i][j]=rand()%10; 14 | } 15 | } 16 | 17 | printf("Matrix A is :\n"); 18 | 19 | for(i=0;i<3;i++) 20 | { 21 | for(j=0;j<3;j++) 22 | { 23 | printf("%3d ",a[i][j]); 24 | } 25 | printf("\n"); 26 | } 27 | 28 | for(i=0;i<3;i++) 29 | { 30 | for(j=0;j<3;j++) 31 | { 32 | b[i][j]=rand()%10; 33 | } 34 | } 35 | 36 | printf("Matrix B is :\n"); 37 | 38 | for(i=0;i<3;i++) 39 | { 40 | for(j=0;j<3;j++) 41 | { 42 | printf("%3d ", b[i][j]); 43 | } 44 | printf("\n"); 45 | } 46 | 47 | for(i=0;i<3;i++) 48 | { 49 | for(j=0;j<3;j++) 50 | { 51 | sum=0; 52 | 53 | for(k=0;k<3;k++) 54 | { 55 | sum+=a[i][k]*b[k][j]; 56 | } 57 | c[i][j]=sum; 58 | } 59 | } 60 | 61 | printf("\nMatrix[A*B]\n"); 62 | 63 | for(i=0;i<3;i++) 64 | { 65 | for(j=0;j<3;j++) 66 | { 67 | printf("%3d ", c[i][j]); 68 | } 69 | printf("\n"); 70 | } 71 | 72 | 73 | } 74 | -------------------------------------------------------------------------------- /arrays/MatrixTranspose,c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int a[3][3], i, j; 6 | srand((unsigned int)time(NULL)); 7 | 8 | for(i=0;i<3;i++) 9 | { 10 | for(j=0;j<3;j++) 11 | { 12 | a[i][j]=rand()%10; 13 | } 14 | } 15 | 16 | printf("Matrix A is :\n"); 17 | 18 | for(i=0;i<3;i++) 19 | { 20 | for(j=0;j<3;j++) 21 | { 22 | printf("%2d ",a[i][j]); 23 | } 24 | printf("\n"); 25 | } 26 | 27 | printf("Transpose of matrix A is :\n"); 28 | 29 | for(i=0;i<3;i++) 30 | { 31 | for(j=0;j<3;j++) 32 | { 33 | printf("%2d ",a[j][i]); 34 | } 35 | printf("\n"); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /arrays/Search Double Dimension Array: -------------------------------------------------------------------------------- 1 | // Seach a 5 x 5 array 2 | 3 | main() 4 | { 5 | int a[5][5]; 6 | int r,c, num, found = 0; 7 | 8 | srand(time(0)); 9 | for(r =0 ; r < 5; r++) 10 | { 11 | for (c =0; c < 5; c ++) 12 | { 13 | a[r][c] = rand() % 100; 14 | printf("%5d", a[r][c]); 15 | } 16 | printf("\n"); 17 | } 18 | 19 | printf("\nEnter number :"); 20 | scanf("%d",&num); 21 | 22 | for(r =0 ; r < 5 ; r++) 23 | { 24 | for (c =0; c < 5; c ++) 25 | { 26 | if (a[r][c] == num) 27 | { 28 | printf("\nFound at %d row %d col",r,c); 29 | found = 1; 30 | break; 31 | } 32 | } 33 | 34 | if (found) 35 | break; 36 | } 37 | 38 | if (!found) 39 | printf("\nNot Found!"); 40 | } 41 | -------------------------------------------------------------------------------- /arrays/Search in Sorted Array: -------------------------------------------------------------------------------- 1 | /* search in a sorted array */ 2 | #include 3 | 4 | main() 5 | { 6 | int a[10]; 7 | int i,j, temp, num, found; 8 | 9 | srand(time(NULL)); 10 | for(i=0; i <10; i ++) 11 | a[i] = rand() % 100; 12 | 13 | printf("\nArray Before Sorting\n"); 14 | for(i=0; i <10; i ++) 15 | printf("%5d",a[i]); 16 | 17 | for(i=0; i < 9; i++) 18 | { 19 | for(j=i+1; j < 10; j++ ) 20 | { 21 | if ( a[i] > a[j]) 22 | { 23 | temp = a[i]; 24 | a[i] = a[j]; 25 | a[j] = temp; 26 | } 27 | } 28 | } /* end of i loop */ 29 | 30 | 31 | printf("\nArray after Sorting\n"); 32 | for(i=0; i <10; i ++) 33 | printf("%5d",a[i]); 34 | 35 | printf("\nEnter number to search : "); 36 | scanf("%d",&num); 37 | 38 | found = 0; 39 | for(i=0; i <10 ; i ++) 40 | { 41 | if (a[i] == num) 42 | { 43 | found = 1; 44 | break; 45 | } 46 | else 47 | if (a[i] > num) 48 | break; 49 | } 50 | 51 | if(found) 52 | printf("\nFound at %d",i); 53 | else 54 | printf("\nNot found! Exited after %d comparisons",i + 1); 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /arrays/Sorting Demo: -------------------------------------------------------------------------------- 1 | 2 | main() 3 | { 4 | int a[5], i,j,k, temp; 5 | 6 | 7 | srand(time(0)); 8 | for(i=0; i < 5; i ++) 9 | { 10 | a[i] = rand() % 100; 11 | printf("%5d", a[i]); 12 | } 13 | 14 | 15 | printf("\nSorting array. Press any key to continue..."); 16 | getch(); 17 | 18 | for(i = 0; i < 4; i ++) 19 | { 20 | for (j=i + 1; j < 5; j ++) 21 | { 22 | if(a[i] > a[j]) 23 | { 24 | temp = a[i]; 25 | a[i] = a[j]; 26 | a[j] = temp; 27 | } 28 | } 29 | // display array after each round 30 | printf("\nEnd of round %d. Press a key to continue\n", i+1); 31 | getch(); // wait for a key 32 | // 33 | for(k = 0; k < 5; k++) 34 | { 35 | printf("%5d", a[k]); 36 | } 37 | printf("\n"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /arrays/array_delete.c: -------------------------------------------------------------------------------- 1 | // deletion of an element in an array 2 | main() 3 | { 4 | int a[10]; 5 | int i,pos; 6 | 7 | srand(time(0)); 8 | // fill it and display array 9 | printf("\nBefore deletion\n"); 10 | for(i=0; i < 10; i++) 11 | { 12 | a[i] = rand()%100; 13 | printf("%d\t", a[i]); 14 | } 15 | printf("\nEnter position [0-9] : "); 16 | scanf("%d",&pos); 17 | 18 | // delete by pusing elements to right 19 | for(i=pos; i < 9;i++) 20 | { 21 | a[i] = a[i+1]; 22 | } 23 | 24 | a[9]=0; 25 | 26 | printf("\nAfter deletion\n"); 27 | for(i=0; i < 10; i++) 28 | { 29 | 30 | printf("%d\t", a[i]); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /arrays/array_insert.c: -------------------------------------------------------------------------------- 1 | // insertion of an element in an array 2 | 3 | main() 4 | { 5 | int a[10]; 6 | int i,pos,num; 7 | 8 | srand(time(0)); 9 | // fill it and display array 10 | printf("\nBefore Insertion\n"); 11 | for(i=0; i < 9; i++) 12 | { 13 | a[i] = rand()%100; 14 | printf("%d\t", a[i]); 15 | } 16 | 17 | printf("\nEnter position :[0-9]"); 18 | scanf("%d",&pos); 19 | 20 | printf("\nEnter number :"); 21 | scanf("%d",&num); 22 | 23 | // insert by pusing elements to right 24 | 25 | for(i=8; i>=pos;i--) 26 | { 27 | a[i+1] = a[i]; 28 | } 29 | 30 | a[pos]=num; 31 | 32 | printf("\nAfter Insertion\n"); 33 | for(i=0; i < 10; i++) 34 | { 35 | 36 | printf("%d\t", a[i]); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /arrays/array_reverse_search.c: -------------------------------------------------------------------------------- 1 | // Find position of a number from end of array 2 | 3 | main() 4 | { 5 | int a[10],n,i; 6 | 7 | srand(time(0)); 8 | for (i=0; i < 10; i ++) 9 | { 10 | a[i] = rand() % 50; 11 | printf("%d ",a[i]); 12 | } 13 | 14 | printf("\nEnter number :"); 15 | scanf("%d",&n); 16 | 17 | //Search in reverse order 18 | for(i = 9 ; i >= 0; i --) 19 | { 20 | if (a[i] == n) 21 | { 22 | printf("\nFound at %d",i); 23 | exit(0); // stop program 24 | } 25 | } 26 | printf("\nNumber not found!"); 27 | } 28 | -------------------------------------------------------------------------------- /arrays/big_small_array.c: -------------------------------------------------------------------------------- 1 | // A function returns biggest and smallest values in an array using pass by reference 2 | #include 3 | 4 | void big_small(int a[], int * b, int *s) 5 | { 6 | int i; 7 | 8 | *b = *s = a[0]; 9 | for(i=1; i < 10; i ++) 10 | { 11 | if (a[i] > *b) 12 | *b = a[i]; 13 | else 14 | if( a[i] < *s) 15 | *s = a[i]; 16 | } 17 | } 18 | 19 | main() 20 | { 21 | int a [10],i,b,s; 22 | 23 | srand(time(0)); 24 | for(i=0; i <10; i ++) { 25 | a[i] = rand()% 100; 26 | printf("%d ",a[i]); 27 | } 28 | 29 | big_small(a,&b,&s); 30 | printf("\nBig : %d small : %d", b,s); 31 | } 32 | -------------------------------------------------------------------------------- /arrays/largest_value_in_2d_array.c: -------------------------------------------------------------------------------- 1 | // Largest value and its position in 5 X 5 array 2 | #include 3 | 4 | main() 5 | { 6 | int a[5][5],j,i,lr,lc,largest; 7 | 8 | lr = lc = largest = 0; 9 | srand(time(0)); 10 | for(i=0; i < 5; i ++) 11 | { 12 | for(j=0; j < 5; j ++) 13 | { 14 | a[i][j] = rand() % 100; 15 | printf("%5d", a[i][j]); 16 | if (a[i][j] > largest) 17 | { 18 | largest = a[i][j]; 19 | lr = i; 20 | lc = j; 21 | } 22 | } 23 | printf("\n"); 24 | } 25 | 26 | printf("\nLargest value is %d at (%d,%d)\n",largest,lr,lc); 27 | } 28 | -------------------------------------------------------------------------------- /arrays/reverse_array.c: -------------------------------------------------------------------------------- 1 | // deletion of an element in an array 2 | main() 3 | { 4 | int a[10], b[10]; 5 | int i; 6 | 7 | srand(time(0)); 8 | // fill it and display array 9 | printf("\nBefore Reversing Array \n"); 10 | for(i=0; i < 10; i++) 11 | { 12 | a[i] = rand()%100; 13 | printf("%d\t", a[i]); 14 | } 15 | 16 | 17 | // copy from a to b in reverse 18 | for(i=0; i < 10;i++) 19 | { 20 | b[9-i] = a[i]; 21 | } 22 | 23 | // copy b to a and print 24 | printf("\nAfter Reversing Array \n"); 25 | for(i=0; i < 10; i++) 26 | { 27 | a[i] = b[i]; 28 | printf("%d\t", a[i]); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /arrays/reverse_array_inplace.c: -------------------------------------------------------------------------------- 1 | // Reversing array without any temp array using swapping 2 | main() 3 | { 4 | int a[10]; 5 | int i, temp; 6 | 7 | srand(time(0)); 8 | // fill it and display array 9 | printf("\nBefore Reversing \n"); 10 | for(i=0; i < 10; i++) 11 | { 12 | a[i] = rand()%100; 13 | printf("%d\t", a[i]); 14 | } 15 | 16 | 17 | // Reverse array by swapping first 5 with last 5 elements 18 | for(i=0; i < 5 ; i++) 19 | { 20 | temp = a[i]; 21 | a[i] = a[9-i]; 22 | a[9-i] = temp; 23 | } 24 | 25 | printf("\nAfter Reversing\n"); 26 | for(i=0; i < 10; i++) 27 | { 28 | 29 | printf("%d\t", a[i]); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /arrays/row_total.c: -------------------------------------------------------------------------------- 1 | // 5 * 5 Array 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | void main() 8 | { 9 | int a[5][5]; 10 | int i,j, total; 11 | 12 | srand(time(0)); 13 | for(i = 0; i < 5; i ++) 14 | { 15 | total = 0; 16 | for (j = 0; j < 5; j ++) 17 | { 18 | a[i][j] = rand() % 50; 19 | printf("%5d", a[i][j]); 20 | total += a[i][j]; 21 | } 22 | printf("%5d\n", total); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /arrays/search_2d_array.c: -------------------------------------------------------------------------------- 1 | // Search in 2d array 2 | #include 3 | 4 | main() 5 | { 6 | int a[5][5],j,i,num, found=0; 7 | 8 | srand(time(0)); 9 | for(i=0; i < 5; i ++) 10 | { 11 | for(j=0; j < 5; j ++) 12 | { 13 | a[i][j] = rand() % 100; 14 | printf("%5d", a[i][j]); 15 | } 16 | printf("\n"); 17 | } 18 | 19 | printf("\nEnter search number :"); 20 | scanf("%d",&num); 21 | 22 | for(i=0; i < 5; i ++) 23 | { 24 | for(j=0; j < 5; j ++) 25 | { 26 | if(a[i][j] == num) 27 | { 28 | found = 1; 29 | break; 30 | } 31 | } 32 | // break outerloop if number is found 33 | if(found) 34 | break; 35 | } 36 | 37 | if(found) 38 | printf("\nFound at %d,%d\n",i,j); 39 | else 40 | printf("\nSorry! Number not found!\n"); 41 | } 42 | -------------------------------------------------------------------------------- /arrays/search_2d_use_goto.c: -------------------------------------------------------------------------------- 1 | // Search in Matrix of 5 X 5 and use goto to come out of nested loops 2 | #include 3 | 4 | void main() 5 | { 6 | int a[5][5]; 7 | int r,c, num, found = 0; 8 | 9 | srand(time(0)); 10 | for (r = 0; r < 5 ; r++ ) 11 | { 12 | for (c = 0; c < 5 ; c++ ) 13 | { 14 | a[r][c] = rand() % 100; 15 | printf("%5d", a[r][c]); 16 | } 17 | printf("\n"); 18 | } 19 | 20 | printf("\nEnter number : "); 21 | scanf("%d",&num); 22 | 23 | for (r = 0; r < 5; r++ ) 24 | { 25 | for (c = 0; c < 5; c++ ) 26 | { 27 | if( a[r][c] == num) 28 | { 29 | printf("Found at %d,%d\n",r,c); 30 | found = 1; 31 | goto showresult; 32 | } 33 | } 34 | } 35 | 36 | showresult: 37 | 38 | if(!found) 39 | printf("Sorry! Not found!"); 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /arrays/search_double_dim_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | 6 | int a[5][5]; 7 | int i,j,num; 8 | 9 | srand(time(0)); 10 | 11 | for(i=0; i <5; i ++) 12 | { 13 | for (j=0; j < 5; j ++) 14 | { 15 | a[i][j] = rand() % 100; 16 | printf("%d\t", a[i][j]); 17 | } 18 | printf("\n"); 19 | } 20 | 21 | printf("Enter number :"); 22 | scanf("%d",&num); 23 | 24 | for(i=0; i <5; i ++) 25 | { 26 | for (j=0; j < 5; j ++) 27 | { 28 | if ( a[i][j] == num) 29 | { 30 | printf("\nFound at %d x %d\n", i, j); 31 | goto end; 32 | } 33 | } 34 | } 35 | printf("\nSorry! Number not found!"); 36 | 37 | end: 38 | printf("\n"); 39 | } 40 | -------------------------------------------------------------------------------- /arrays/search_two_dim_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | 6 | int a[5][5]; 7 | int i,j,num; 8 | 9 | srand(time(0)); 10 | 11 | for(i=0; i <5; i ++) 12 | { 13 | for (j=0; j < 5; j ++) 14 | { 15 | a[i][j] = rand() % 100; 16 | printf("%d\t", a[i][j]); 17 | } 18 | printf("\n"); 19 | } 20 | 21 | printf("Enter number :"); 22 | scanf("%d",&num); 23 | 24 | int found = 0; 25 | for(i=0; i <5 && !found; i ++) 26 | { 27 | for (j=0; j < 5; j ++) 28 | { 29 | if ( a[i][j] == num) 30 | { 31 | printf("\nFound at %d x %d\n", i, j); 32 | found = 1; 33 | break; 34 | } 35 | } 36 | } 37 | if (!found) 38 | printf("\nSorry! Number not found!"); 39 | 40 | } 41 | -------------------------------------------------------------------------------- /arrays/search_two_dim_array_use_goto.c: -------------------------------------------------------------------------------- 1 | // Search for a number in 5X5 array and use goto to break out of two loops 2 | 3 | # include 4 | 5 | main() 6 | { 7 | int a[5][5]; 8 | int i,j,num; 9 | 10 | srand(time(0)); 11 | for(i=0; i < 5; i ++) 12 | { 13 | for(j=0; j < 5; j++) 14 | { 15 | a[i][j] = rand()%50; 16 | printf("%5d", a[i][j]); 17 | } 18 | printf("\n"); 19 | } 20 | 21 | printf("\nEnter a number :"); 22 | scanf("%d", &num); 23 | 24 | for(i=0; i < 5; i ++) 25 | { 26 | for(j=0; j < 5; j++) 27 | { 28 | if (a[i][j] == num) { 29 | printf("\nFound at : %d,%d",i,j); 30 | goto over; 31 | } 32 | } 33 | } 34 | 35 | printf("\nNot found"); 36 | over: 37 | printf("\n"); 38 | } 39 | -------------------------------------------------------------------------------- /arrays/selection_sort.c: -------------------------------------------------------------------------------- 1 | // Selection sort 2 | 3 | #include 4 | 5 | main() 6 | { 7 | int a[10]; 8 | int i,temp,min_pos,j; 9 | 10 | srand(time(0)); 11 | printf("Original Array\n\n"); 12 | for(i=0; i < 10; i ++) 13 | { 14 | a[i] = rand() %100; 15 | printf("%4d",a[i]); 16 | } 17 | 18 | // sort array 19 | for(i=0; i < 9; i ++) 20 | { 21 | min_pos = i; 22 | for(j=i + 1; j < 10; j ++) 23 | { 24 | if (a[min_pos] > a[j]) 25 | min_pos = j; 26 | } 27 | 28 | // Interchange ith element with min_pos element 29 | if(i != min_pos) 30 | { 31 | temp = a[i]; 32 | a[i] = a[min_pos]; 33 | a[min_pos] = temp; 34 | } 35 | 36 | } 37 | 38 | printf("\n\nAfter Sorting\n\n"); 39 | for(i=0; i < 10; i ++) 40 | { 41 | printf("%4d",a[i]); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /arrays/sort_2d_array.c: -------------------------------------------------------------------------------- 1 | // Sorting 5x5 array using 25 elements array 2 | 3 | # include 4 | 5 | main() 6 | { 7 | int a[5][5], sd[25]; 8 | int i,j,p,temp; 9 | 10 | 11 | srand(time(0)); 12 | printf("\nOriginal Array\n"); 13 | for(i=0; i < 5; i ++) 14 | { 15 | for(j = 0; j < 5; j ++) 16 | { 17 | a[i][j] = rand() % 100; 18 | printf("%5d", a[i][j]); 19 | } 20 | printf("\n"); 21 | } 22 | 23 | // copy 2d array into 1d 24 | p = 0; 25 | for(i=0; i < 5; i ++) 26 | { 27 | for(j = 0; j < 5; j ++) 28 | { 29 | sd[p] = a[i][j]; 30 | p++; 31 | } 32 | } 33 | 34 | // Sort 1d array 35 | for(i=0; i < 24; i ++) 36 | { 37 | for(j = i+1; j < 25; j ++) 38 | { 39 | if(sd[i] > sd[j]) 40 | { 41 | temp = sd[i]; 42 | sd[i] = sd[j]; 43 | sd[j] = temp; 44 | } 45 | } // j loop 46 | }// i loop 47 | 48 | // copy 1d array into 2d 49 | printf("\nSorted 2D Array\n"); 50 | p = 0; 51 | for(i=0; i < 5; i ++) 52 | { 53 | for(j = 0; j < 5; j ++) 54 | { 55 | a[i][j] = sd[p]; 56 | printf("%5d", a[i][j]); 57 | p++; 58 | } 59 | printf("\n"); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /basics/Net Salary Calculation: -------------------------------------------------------------------------------- 1 | 2 | main() 3 | { 4 | int salary, hra, da, netsalary; 5 | 6 | /* take salary as input */ 7 | printf("Enter salary : "); 8 | scanf("%d",&salary); 9 | 10 | // calculate hra and da 11 | hra = salary * 40/100; 12 | da = salary * 20/100; 13 | 14 | // calculate net salary 15 | netsalary = salary + hra + da; 16 | 17 | // print details 18 | printf("Salary : %d\n", salary); 19 | printf("HRA : %d\n", hra); 20 | printf("DA : %d\n", da); 21 | printf("Net Salary: %d\n", netsalary); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /basics/net_amount_calculation.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | int amount,discount, tax, net_amount,taxable_amount; 6 | 7 | printf("Enter amount :"); 8 | scanf("%d",&amount); 9 | // calculate discount 10 | if (amount > 10000) 11 | discount = amount * 0.10; 12 | else 13 | discount = amount * 0.05; 14 | 15 | taxable_amount = amount - discount; 16 | 17 | // calculate tax 18 | if (taxable_amount > 5000) 19 | tax = taxable_amount * 0.12; 20 | else 21 | tax = 0; 22 | 23 | net_amount = taxable_amount + tax; 24 | 25 | // print details 26 | printf("Amount : %10d\n",amount); 27 | printf("Discount : %10d\n",discount); 28 | printf("Taxable : %10d\n",taxable_amount); 29 | printf("Tax : %10d\n",tax); 30 | printf("Net amount : %10d", net_amount); 31 | } 32 | -------------------------------------------------------------------------------- /basics/net_amount_with_discount_and_tax.c: -------------------------------------------------------------------------------- 1 | // Program to calculate net amount based on price and qty 2 | // Date : 22-Mar-2019 3 | // Author : Srikanth 4 | 5 | #include 6 | 7 | void main() 8 | { 9 | int price, qty, amount, discount = 0; 10 | int tax, grossamount, netamount; 11 | 12 | /* Take input from user */ 13 | printf("Enter price : "); 14 | scanf("%d",&price); 15 | printf("Enter quantity : "); 16 | scanf("%d",&qty); 17 | 18 | // Calculate net amount 19 | amount = qty * price; 20 | if (qty > 2) 21 | discount = amount * 0.10; 22 | 23 | grossamount = amount - discount; 24 | tax = grossamount * 0.12; 25 | netamount = grossamount + tax; 26 | 27 | // Display bill 28 | printf("\nBill\n"); 29 | printf("Amount : %6d\n",amount); 30 | printf(" - discount : %6d\n",discount); 31 | printf(" ------\n"); 32 | printf("Gross Amount : %6d\n",grossamount); 33 | printf(" + Tax : %6d\n",tax); 34 | printf(" ------\n"); 35 | printf("Net Amount : %6d\n",netamount); 36 | } 37 | -------------------------------------------------------------------------------- /basics/net_price.c: -------------------------------------------------------------------------------- 1 | // Calculate Net price 2 | #include 3 | 4 | void main() 5 | { 6 | int qty, price, amount, discount, tax,gross_amount,net_price; 7 | 8 | printf("Enter quantity : "); 9 | scanf("%d",&qty); 10 | printf("Enter price : "); 11 | scanf("%d",&price); 12 | 13 | amount = qty * price; 14 | discount = amount * 0.10; 15 | gross_amount = amount - discount; 16 | tax = gross_amount * 0.12; 17 | net_price = gross_amount + tax; 18 | 19 | printf("\nAmount : %d", amount); 20 | printf("\n - Discount : %d", discount); 21 | printf("\n ---------"); 22 | printf("\nGross Amount : %d", gross_amount); 23 | printf("\n + Tax : %d", tax); 24 | printf("\n ---------"); 25 | printf("\nNet Price : %d", net_price); 26 | } 27 | -------------------------------------------------------------------------------- /basics/net_salary.c: -------------------------------------------------------------------------------- 1 | // Net salary calculation 2 | #include 3 | main() 4 | { 5 | int salary, hra, da, pf, gross, net; 6 | 7 | // Take salary from user 8 | printf("Enter basic salary : "); 9 | scanf("%d",&salary); 10 | 11 | // Calculate HRA,DA and PF 12 | hra = salary * 40 / 100; 13 | da = salary * 25 / 100; 14 | gross = salary + hra + da; 15 | pf = gross * 3 / 100; 16 | net = gross - pf; 17 | 18 | // Print all details 19 | printf("Basic Salary : %d\n", salary); 20 | printf("HRA : %d\n", hra); 21 | printf("DA : %d\n", da); 22 | printf("Gross Salary : %d\n", gross); 23 | printf("PF : %d\n", pf); 24 | printf("Net Salary : %d\n", net); 25 | } 26 | -------------------------------------------------------------------------------- /basics/net_salary_calculation.c: -------------------------------------------------------------------------------- 1 | // Calculate net salary based on salary 2 | 3 | #include 4 | 5 | void main() 6 | { 7 | int salary, hra,da,gross,net,tax; 8 | 9 | printf("Enter salary : "); 10 | scanf("%d",&salary); 11 | 12 | if (salary > 20000) 13 | { 14 | hra = salary * 0.30; 15 | da = salary * 0.25; 16 | } 17 | else 18 | { 19 | hra = salary * 0.25; 20 | da = salary * 0.15; 21 | } 22 | 23 | gross = salary + hra + da; 24 | 25 | if (gross > 50000) 26 | tax = gross * 0.12; 27 | else 28 | tax = 0; 29 | 30 | net = gross - tax; 31 | 32 | printf("Salary : %6d\n",salary); 33 | printf("+ HRA : %6d\n",hra); 34 | printf("+ DA : %6d\n",da); 35 | printf("Gross Salary: %6d\n",gross); 36 | printf("- Tax : %6d\n",tax); 37 | printf("Net Salary : %6d\n",net); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /basics/wage_calculation.c: -------------------------------------------------------------------------------- 1 | // Calculate wage 2 | #include 3 | void main() 4 | { 5 | int hours, rate, wage, bonus, gross_wage,net_wage,tax; 6 | 7 | printf("Enter hours and rate :"); 8 | scanf("%d%d",&hours,&rate); 9 | 10 | wage = hours * rate; 11 | bonus = wage * 0.5; 12 | gross_wage = wage + bonus; 13 | tax = gross_wage * 0.05; 14 | net_wage = gross_wage - tax; 15 | 16 | printf("No. of hours : %6d\n",hours); 17 | printf("Rate per hour : %6d\n",rate); 18 | printf("Wage : %6d\n",wage); 19 | printf(" + Bonus : %6d\n",bonus); 20 | printf("Gross Wage : %6d\n",gross_wage); 21 | printf("- Tax : %6d\n",tax); 22 | printf("Net Wage : %6d\n",net_wage); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /chars/Password check: -------------------------------------------------------------------------------- 1 | main() 2 | { 3 | char ch, pwd[6]; 4 | int i, upper = 0, digit = 0, special = 0; 5 | 6 | for(i=0; i < 6; i++) 7 | { 8 | ch = getch(); 9 | pwd[i] = ch; 10 | putch('*'); 11 | if (isupper(ch)) 12 | upper = 1; 13 | else 14 | if (isdigit(ch)) 15 | digit = 1; 16 | else 17 | if (!islower(ch)) 18 | special = 1; 19 | } 20 | 21 | if(!upper) 22 | printf("\nUppercase letter is missing!"); 23 | 24 | if(!digit) 25 | printf("\nDigit is missing!"); 26 | 27 | if(!special) 28 | printf("\nSpecial character is missing!"); 29 | 30 | if (upper && digit && special) 31 | { 32 | // display password 33 | printf("\n Password : "); 34 | for(i=0; i < 6; i++) 35 | putch( pwd[i]); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /chars/ValidatePin.c: -------------------------------------------------------------------------------- 1 | // Validate PIN entered by user 2 | #include 3 | 4 | main() 5 | { 6 | char ch; 7 | int i, dcount; 8 | 9 | dcount = 0; 10 | printf("Enter Pin :"); 11 | for(i=1; i <= 4; i ++) 12 | { 13 | ch = getch(); 14 | putch('.'); 15 | 16 | if (isdigit(ch) ) 17 | dcount ++; 18 | } 19 | 20 | if (dcount == 4) 21 | printf("\nValid PIN"); 22 | else 23 | printf("\nInvalid PIN"); 24 | } 25 | -------------------------------------------------------------------------------- /chars/validate_password.c: -------------------------------------------------------------------------------- 1 | // password check 2 | 3 | main() 4 | { 5 | char ch; 6 | int i,digits, upper,special; 7 | 8 | digits = upper = special = 0; 9 | 10 | printf("Enter your password :"); 11 | 12 | for(i=1; i <= 6; i ++) 13 | { 14 | ch = getch(); 15 | putch('.'); 16 | if (isupper(ch)) 17 | upper++; 18 | else 19 | if ( isdigit(ch)) 20 | digits ++; 21 | else 22 | if(ch == '_' || ch == '-' || ch == '*' || ch == '#') 23 | special ++; 24 | } 25 | 26 | if (upper == 0) 27 | printf("\nUpper case letter is missing!"); 28 | 29 | if (digits == 0) 30 | printf("\nDigit is missing!"); 31 | 32 | if (special == 0) 33 | printf("\nSpecial char #,_, _ ,* is missing!"); 34 | 35 | if (upper>0 && digits> 0 && special > 0) 36 | printf("\nValid password!"); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /conditional/no_days.c: -------------------------------------------------------------------------------- 1 | // Display no. of days in the given month 2 | 3 | main() 4 | { 5 | int month, year, nodays; 6 | 7 | printf("Enter month : "); 8 | scanf("%d",&month); 9 | 10 | switch(month) 11 | { 12 | case 2 : 13 | printf("Enter year : "); 14 | scanf("%d",&year); 15 | if( year % 4 == 0 && year % 100 != 0 || year % 400 == 0) 16 | nodays = 29; 17 | else 18 | nodays = 28; 19 | break; 20 | 21 | case 4 : 22 | case 6 : 23 | case 9 : 24 | case 11: 25 | nodays = 30; 26 | break; 27 | default : 28 | nodays = 31; 29 | } 30 | 31 | printf("No. of days : %d", nodays); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /files/Binary File Demo with Books: -------------------------------------------------------------------------------- 1 | writebooks.c 2 | ============= 3 | #include 4 | 5 | struct book 6 | { 7 | char title[50], author[30]; 8 | }; 9 | 10 | main() 11 | { 12 | FILE * fp; 13 | struct book book; 14 | 15 | 16 | fp = fopen("books.dat", "ab"); 17 | 18 | while(1) 19 | { 20 | printf("Enter title : "); 21 | gets(book.title); 22 | 23 | if (strcmp(book.title,"end") == 0) 24 | break; 25 | 26 | printf("Enter Author : "); 27 | gets(book.author); 28 | 29 | fwrite(&book,sizeof(book),1,fp); 30 | } 31 | 32 | fclose(fp); 33 | } 34 | 35 | listbooks.c 36 | =========== 37 | 38 | #include 39 | 40 | struct book 41 | { 42 | char title[50], author[30]; 43 | }; 44 | 45 | main() 46 | { 47 | FILE * fp; 48 | struct book book; 49 | int count; 50 | 51 | fp = fopen("books.dat", "rb"); 52 | 53 | while(1) 54 | { 55 | count = fread(&book,sizeof(book),1,fp); 56 | if (count == 0) 57 | break; 58 | printf("%-30s %-30s\n", book.title, book.author); 59 | } 60 | 61 | fclose(fp); 62 | } 63 | 64 | booksbytopic.c 65 | ============== 66 | 67 | #include 68 | 69 | struct book 70 | { 71 | char title[50], author[30]; 72 | }; 73 | 74 | main() 75 | { 76 | FILE * fp; 77 | struct book book; 78 | char subject[10]; 79 | int count; 80 | 81 | fp = fopen("books.dat", "rb"); 82 | 83 | printf("Enter subject : "); 84 | scanf("%s",&subject); 85 | 86 | while(1) 87 | { 88 | count = fread(&book,sizeof(book),1,fp); 89 | if (count == 0) 90 | break; 91 | if( strstr(book.title,subject) != 0) 92 | printf("\n%-30s %-30s\n", book.title, book.author); 93 | } 94 | 95 | fclose(fp); 96 | } 97 | 98 | readbookbyposition.c 99 | ==================== 100 | #include 101 | 102 | struct book 103 | { 104 | char title[50], author[30]; 105 | }; 106 | 107 | main() 108 | { 109 | FILE * fp; 110 | struct book book; 111 | int bookno,count; 112 | 113 | fp = fopen("books.dat", "rb"); 114 | 115 | while(1) 116 | { 117 | printf("\nEnter book number [0 to stop] :"); 118 | scanf("%d",&bookno); 119 | 120 | if (bookno == 0) 121 | break; 122 | 123 | fseek(fp,sizeof(book) * (bookno-1),0); // position file pointer 124 | count = fread(&book,sizeof(book),1,fp); // read a book 125 | if(count == 0) 126 | printf("\nSorry! Book Not Found!\n"); 127 | else 128 | printf("\n%-30s %-30s\n", book.title, book.author); 129 | } 130 | 131 | fclose(fp); 132 | } 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /files/Binary File Examples: -------------------------------------------------------------------------------- 1 | // Write products info into PRODUCTS.DAT 2 | #include 3 | 4 | struct product 5 | { 6 | char name[30]; 7 | int price; 8 | }; 9 | 10 | main() 11 | { 12 | FILE * fp; 13 | struct product p; 14 | 15 | fp = fopen("products.dat","wb"); 16 | while(1) 17 | { 18 | fflush(stdin); // clear keyboard buffer 19 | printf("Enter product name [press enter to stop]:"); 20 | gets(p.name); 21 | if (strlen(p.name) == 0) 22 | break; 23 | 24 | printf("Enter product price:"); 25 | scanf("%d",&p.price); 26 | fwrite(&p,sizeof(p),1,fp); 27 | } 28 | 29 | fclose(fp); 30 | } 31 | 32 | ---------------------------------------- 33 | // Read and list products from Products.dat 34 | 35 | #include 36 | struct product 37 | { 38 | char name[30]; 39 | int price; 40 | }; 41 | 42 | main() 43 | { 44 | FILE * fp; 45 | struct product p; 46 | int count; 47 | 48 | fp = fopen("products.dat","rb"); 49 | while(1) 50 | { 51 | count = fread(&p,sizeof(p),1,fp); 52 | if (count == 0) 53 | break; 54 | 55 | printf("\n%-30s %6d",p.name,p.price); 56 | } 57 | fclose(fp); 58 | } 59 | 60 | ---------------------------- 61 | 62 | 63 | // Take position and display product at that position 64 | #include 65 | 66 | struct product 67 | { 68 | char name[30]; 69 | int price; 70 | }; 71 | 72 | main() 73 | { 74 | FILE * fp; 75 | struct product p; 76 | int count,pos,offset; 77 | 78 | fp = fopen("products.dat","rb"); 79 | while(1) 80 | { 81 | printf("Enter position : "); 82 | scanf("%d",&pos); 83 | if (pos == 0) 84 | break; 85 | 86 | offset = (pos - 1) * 3 //sizeof(struct product); 87 | fseek(fp,offset,SEEK_SET); 88 | fread(&p,sizeof(p),1,fp); 89 | printf("\n%-30s %6d\n",p.name,p.price); 90 | } 91 | 92 | fclose(fp); 93 | } 94 | -------------------------------------------------------------------------------- /files/CommonLines.c: -------------------------------------------------------------------------------- 1 | // Ver. 1.0 2 | // Display common lines in two files 3 | // Files are provided on command line 4 | 5 | #include 6 | main(int argc, char * argv[]) 7 | { 8 | FILE * fp1, * fp2; 9 | char line1[100], line2[100]; 10 | char * cp; 11 | 12 | if ( argc < 3) 13 | { 14 | printf("Usage : commonlines file1 file2"); 15 | exit(0); 16 | } 17 | 18 | fp1 = fopen(argv[1],"rt"); 19 | if ( fp1 == NULL) 20 | { 21 | printf("Sorry! Could not open file %s. Quitting.", argv[1]); 22 | exit(1); 23 | } 24 | 25 | fp2 = fopen(argv[2],"rt"); 26 | if ( fp2 == NULL) 27 | { 28 | printf("Sorry! Could not open file %s. Quitting.", argv[2]); 29 | exit(2); 30 | } 31 | 32 | while(1) 33 | { 34 | cp = fgets(line1,100,fp1); 35 | if (cp == NULL) 36 | break; 37 | 38 | // read and compare first file line with each line in second file 39 | fseek(fp2,0,0); 40 | while(1) 41 | { 42 | cp = fgets(line2,100,fp2); 43 | if (cp == NULL) 44 | break; 45 | if (strcmp(line1,line2) == 0) 46 | { 47 | printf(line1); 48 | break; 49 | } 50 | } 51 | } 52 | fclose(fp1); 53 | fclose(fp2); 54 | } 55 | -------------------------------------------------------------------------------- /files/CommonLines2.c: -------------------------------------------------------------------------------- 1 | // Ver 2.0 - uses linked list 2 | // Display common lines in two files 3 | // Files are provided on command line 4 | 5 | #include 6 | 7 | struct node 8 | { 9 | char line[100]; 10 | int found; 11 | struct node * next; 12 | }; 13 | 14 | int is_present(struct node * start, char line[]) 15 | { 16 | while(start != NULL) 17 | { 18 | if (strcmp(start->line, line) == 0 ) // found common line 19 | { 20 | if (start->found == 1) // Already line was found so return false 21 | return 0; 22 | else 23 | { 24 | start->found = 1; // Turn flag on 25 | return 1; // Return true as we encountered first time 26 | } 27 | } 28 | start = start -> next; 29 | } 30 | return 0; // Not found 31 | } 32 | 33 | main(int argc, char * argv[]) 34 | { 35 | FILE * fp1, * fp2; 36 | char line[100]; 37 | char * cp; 38 | struct node * root = NULL, * prev, * current; 39 | 40 | 41 | if ( argc < 3) 42 | { 43 | printf("Usage : commonlines2 file1 file2"); 44 | exit(0); 45 | } 46 | 47 | fp1 = fopen(argv[1],"rt"); 48 | if ( fp1 == NULL) 49 | { 50 | printf("Sorry! Could not open file %s. Quitting.", argv[1]); 51 | exit(1); 52 | } 53 | 54 | fp2 = fopen(argv[2],"rt"); 55 | if ( fp2 == NULL) 56 | { 57 | printf("Sorry! Could not open file %s. Quitting.", argv[2]); 58 | exit(2); 59 | } 60 | 61 | while(1) 62 | { 63 | cp = fgets(line,100,fp1); 64 | if (cp == NULL) 65 | break; 66 | // add to list 67 | current = (struct node *) malloc(sizeof(struct node)); 68 | current->next = NULL; 69 | current->found = 0; 70 | strcpy(current->line, line); 71 | 72 | if (root == NULL) // first node 73 | root = current; 74 | else 75 | prev -> next = current; 76 | 77 | prev = current; 78 | } 79 | 80 | // Read second file and print common lines 81 | while(1) 82 | { 83 | cp = fgets(line,100,fp2); 84 | if (cp == NULL) 85 | break; 86 | 87 | if (is_present(root,line)) 88 | printf(line); // found in first file(list) so common line 89 | } 90 | 91 | 92 | fclose(fp1); 93 | fclose(fp2); 94 | 95 | 96 | 97 | } 98 | -------------------------------------------------------------------------------- /files/ContactsMenu.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define FILENAME "contacts.dat" 7 | 8 | struct contact { 9 | char name[30]; 10 | char email[50]; 11 | char mobile[11]; 12 | }; 13 | 14 | void add_contact(); 15 | void get_contact(); 16 | void list_contacts(); 17 | void search_contacts(); 18 | 19 | void main() 20 | { 21 | int opt; 22 | 23 | while(1) 24 | { 25 | printf("\n***** MENU ******\n"); 26 | printf("1. Add Contact\n"); 27 | printf("2. List Contacts\n"); 28 | printf("3. Get Contact\n"); 29 | printf("4. Search Contacts\n"); 30 | printf("5. Exit\n"); 31 | printf("Enter your choice [1-5] :");\ 32 | scanf("%d",&opt); 33 | 34 | switch(opt) 35 | { 36 | case 1: add_contact(); break; 37 | case 2: list_contacts(); break; 38 | case 3: get_contact(); break; 39 | case 4: search_contacts(); break; 40 | case 5: exit(0); 41 | } // switch 42 | } // while 43 | } // main() 44 | 45 | 46 | void add_contact() 47 | { 48 | FILE * fp; 49 | struct contact c; 50 | 51 | fp = fopen(FILENAME,"ab"); 52 | if (fp==NULL) 53 | { 54 | printf("Sorry! File could not be opened!"); 55 | exit(0); 56 | } 57 | 58 | fflush(stdin); // clear keyboard buffer 59 | printf("Enter name : "); 60 | gets(c.name); 61 | printf("Enter email : "); 62 | gets(c.email); 63 | printf("Enter mobile : "); 64 | gets(c.mobile); 65 | fwrite(&c,sizeof(c),1,fp); 66 | printf("\nContact Saved Successfully!\n"); 67 | fclose(fp); 68 | 69 | printf("\nPress a key to continue...\n"); 70 | getch(); 71 | } 72 | 73 | void list_contacts() 74 | { 75 | FILE * fp; 76 | struct contact c; 77 | int count; 78 | 79 | fp = fopen(FILENAME,"rb"); 80 | if (fp==NULL) 81 | { 82 | printf("Sorry! File could not be opened!"); 83 | exit(0); 84 | } 85 | printf("\nList of Contacts\n"); 86 | printf("\n================\n"); 87 | 88 | while(1) 89 | { 90 | count = fread(&c,sizeof(c),1,fp); 91 | if (count == 0) 92 | break; 93 | printf("%-20s %-30s %s\n", c.name,c.email,c.mobile); 94 | } 95 | 96 | fclose(fp); 97 | printf("\nPress a key to continue...\n"); 98 | getch(); 99 | 100 | } 101 | 102 | void get_contact() 103 | { 104 | FILE * fp; 105 | struct contact c; 106 | int id,count; 107 | 108 | fp = fopen(FILENAME,"rb"); 109 | if (fp==NULL) 110 | { 111 | printf("Sorry! File could not be opened!"); 112 | exit(0); 113 | } 114 | 115 | printf("Enter contact id : ");\ 116 | scanf("%d",&id); 117 | fseek(fp, (id-1) * sizeof(c), SEEK_SET); 118 | count = fread(&c,sizeof(c),1,fp); 119 | if (count == 0) 120 | printf("Sorry! Could not read contact details!"); 121 | else 122 | printf("%-20s %-30s %s\n", c.name,c.email,c.mobile); 123 | 124 | fclose(fp); 125 | printf("\nPress a key to continue...\n"); 126 | getch(); 127 | } 128 | 129 | 130 | void search_contacts() 131 | { 132 | FILE * fp; 133 | struct contact c; 134 | char name[30]; 135 | int count; 136 | 137 | fp = fopen(FILENAME,"rb"); 138 | if (fp==NULL) 139 | { 140 | printf("Sorry! File could not be opened!"); 141 | exit(0); 142 | } 143 | 144 | fflush(stdin); 145 | printf("Enter contact name to search : "); 146 | gets(name); 147 | 148 | while(1) 149 | { 150 | count = fread(&c,sizeof(c),1,fp); 151 | if (count == 0) 152 | break; 153 | 154 | if(strstr(c.name, name) != NULL) 155 | printf("%-20s %-30s %s\n", c.name,c.email,c.mobile); 156 | } 157 | 158 | fclose(fp); 159 | printf("\nPress a key to continue...\n"); 160 | getch(); 161 | } 162 | -------------------------------------------------------------------------------- /files/ConvertFileToUpper.c: -------------------------------------------------------------------------------- 1 | // Convert file content to uppercase 2 | #include 3 | main() 4 | { 5 | FILE * sfp, * tfp; 6 | char sfile[30], tfile[30]; 7 | int ch; 8 | 9 | printf("Enter source filename :"); 10 | gets(sfile); 11 | 12 | printf("Enter target filename :"); 13 | gets(tfile); 14 | 15 | sfp = fopen(sfile,"rt"); // create file 16 | if (sfp == NULL) 17 | { 18 | printf("Sorry! Could not open file!"); 19 | exit(0); 20 | } 21 | 22 | tfp = fopen(tfile,"wt"); // create file 23 | if (tfp == NULL) 24 | { 25 | printf("Sorry! Could not create file!"); 26 | exit(0); 27 | } 28 | 29 | while(1) 30 | { 31 | ch = fgetc(sfp); 32 | if(ch == EOF) 33 | break; 34 | 35 | fputc(toupper(ch),tfp); 36 | } 37 | 38 | fclose(sfp); 39 | fclose(tfp); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /files/CopyFile.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main(int argc, char * argv[]) 4 | { 5 | FILE * sfp, * tfp; 6 | char ch; 7 | 8 | 9 | if (argc < 3) 10 | { 11 | printf("Usage : CopyFile sourcefile targetfile\n"); 12 | exit(0); 13 | } 14 | 15 | sfp = fopen(argv[1],"r"); 16 | if ( sfp == NULL) 17 | { 18 | printf("Sorry! [%s] not found.\n", argv[1]); 19 | exit(1); 20 | } 21 | 22 | tfp = fopen(argv[2],"w"); 23 | if (tfp == NULL) 24 | { 25 | printf("Sorry! [%s] was not create.\n", argv[2]); 26 | exit(1); 27 | } 28 | 29 | while (1) 30 | { 31 | ch = fgetc(sfp); 32 | if (ch == -1) 33 | break; 34 | 35 | fputc(ch,tfp); 36 | } 37 | 38 | fclose(sfp); 39 | fclose(tfp); 40 | 41 | printf("Copied [%s] to [%s] successfully!\n", argv[1],argv[2]); 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /files/Courses.c: -------------------------------------------------------------------------------- 1 | // Program to manage courses using binary files of C 2 | // Stores data that is in a struct course into courses.dat file 3 | 4 | #include 5 | 6 | struct course 7 | { 8 | char name[20]; 9 | int duration, fee; 10 | }; 11 | 12 | main() 13 | { 14 | FILE * fp; 15 | int opt; 16 | 17 | 18 | fp = fopen("e:\\c\\apr27\\courses.dat","r+b"); 19 | if (fp == NULL) 20 | { 21 | printf("Sorry! Could not open file!"); 22 | exit(1); 23 | } 24 | 25 | while(1) 26 | { 27 | printf("\n *** Menu ***\n"); 28 | printf("1. Add Course\n"); 29 | printf("2. List Courses\n"); 30 | printf("10. Exit\n"); 31 | 32 | scanf("%d",&opt); 33 | switch(opt) 34 | { 35 | case 1 : add_course(fp); break; 36 | case 2 : list_courses(fp); break; 37 | case 10 : exit(0); 38 | } 39 | } 40 | 41 | fclose(fp); 42 | } 43 | 44 | void add_course(FILE * fp) 45 | { 46 | struct course c; 47 | 48 | // clear keyboard buffer 49 | fflush(stdin); 50 | printf("Enter name : "); 51 | gets(c.name); 52 | printf("Enter duration : "); 53 | scanf("%d",&c.duration); 54 | printf("Enter fee : "); 55 | scanf("%d",&c.fee); 56 | 57 | // go to end of file and write a new record 58 | fseek(fp,0, SEEK_END); 59 | fwrite(&c, sizeof(c), 1, fp); 60 | printf("Added Course Successfully!"); 61 | getch(); 62 | } 63 | 64 | 65 | void list_courses(FILE *fp) 66 | { 67 | struct course c; 68 | int count; 69 | 70 | // go to beginning of file 71 | fseek(fp,0,SEEK_SET); 72 | while(1) 73 | { 74 | count = fread(&c,sizeof(c),1,fp); 75 | if (count != 1) 76 | break; 77 | printf("%-20s %3d %5d\n", c.name, c.duration, c.fee); 78 | } 79 | getch(); // wait for key 80 | 81 | } 82 | -------------------------------------------------------------------------------- /files/Customers Operations: -------------------------------------------------------------------------------- 1 | write_customers.c 2 | ================= 3 | #include 4 | 5 | struct customer 6 | { 7 | char name[20], mobile[15], email[50]; 8 | }; 9 | 10 | main() 11 | { 12 | FILE * fp; 13 | struct customer c; 14 | 15 | fp = fopen("customers.dat","wb"); 16 | 17 | while(1) 18 | { 19 | printf("Enter customer name : "); 20 | gets(c.name); 21 | if (strcmp(c.name,"end") == 0) 22 | break; 23 | printf("Enter customer mobile : "); 24 | gets(c.mobile); 25 | 26 | printf("Enter customer email : "); 27 | gets(c.email); 28 | 29 | fwrite(&c, sizeof(c),1,fp); 30 | } 31 | 32 | fclose(fp); 33 | } 34 | 35 | 36 | 37 | list_customers.c 38 | ================= 39 | #include 40 | #define FILENAME "customers.dat" 41 | struct customer 42 | { 43 | char name[20], mobile[15], email[50]; 44 | }; 45 | 46 | main() 47 | { 48 | FILE * fp; 49 | struct customer c; 50 | int count; 51 | 52 | fp = fopen(FILENAME,"rb"); 53 | while(1) 54 | { 55 | count = fread(&c,sizeof(c),1,fp); 56 | if (count == 0) // EOF 57 | break; 58 | printf("\n%-20s %-15s %-50s", c.name,c.mobile, c.email); 59 | } 60 | 61 | fclose(fp); 62 | } 63 | 64 | 65 | update_customer.c 66 | ================= 67 | #include 68 | #define FILENAME "customers.dat" 69 | 70 | struct customer 71 | { 72 | char name[20], mobile[15], email[50]; 73 | }; 74 | 75 | main() 76 | { 77 | FILE * fp; 78 | struct customer c; 79 | int count,id,pos; 80 | 81 | fp = fopen(FILENAME,"r+b"); 82 | printf("Enter customer id : "); 83 | scanf("%d",&id); 84 | pos = (id- 1) * sizeof(struct customer); 85 | fseek(fp,pos, SEEK_SET); // Move to given offset from start of file 86 | count = fread(&c,sizeof(c),1,fp); 87 | if (count == 0) // EOF 88 | { 89 | printf("Sorry! Customer Id Not Found"); 90 | exit(0); 91 | } 92 | fflush(stdin); 93 | printf("Enter new mobile number :"); 94 | gets(c.mobile); 95 | fseek(fp,pos, SEEK_SET); 96 | fwrite(&c,sizeof(c),1,fp); 97 | fclose(fp); 98 | printf("Updated Mobile Successfully!"); 99 | } 100 | 101 | 102 | -------------------------------------------------------------------------------- /files/Display Files With Line Numbers: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main(int argc, char * argv[]) 4 | { 5 | FILE * fp; 6 | char line[200]; 7 | int i,lineno; 8 | 9 | 10 | if ( argc == 1) 11 | { 12 | printf("Usage : DisplayFile ... \n"); 13 | exit(1); 14 | } 15 | 16 | for(i=1; i < argc ; i ++) 17 | { 18 | lineno=1; 19 | fp = fopen(argv[i],"r"); 20 | if (fp == NULL) // file not opened successfully 21 | { 22 | printf("\n\nSorry! [%s] File Not Found!\n\n", argv[i]); 23 | continue; 24 | } 25 | printf("\n********%s***********\n\n", argv[i]); 26 | while(1) 27 | { 28 | if (fgets(line,200,fp) == NULL) // reached end of file 29 | break; 30 | printf("%4d: %s", lineno,line); 31 | lineno++; 32 | } 33 | 34 | fclose(fp); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /files/Dynamic and Args Demo: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main(int argc, char * argv[]) 4 | { 5 | int i,n; 6 | int * p; 7 | 8 | n = atoi(argv[1]); // string to int 9 | 10 | p = (int *) malloc(n * sizeof(int)); 11 | if(p == NULL) 12 | { 13 | printf("\nMemory Insufficient!"); 14 | exit(1); 15 | } 16 | 17 | srand(time(0)); 18 | 19 | for(i=0; i < n ; i ++) 20 | { 21 | p[i] = rand() % 100; 22 | } 23 | 24 | for(i=n-1; i >=0 ; i --) 25 | { 26 | printf("%d \t", p[i]); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /files/Employee Operations: -------------------------------------------------------------------------------- 1 | WRITEEMP.C 2 | ============ 3 | // Write employee details to emp.dat 4 | 5 | #include 6 | 7 | struct employee 8 | { 9 | char name[20]; 10 | int salary; 11 | }; 12 | 13 | main() 14 | { 15 | FILE * fp; 16 | struct employee emp; 17 | 18 | fp = fopen("emp.dat","ab"); 19 | if (fp == NULL) 20 | { 21 | exit(1); 22 | } 23 | 24 | while (1) 25 | { 26 | fflush(stdin); // clear keyboard buffer 27 | 28 | printf("Enter name [enter to stop]:"); 29 | gets(emp.name); 30 | 31 | if (strlen(emp.name) == 0) 32 | break; 33 | 34 | printf("Enter salary :"); 35 | scanf("%d",&emp.salary); 36 | // write employee details to file 37 | fwrite(&emp,sizeof(emp), 1, fp); 38 | } 39 | 40 | fclose(fp); 41 | 42 | } 43 | 44 | 45 | READEMP.C 46 | ========= 47 | #include 48 | 49 | struct employee 50 | { 51 | char name[20]; 52 | int salary; 53 | }; 54 | 55 | main() 56 | { 57 | FILE * fp; 58 | struct employee emp; 59 | int empid, result; 60 | 61 | fp = fopen("emp.dat","rb"); 62 | if (fp == NULL) 63 | { 64 | exit(1); 65 | } 66 | 67 | while (1) 68 | { 69 | printf("Enter employee id [0 to stop]:"); 70 | scanf("%d",&empid); 71 | 72 | if (empid == 0) 73 | break; 74 | 75 | // move to required record 76 | fseek(fp, (empid - 1) * sizeof(emp),0); 77 | result = fread(&emp,sizeof(emp),1,fp); 78 | 79 | if (result == 1) 80 | printf("\n%s %d\n", emp.name, emp.salary); 81 | else 82 | printf("\nSorry! Employee Not Found\n"); 83 | 84 | } 85 | 86 | fclose(fp); 87 | } 88 | 89 | UPDATESALAY.C 90 | ============== 91 | #include 92 | 93 | struct employee 94 | { 95 | char name[20]; 96 | int salary; 97 | }; 98 | 99 | main() 100 | { 101 | FILE * fp; 102 | struct employee emp; 103 | int empid, result; 104 | 105 | fp = fopen("emp.dat","r+b"); // open read write mode 106 | if (fp == NULL) 107 | { 108 | printf("Open error"); 109 | exit(1); 110 | } 111 | 112 | while (1) 113 | { 114 | printf("Enter employee id [0 to stop]:"); 115 | scanf("%d",&empid); 116 | 117 | if (empid == 0) 118 | break; 119 | 120 | // move to required record 121 | fseek(fp, (empid - 1) * sizeof(emp),0); 122 | result = fread(&emp,sizeof(emp),1,fp); 123 | 124 | if (result == 1) 125 | { 126 | printf("\n%s %d\n", emp.name, emp.salary); 127 | printf("\nEnter new salary : "); 128 | scanf("%d",&emp.salary); 129 | // write emp record back to file 130 | fseek(fp, (empid - 1) * sizeof(emp),0); 131 | fwrite(&emp,sizeof(emp),1,fp); 132 | } 133 | else 134 | printf("\nSorry! Employee Not Found\n"); 135 | } 136 | 137 | fclose(fp); 138 | } 139 | 140 | DisplayEmployee.c (Takes employee id on command line and displays details of employee) 141 | ================ 142 | #include 143 | 144 | struct employee 145 | { 146 | char name[20]; 147 | int salary; 148 | }; 149 | 150 | main(int argc, char * argv[]) 151 | { 152 | FILE * fp; 153 | struct employee emp; 154 | int empid, result; 155 | 156 | if(argc == 1) 157 | { 158 | printf("\nPlease provide employee id.\nExample : c:>DisplayEmployee 3\n"); 159 | return; 160 | } 161 | 162 | fp = fopen("emp.dat","rb"); 163 | if (fp == NULL) 164 | { 165 | printf("\nSorry!File could not be opened!\n"); 166 | exit(1); 167 | } 168 | 169 | empid = atoi(argv[1]); 170 | 171 | // move to required record 172 | fseek(fp, (empid - 1) * sizeof(emp),0); 173 | result = fread(&emp,sizeof(emp),1,fp); 174 | 175 | if (result == 1) 176 | { 177 | printf("\nName : %s", emp.name); 178 | printf("\nSalary : %d\n", emp.salary); 179 | } 180 | else 181 | printf("\nSorry! Employee Not Found\n"); 182 | 183 | fclose(fp); 184 | } 185 | 186 | 187 | -------------------------------------------------------------------------------- /files/EncDec.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | sytax : encdec flag source target 5 | 6 | flag can be -e or -d. -e for encrypt, -d for decrypt 7 | 8 | source is source file to be converted 9 | target is target file where to write 10 | 11 | */ 12 | 13 | main(int argc, char * argv[]) 14 | { 15 | int opt = 1; // encryption 16 | FILE * sfp, * tfp; 17 | int ch; 18 | 19 | if ( argc < 4) 20 | { 21 | printf("\nSyntax : encdec flag source target\n\n"); 22 | printf("flag : is either -e for encryption or -d for decryption\n"); 23 | printf("Source file : is file to be encrypted or decrypted\n"); 24 | printf("Target file : is file where you write encrypted or decrypted contents\n"); 25 | exit(1); 26 | } 27 | 28 | if (strcmp(argv[1],"-d") == 0) 29 | opt = 2; // decrypt 30 | else 31 | if (strcmp(argv[1],"-e") != 0) 32 | { 33 | printf("\nSorry! Invalid Flag. It must be -e for encrypt, -d for decrypt!\n"); 34 | exit(2); 35 | } 36 | 37 | // open source file 38 | sfp = fopen(argv[2],"r"); 39 | if ( sfp == NULL) 40 | { 41 | printf("\nSorry! Source file [%s] could not be opened!\n",argv[2]); 42 | exit(3); 43 | 44 | } 45 | 46 | tfp = fopen(argv[3],"w"); 47 | if ( tfp == NULL) 48 | { 49 | printf("\nSorry! Target file [%s] could not be created!\n",argv[3]); 50 | exit(4); 51 | } 52 | 53 | 54 | while(1) 55 | { 56 | 57 | ch = fgetc(sfp); 58 | if ( ch == -1) 59 | break; 60 | if ( opt == 1) 61 | fputc(ch + 1, tfp); 62 | else 63 | fputc(ch - 1, tfp); 64 | } 65 | 66 | fclose(tfp); 67 | fclose(sfp); 68 | 69 | } 70 | -------------------------------------------------------------------------------- /files/Encrypt.c: -------------------------------------------------------------------------------- 1 | // Encrypt source file and write to target file 2 | #include 3 | main(int argc, char * argv[]) 4 | { 5 | FILE * sfp, * tfp; 6 | int ch; 7 | 8 | if (argc < 3) 9 | { 10 | printf("\nUsage : encrypt \n"); 11 | printf("\nEncrypts given sourcefile and writes encrypted contents to target file\n"); 12 | printf("\n : is source file to be encrypted"); 13 | printf("\n : is file into which encrypted content to be written\n"); 14 | exit(1); 15 | } 16 | 17 | sfp = fopen(argv[1],"rt"); // create file 18 | if (sfp == NULL) 19 | { 20 | printf("Sorry! Could not open file -> %s\n",argv[1]); 21 | exit(2); 22 | } 23 | 24 | tfp = fopen(argv[2],"wt"); // create file 25 | if (tfp == NULL) 26 | { 27 | printf("Sorry! Could not create file -> %s",argv[2]); 28 | exit(3); 29 | } 30 | 31 | while(1) 32 | { 33 | ch = fgetc(sfp); 34 | if(ch == EOF) 35 | break; 36 | fputc(ch+1,tfp); 37 | } 38 | 39 | fclose(sfp); 40 | fclose(tfp); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /files/File Utilities: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void upper() 4 | { 5 | char sfn[50],tfn[50]; 6 | FILE *sfp,*tfp; 7 | int ch; 8 | 9 | // clear keyboard buffer 10 | fflush(stdin); 11 | 12 | printf("Enter source filename :"); 13 | gets(sfn); 14 | 15 | sfp = fopen(sfn,"r"); 16 | if (sfp == NULL) 17 | { 18 | printf("\nSorry! Source file could not be opened!"); 19 | return; 20 | } 21 | 22 | printf("Enter target filename :"); 23 | gets(tfn); 24 | 25 | 26 | tfp = fopen(tfn,"w"); 27 | if (tfp == NULL) 28 | { 29 | printf("\nSorry! Target file could not be opened!"); 30 | return; 31 | } 32 | 33 | while(1) 34 | { 35 | ch = fgetc(sfp); 36 | if (ch == EOF) 37 | break; 38 | fputc( toupper(ch),tfp); 39 | } 40 | 41 | fclose(sfp); 42 | fclose(tfp); 43 | 44 | printf("\nConverted [%s] to uppercase and placed in [%s]\n",sfn,tfn); 45 | 46 | } 47 | 48 | 49 | void lower() 50 | { 51 | char sfn[50],tfn[50]; 52 | FILE *sfp,*tfp; 53 | int ch; 54 | 55 | // clear keyboard buffer 56 | fflush(stdin); 57 | 58 | printf("Enter source filename :"); 59 | gets(sfn); 60 | 61 | sfp = fopen(sfn,"r"); 62 | if (sfp == NULL) 63 | { 64 | printf("\nSorry! Source file could not be opened!"); 65 | return; 66 | } 67 | 68 | printf("Enter target filename :"); 69 | gets(tfn); 70 | 71 | 72 | tfp = fopen(tfn,"w"); 73 | if (tfp == NULL) 74 | { 75 | printf("\nSorry! Target file could not be opened!"); 76 | return; 77 | } 78 | 79 | while(1) 80 | { 81 | ch = fgetc(sfp); 82 | if (ch == EOF) 83 | break; 84 | fputc( tolower(ch),tfp); 85 | } 86 | 87 | fclose(sfp); 88 | fclose(tfp); 89 | 90 | printf("\nConverted [%s] to lowercase and placed in [%s]\n",sfn,tfn); 91 | } 92 | 93 | void count() 94 | { 95 | char fn[50]; 96 | FILE *fp; 97 | int ch, chars, words, lines; 98 | 99 | // clear keyboard buffer 100 | fflush(stdin); 101 | 102 | printf("Enter filename :"); 103 | gets(fn); 104 | 105 | fp = fopen(fn,"r"); 106 | if (fp == NULL) 107 | { 108 | printf("\nSorry! File could not be opened!"); 109 | return; 110 | } 111 | chars = words = lines = 0; 112 | 113 | while(1) 114 | { 115 | ch = fgetc(fp); 116 | if (ch == EOF) 117 | break; 118 | chars++; 119 | if(ch == 32) 120 | words ++; 121 | 122 | if(ch == 10) 123 | { 124 | words ++; 125 | lines ++; 126 | } 127 | } 128 | fclose(fp); 129 | printf("\nNo. of lines : %5d",lines); 130 | printf("\nNo. of words : %5d",words); 131 | printf("\nNo. of chars : %5d\n",chars); 132 | 133 | } 134 | 135 | main() 136 | { 137 | int choice; 138 | 139 | while(1) 140 | { 141 | printf("\nMenu"); 142 | printf("\n====\n"); 143 | printf("1. Convert to upper\n"); 144 | printf("2. Convert to lower\n"); 145 | printf("3. Count char,words and lines\n"); 146 | printf("4. Exit\n"); 147 | printf("Enter your choice [1-3]:"); 148 | scanf("%d",&choice); 149 | printf("\n"); 150 | 151 | switch(choice) 152 | { 153 | case 1 : upper(); break; 154 | case 2 : lower(); break; 155 | case 3 : count(); break; 156 | case 4 : exit(0); 157 | } 158 | } 159 | 160 | } 161 | -------------------------------------------------------------------------------- /files/List of Employees: -------------------------------------------------------------------------------- 1 | // Display employees who have more salary than average salary 2 | // Uses file emp.dat which contains records of type struct employee 3 | 4 | #include 5 | 6 | struct employee 7 | { 8 | char name[20]; 9 | int salary; 10 | }; 11 | 12 | struct node 13 | { 14 | struct employee data; 15 | struct node * next; 16 | }; 17 | 18 | main() 19 | { 20 | FILE * fp; 21 | struct employee emp; 22 | struct node * root = NULL, * current, * prev; 23 | int count, total = 0, countemp = 0, avg; 24 | 25 | fp = fopen("emp.dat","rb"); 26 | while(1) 27 | { 28 | count = fread(&emp,sizeof(emp),1,fp); 29 | if ( count == 0) 30 | break; 31 | 32 | // add name to list 33 | current = (struct node *) malloc(sizeof(struct node)); 34 | current->data = emp; 35 | 36 | total += emp.salary; 37 | countemp++; 38 | 39 | current->next = NULL; 40 | 41 | if (root == NULL) 42 | root = current; 43 | else 44 | prev->next = current; 45 | 46 | prev = current; 47 | } 48 | 49 | fclose(fp); 50 | 51 | avg = total / countemp; 52 | 53 | current = root; 54 | while(current != NULL) 55 | { 56 | if (current->data.salary > avg) 57 | printf("%-20s %d\n", current->data.name, current->data.salary); 58 | current = current->next; 59 | } 60 | 61 | } 62 | 63 | 64 | -------------------------------------------------------------------------------- /files/Menu Template: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void option1() 4 | { 5 | printf("\nOption1 Completed Successfully!\n"); 6 | } 7 | 8 | void option2() 9 | { 10 | 11 | printf("\nOption1 Completed Successfully!\n"); 12 | } 13 | 14 | main() 15 | { 16 | int opt; 17 | 18 | while(1) 19 | { 20 | printf("\nMenu\n"); 21 | printf("\n1. Option1"); 22 | printf("\n2. Option2"); 23 | printf("\n3. Exit"); 24 | 25 | printf("\n\nEnter your choice [1-3] :"); 26 | scanf("%d", &opt); 27 | 28 | switch(opt) 29 | { 30 | case 1 : option1(); break; 31 | case 2 : option2(); break; 32 | case 3 : exit(0); 33 | default: printf("Sorry! Invalid Option. Try again!\n"); 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /files/Merge.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main(int argc, char * argv[]) 4 | { 5 | FILE * sfp, * tfp; 6 | char ch; 7 | 8 | 9 | if (argc < 3) 10 | { 11 | printf("Usage : Merge targetfile sourcefile1 [sourcefile2] ...\n"); 12 | exit(0); 13 | } 14 | 15 | 16 | tfp = fopen(argv[1],"w"); 17 | if (tfp == NULL) 18 | { 19 | printf("Sorry! [%s] was not create.\n", argv[2]); 20 | exit(1); 21 | } 22 | 23 | int i; 24 | for(i=2; i < argc; i ++) 25 | { 26 | // open source file 27 | sfp = fopen(argv[i],"r"); 28 | if (sfp == NULL) 29 | { 30 | printf("\nSorry! [%s] not found.\n", argv[i]); 31 | continue; 32 | } 33 | fprintf(tfp,"\n======================="); 34 | fprintf(tfp,"\nFile : %s", argv[i]); 35 | fprintf(tfp,"\n=======================\n"); 36 | while (1) 37 | { 38 | ch = fgetc(sfp); 39 | if (ch == -1) 40 | break; 41 | 42 | fputc(ch,tfp); 43 | } // while 44 | 45 | fclose(sfp); 46 | 47 | fputs("\n",tfp); // write blank lines between files 48 | } 49 | 50 | fclose(tfp); 51 | 52 | } 53 | -------------------------------------------------------------------------------- /files/NonDigitCopyFile.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | FILE * sfp, *tfp; 6 | int ch; 7 | char sfn[50], tfn[50]; 8 | 9 | 10 | printf("Enter source filename : "); 11 | gets(sfn); 12 | 13 | sfp = fopen(sfn,"r"); 14 | if ( sfp == NULL) 15 | { 16 | printf("Sorry! Source file not found!"); 17 | exit(1); 18 | } 19 | 20 | 21 | printf("Enter target filename : "); 22 | gets(tfn); 23 | 24 | tfp = fopen(tfn,"w"); 25 | if ( tfp == NULL) 26 | { 27 | printf("Sorry! Target cannot be created!"); 28 | exit(2); 29 | } 30 | 31 | while(1) 32 | { 33 | ch = fgetc(sfp); 34 | if (ch == -1) // EOF 35 | break; 36 | 37 | if (! isdigit(ch)) 38 | fputc(ch,tfp); 39 | } 40 | 41 | fclose(tfp); 42 | fclose(sfp); 43 | } 44 | -------------------------------------------------------------------------------- /files/Product Database Using Binary Files: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct product 4 | { 5 | char name[50]; 6 | int price; 7 | char status; 8 | }; 9 | 10 | void add_product(FILE * fp) 11 | { 12 | struct product p; 13 | 14 | fflush(stdin); // clear keyboard buffer 15 | 16 | printf("Product Name :"); 17 | gets(p.name); 18 | 19 | printf("Product Price : "); 20 | scanf("%d",&p.price); 21 | 22 | p.status = 'a'; 23 | 24 | fseek(fp,0,2); // go to end of file 25 | 26 | fwrite(&p,sizeof(p),1,fp); // write a record 27 | 28 | printf("\nProduct added successfully!\n"); 29 | } 30 | 31 | void search_product(FILE * fp) 32 | { 33 | struct product p; 34 | int id,c; 35 | 36 | printf("\nEnter product id :"); 37 | scanf("%d",&id); 38 | 39 | c = fseek(fp, sizeof(p) * (id-1),0); 40 | if ( c != 0) 41 | { 42 | printf("\nSorry! Invalid product id!\n"); 43 | return; 44 | } 45 | 46 | c = fread(&p,sizeof(p),1,fp); // write a record 47 | if ( c == 0) 48 | { 49 | printf("\nSorry! Invalid product id!\n"); 50 | return; 51 | } 52 | 53 | if(p.status != 'd') 54 | { 55 | printf("\n%-30s %6d\n", p.name, p.price); 56 | } 57 | else 58 | { 59 | printf("\nSorry! Product is not available!\n"); 60 | } 61 | } 62 | 63 | 64 | void delete_product(FILE * fp) 65 | { 66 | struct product p; 67 | int id,c; 68 | 69 | printf("\nEnter product id :"); 70 | scanf("%d",&id); 71 | 72 | c = fseek(fp, sizeof(p) * (id-1),0); 73 | if ( c != 0) 74 | { 75 | printf("\nSorry! Invalid product id!\n"); 76 | return; 77 | } 78 | 79 | c = fread(&p,sizeof(p),1,fp); // write a record 80 | if ( c == 0) 81 | { 82 | printf("\nSorry! Invalid product id!\n"); 83 | return; 84 | } 85 | p.status = 'd'; 86 | // write back 87 | fseek(fp, sizeof(p) * -1l,1); // go back by one record 88 | fwrite(&p,sizeof(p),1,fp); 89 | printf("\nProduct has been deleted!\n"); 90 | 91 | } 92 | 93 | 94 | 95 | void list_products(FILE * fp) 96 | { 97 | struct product p; 98 | 99 | fseek(fp,0,0); // go to beginning of file 100 | 101 | int c; 102 | int id = 1; 103 | while(1) 104 | { 105 | c = fread(&p,sizeof(p),1,fp); // write a record 106 | if (c == 0) 107 | break; 108 | 109 | if(p.status != 'd') 110 | { 111 | printf("\n%3d %-30s %6d", id, p.name, p.price); 112 | } 113 | 114 | id++; 115 | } 116 | 117 | printf("\n*************\n"); 118 | } 119 | 120 | 121 | main() 122 | { 123 | int choice; 124 | FILE * fp; 125 | 126 | fp = fopen("products.dat","r+b"); 127 | if ( fp == NULL) 128 | { 129 | printf("Sorry! File cannot be opened!"); 130 | exit(1); 131 | } 132 | 133 | while(1) 134 | { 135 | printf("\nMenu\n"); 136 | printf("==============\n"); 137 | printf("1.Add Product\n"); 138 | printf("2.List Product\n"); 139 | printf("3.Search Product\n"); 140 | printf("4.Delete Product\n"); 141 | printf("5.Exit\n"); 142 | printf("Enter your choice[1-5]:"); 143 | fflush(stdin); 144 | scanf("%d",&choice); 145 | 146 | switch(choice) 147 | { 148 | case 1 : add_product(fp); 149 | break; 150 | case 2 : list_products(fp); 151 | break; 152 | case 3 : search_product(fp); 153 | break; 154 | case 4 : delete_product(fp); 155 | break; 156 | case 5 : exit(0); 157 | } 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /files/Products Binary File: -------------------------------------------------------------------------------- 1 | #include 2 | #define FILENAME "products.dat" 3 | 4 | struct product 5 | { 6 | char name[30]; 7 | int price; 8 | }; 9 | 10 | void list_products() 11 | { 12 | FILE * fp; 13 | struct product p; 14 | int count; 15 | 16 | 17 | fp = fopen(FILENAME, "rb"); 18 | 19 | printf("\n********* LIST OF PRODUCTS *************\n"); 20 | 21 | while(1) 22 | { 23 | count = fread(&p, sizeof(p), 1, fp); 24 | if ( count == 0) 25 | break; 26 | 27 | printf("\n%-30s %10d", p.name, p.price); 28 | } 29 | 30 | fclose(fp); 31 | 32 | printf("\n\n"); 33 | 34 | } 35 | 36 | void add_product() 37 | { 38 | FILE * fp; 39 | struct product p; 40 | 41 | 42 | fp = fopen(FILENAME, "ab"); 43 | while(1) 44 | { 45 | // empty keyboard buffer 46 | fflush(stdin); 47 | 48 | printf("\nEnter product name [enter to stop] : "); 49 | gets(p.name); 50 | 51 | if (strlen(p.name)== 0) 52 | break; 53 | 54 | printf("Enter product price : "); 55 | scanf("%d",&p.price); 56 | 57 | fwrite(&p, sizeof(p), 1, fp); 58 | } 59 | 60 | fclose(fp); 61 | } 62 | 63 | 64 | void search_product() 65 | { 66 | FILE * fp; 67 | struct product p; 68 | int count,id; 69 | 70 | 71 | fp = fopen(FILENAME, "rb"); 72 | 73 | while(1) 74 | { 75 | printf("\nEnter product id [0 to stop] : "); 76 | scanf("%d",&id); 77 | if ( id == 0) 78 | break; 79 | 80 | fseek(fp, (id - 1) * sizeof(p), 0) ; 81 | count = fread(&p, sizeof(p), 1, fp); 82 | if (count == 0) 83 | printf("\nProduct Not Found\n"); 84 | else 85 | printf("\n%-30s %10d", p.name, p.price); 86 | 87 | } 88 | fclose(fp); 89 | printf("\n\n"); 90 | } 91 | 92 | 93 | main() 94 | { 95 | int choice; 96 | 97 | while(1) 98 | { 99 | printf("\n **** MENU *****\n"); 100 | printf("\n1. List Products"); 101 | printf("\n2. Add Product"); 102 | printf("\n3. Search Product"); 103 | printf("\n4. Exit"); 104 | 105 | printf("\nChoice [1-4]: "); 106 | 107 | scanf("%d",&choice) ; 108 | 109 | switch(choice) 110 | { 111 | case 1 : list_products(); break; 112 | case 2 : add_product(); break; 113 | case 3 : search_product(); break; 114 | case 4: exit(0); 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /files/Products Binary File Example: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct product 4 | { 5 | char name[50]; 6 | int price; 7 | }; 8 | 9 | 10 | main() 11 | { 12 | FILE * fp; 13 | int choice; 14 | 15 | fp = fopen("products.dat","r+b"); 16 | if ( fp == NULL) 17 | { 18 | printf("Sorry! File not found!"); 19 | exit(1); 20 | } 21 | 22 | while(1) 23 | { 24 | printf("\nMenu\n"); 25 | printf("=========\n"); 26 | printf("1.Add Product\n"); 27 | printf("2.List Products\n"); 28 | printf("3.Search Products\n"); 29 | printf("4.Update Product\n"); 30 | printf("6.Exit\n"); 31 | printf("Your Choice [1-6] :"); 32 | scanf("%d",&choice); 33 | 34 | switch(choice) 35 | { 36 | case 1 : add(fp); break; 37 | case 2 : list(fp); break; 38 | case 3 : search(fp); break; 39 | case 4 : update(fp); break; 40 | case 6 : fclose(fp); 41 | exit(0); 42 | } 43 | } 44 | } 45 | 46 | 47 | void add(FILE * fp) 48 | { 49 | struct product p; 50 | 51 | fseek(fp,0,2); // go to end of file 52 | fflush(stdin); 53 | printf("Enter product name : "); 54 | gets(p.name); 55 | 56 | printf("Enter product price: "); 57 | scanf("%d",&p.price); 58 | 59 | fwrite(&p,sizeof(p),1,fp); 60 | printf("\nProduct Added Successfully!\n"); 61 | } 62 | 63 | void list(FILE *fp) 64 | { 65 | struct product p; 66 | int count; 67 | 68 | fseek(fp,0,0); 69 | 70 | printf("\n\n******* List Of Products ********** \n"); 71 | 72 | while(1) 73 | { 74 | count = fread(&p,sizeof(p),1,fp); 75 | if ( count == 0) 76 | break; 77 | // print 78 | printf("\n%-50s %6d", p.name, p.price); 79 | } 80 | 81 | printf("\n\n"); 82 | 83 | } 84 | 85 | 86 | void search(FILE *fp) 87 | { 88 | struct product p; 89 | int count; 90 | char sword[20]; 91 | 92 | fseek(fp,0,0); // take pointer to beginning of file 93 | fflush(stdin); // clear keyboard buffer 94 | 95 | printf("Enter search string :"); 96 | gets(sword); 97 | 98 | printf("\n\n******* List Of Products ********** \n"); 99 | 100 | while(1) 101 | { 102 | count = fread(&p,sizeof(p),1,fp); 103 | if (count == 0) 104 | break; 105 | 106 | if (strstr( p.name, sword) != NULL) 107 | printf("\n%-50s %6d", p.name, p.price); 108 | } 109 | 110 | printf("\n\n"); 111 | } 112 | 113 | 114 | void update(FILE * fp) 115 | { 116 | struct product p; 117 | int pno; 118 | 119 | fflush(stdin); 120 | printf("Enter product number : "); 121 | scanf("%d", &pno); 122 | 123 | fseek(fp, (pno-1) * sizeof(p),0); // move to required product 124 | fread(&p,sizeof(p),1,fp); 125 | 126 | printf("Enter new price : "); 127 | scanf("%d",&p.price); 128 | 129 | fseek(fp, (pno-1) * sizeof(p),0); // move to required product 130 | fwrite(&p,sizeof(p),1,fp); // overwrite existing product 131 | printf("\nProduct Updated Successfully!\n"); 132 | } 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | -------------------------------------------------------------------------------- /files/Read Names From File: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | char name[20], *p; 5 | FILE * fp; 6 | int ln; 7 | 8 | fp = fopen("names.txt","r"); 9 | if ( fp == NULL) 10 | { 11 | printf("Sorry! File cannot be opened!"); 12 | exit(1); 13 | } 14 | ln=1; 15 | while(1) 16 | { 17 | p = fgets(name,20,fp); 18 | if (p == NULL) // reached EOF 19 | break; 20 | 21 | printf("%3d: %s",ln++,name); 22 | } 23 | 24 | fclose(fp); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /files/Remove Blank Lines: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | char sfn[50], tfn[50]; 6 | FILE * sfp, * tfp; 7 | char line[200]; 8 | 9 | printf("Enter source filename :"); 10 | gets(sfn); 11 | printf("Enter target filename :"); 12 | gets(tfn); 13 | 14 | sfp = fopen(sfn,"r"); 15 | if (sfp == NULL) // file not opened successfully 16 | { 17 | printf("\nSorry! Source File Not Found!"); 18 | exit(1); // exit with status code 1 - error 19 | } 20 | 21 | tfp = fopen(tfn,"w"); 22 | if (tfp == NULL) // file not opened successfully 23 | { 24 | printf("\nSorry! Could not create target file!"); 25 | exit(2); 26 | } 27 | 28 | while(1) 29 | { 30 | if (fgets(line,200,sfp) == NULL) // reached end of file 31 | break; 32 | 33 | if (strlen(line) > 1) // one char is \n at the end 34 | fputs(line,tfp); 35 | } 36 | 37 | fclose(tfp); 38 | fclose(sfp); 39 | 40 | } 41 | -------------------------------------------------------------------------------- /files/RemoveBlankLines.c: -------------------------------------------------------------------------------- 1 | // Remove blank lines 2 | 3 | #include 4 | 5 | main() 6 | { 7 | char sfile[100], tfile[100], line[100], *p; 8 | FILE * sfp, *tfp; 9 | 10 | 11 | printf("Enter source file : "); 12 | gets(sfile); 13 | 14 | printf("Enter target file : "); 15 | gets(tfile); 16 | 17 | sfp = fopen(sfile,"r"); 18 | if ( sfp == NULL) 19 | { 20 | printf("Source File [%s] Could Not Be Opened!\n", sfile); 21 | exit(1); 22 | } 23 | 24 | tfp = fopen(tfile,"w"); 25 | if ( tfp == NULL) 26 | { 27 | printf("Target File [%s] Could Not Be Created!\n", tfile); 28 | exit(1); 29 | } 30 | 31 | 32 | while(1) 33 | { 34 | p = fgets(line,100,sfp); 35 | if (p == NULL) 36 | break; 37 | // length includes newline. So ignore lines with only 1 char 38 | if (strlen(line) > 1) 39 | fputs(line,tfp); 40 | } 41 | 42 | fclose(sfp); 43 | fclose(tfp); 44 | 45 | } // main() 46 | -------------------------------------------------------------------------------- /files/ShowFileWithLineNumbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | FILE * fp; 6 | char fn[100] = "f:\\c\\jan20\\colsum.c"; 7 | char line[100]; 8 | char * p; 9 | int lineno=1; 10 | 11 | 12 | fp = fopen(fn,"r"); 13 | if ( fp == NULL) 14 | { 15 | printf("\nSorry! File not found!\n"); 16 | exit(1); 17 | } 18 | 19 | while(1) 20 | { 21 | p = fgets(line,100,fp); // read a line 22 | if ( p == NULL) 23 | break; 24 | printf("%3d: %s",lineno,line); // print line with line number 25 | lineno++; 26 | } 27 | fclose(fp); 28 | } 29 | -------------------------------------------------------------------------------- /files/Students Data Process: -------------------------------------------------------------------------------- 1 | write_students.c 2 | =============== 3 | #include 4 | 5 | 6 | struct student 7 | { 8 | int admno; 9 | char name[30]; 10 | }; 11 | 12 | main() 13 | { 14 | struct student s1; 15 | FILE * fp; 16 | 17 | 18 | fp = fopen("students.dat","wb"); 19 | if ( fp == NULL) 20 | { 21 | printf("Sorry! Could not create file!"); 22 | exit(1); 23 | } 24 | 25 | while(1) 26 | { 27 | printf("Enter admno :"); 28 | scanf("%d",&s1.admno); 29 | if (s1.admno == 0) 30 | break; 31 | 32 | scanf("%s", s1.name); 33 | 34 | fwrite(&s1, sizeof(s1),1,fp); 35 | } 36 | 37 | fclose(fp); 38 | } 39 | 40 | 41 | read_students.c 42 | =============== 43 | #include 44 | 45 | 46 | struct student 47 | { 48 | int admno; 49 | char name[30]; 50 | }; 51 | 52 | main() 53 | { 54 | struct student s1; 55 | FILE * fp; 56 | int count; 57 | 58 | fp = fopen("students.dat","rb"); 59 | if ( fp == NULL) 60 | { 61 | printf("Sorry! Could not create file!"); 62 | exit(1); 63 | } 64 | 65 | while(1) 66 | { 67 | count = fread(&s1, sizeof(s1),1,fp); 68 | if ( count == 0) 69 | break; 70 | 71 | printf("\n%5d %-20s",s1.admno,s1.name); 72 | } 73 | 74 | fclose(fp); 75 | } 76 | 77 | update_student.c 78 | =============== 79 | #include 80 | 81 | struct student 82 | { 83 | int admno; 84 | char name[30]; 85 | }; 86 | 87 | main() 88 | { 89 | struct student s1; 90 | FILE * fp; 91 | int count, admno; 92 | 93 | fp = fopen("students.dat","r+b"); 94 | if ( fp == NULL) 95 | { 96 | printf("Sorry! Could not create file!"); 97 | exit(1); 98 | } 99 | 100 | while(1) 101 | { 102 | printf("\nEnter admno [0 to stop] :"); 103 | scanf("%d",&admno); 104 | 105 | if (admno == 0) 106 | break; 107 | 108 | fseek(fp, sizeof(struct student) * (admno-1), 0); 109 | count = fread(&s1, sizeof(s1),1,fp); 110 | if (count == 1) 111 | { 112 | printf("\nEnter new name : "); 113 | scanf("%s",s1.name); 114 | // reset to same position 115 | fseek(fp, sizeof(struct student) * (admno-1), 0); 116 | fwrite(&s1, sizeof(struct student), 1,fp); 117 | printf("\nName successfully updated!\n"); 118 | } 119 | else 120 | printf("\nSorry! Could not read student!\n"); 121 | } // while 122 | 123 | fclose(fp); 124 | } 125 | 126 | 127 | search_student.c 128 | ================ 129 | #include 130 | 131 | 132 | struct student 133 | { 134 | int admno; 135 | char name[30]; 136 | }; 137 | 138 | main() 139 | { 140 | struct student s1; 141 | FILE * fp; 142 | int count, admno; 143 | 144 | fp = fopen("students.dat","rb"); 145 | if ( fp == NULL) 146 | { 147 | printf("Sorry! Could not create file!"); 148 | exit(1); 149 | } 150 | 151 | while(1) 152 | { 153 | printf("\nEnter admno [0 to stop] :"); 154 | scanf("%d",&admno); 155 | 156 | if (admno == 0) 157 | break; 158 | 159 | fseek(fp, sizeof(struct student) * (admno-1), 0); 160 | count = fread(&s1, sizeof(s1),1,fp); 161 | if (count ==1) 162 | printf("\n%s\n",s1.name); 163 | else 164 | printf("\nSorry! Could not read student!\n"); 165 | } // while 166 | 167 | fclose(fp); 168 | } 169 | -------------------------------------------------------------------------------- /files/Students Database Operations: -------------------------------------------------------------------------------- 1 | // Store and manipulate data related to students 2 | 3 | #include 4 | #define FILENAME "students.dat" 5 | struct student { 6 | int admno; 7 | char name [30]; 8 | int marks; 9 | }; 10 | 11 | 12 | main() 13 | { 14 | int choice; 15 | FILE * fp; 16 | 17 | 18 | // open file 19 | fp = fopen(FILENAME,"r+b"); 20 | if (fp == NULL) 21 | { 22 | fp = fopen(FILENAME,"w+b"); 23 | if (fp == NULL) 24 | { 25 | printf("Sorry! File could not be opened. Quitting....\n"); 26 | exit(1); 27 | } 28 | } 29 | 30 | while(1) 31 | { 32 | printf("\n *** MENU ******\n"); 33 | printf("\n1. Add Student\n"); 34 | printf("\n2. List Students\n"); 35 | printf("\n3. Update Student\n"); 36 | printf("\n4. Exit\n"); 37 | printf("\nChoice :[1-4] :"); 38 | scanf("%d",&choice); 39 | 40 | switch(choice) 41 | { 42 | case 1 : add_student(fp); break; 43 | case 2 : list_students(fp); break; 44 | case 3 : update_student(fp); break; 45 | case 4 : exit(0); 46 | } 47 | } 48 | } 49 | 50 | void add_student(FILE * fp) 51 | { 52 | struct student s; 53 | int count; 54 | 55 | printf("Enter admno : "); 56 | scanf("%d", &s.admno); 57 | 58 | fflush(stdin); 59 | 60 | printf("Enter name : "); 61 | gets(s.name); 62 | 63 | printf("Enter marks : "); 64 | scanf("%d",&s.marks); 65 | 66 | // go to end of the file 67 | fseek(fp,0, SEEK_END); 68 | // Write student block at the end 69 | count = fwrite(&s, sizeof(s), 1,fp); 70 | if ( count == 1) 71 | printf("\nAdded Student SuccessfullY!\n"); 72 | 73 | } 74 | 75 | 76 | void list_students(FILE * fp) 77 | { 78 | struct student s; 79 | int count; 80 | 81 | 82 | // go to beginning of the file 83 | fseek(fp,0, SEEK_SET); 84 | 85 | printf("\n\n***** Students List ********\n\n"); 86 | 87 | while(1) 88 | { 89 | count = fread(&s,sizeof(s),1,fp); 90 | if (count != 1) 91 | break; 92 | 93 | printf("%4d %-30s %3d\n", s.admno, s.name, s.marks); 94 | } 95 | } 96 | 97 | 98 | void update_student(FILE * fp) 99 | { 100 | struct student s; 101 | int count, admno; 102 | 103 | 104 | // go to beginning of the file 105 | fseek(fp,0, SEEK_SET); 106 | 107 | printf("\nEnter admno : "); 108 | scanf("%d",&admno); 109 | 110 | while(1) 111 | { 112 | count = fread(&s,sizeof(s),1,fp); 113 | if (count != 1) 114 | break; 115 | 116 | if (s.admno == admno) 117 | { 118 | printf("\nEnter Marks : "); 119 | scanf("%d",&s.marks); 120 | // go back to previous block 121 | fseek(fp, sizeof(s) * -1l ,SEEK_CUR); 122 | // Overwrite the block with block in memory 123 | count = fwrite(&s, sizeof(s), 1,fp); 124 | if (count == 1) 125 | printf("\n\nUpdated Student SuccessfullY!\n\n"); 126 | else 127 | printf("\n\nSorry! Unable to update student.\n\n"); 128 | 129 | break; 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /files/UpdateTime.c: -------------------------------------------------------------------------------- 1 | // Update time (increments time by one second) at the given position 2 | // Uses birnary files, random access and struct time 3 | 4 | #include 5 | 6 | struct time { 7 | int h,m,s; 8 | }; 9 | 10 | struct time next_second(struct time t) 11 | { 12 | t.s ++; 13 | if (t.s > 59) 14 | { 15 | t.s = 0; 16 | t.m ++; 17 | if (t.m > 59) 18 | { 19 | t.m = 0; 20 | t.h ++; 21 | if(t.h > 23) 22 | t.h = 0; 23 | } 24 | } 25 | return t; 26 | } 27 | main() 28 | { 29 | FILE * fp; 30 | struct time t; 31 | int count; 32 | int pos, result; 33 | // make sure you have this file with some data 34 | fp = fopen("times.dat","r+b"); 35 | while(1) 36 | { 37 | printf("\nEnter position [0 to stop] : "); 38 | scanf("%d",&pos); 39 | 40 | if (pos == 0) 41 | break; 42 | 43 | result = fseek(fp, (pos-1) * sizeof(t),0); // got to required record 44 | if (result != 0) 45 | { 46 | printf("\nSorry! Position not found!"); 47 | continue; 48 | } 49 | 50 | count = fread(&t,sizeof(t),1,fp); // read record 51 | if (count == 0) 52 | { 53 | printf("\nSorry! Could not read record!"); 54 | continue; 55 | } 56 | 57 | t = next_second(t); // change time to next second 58 | fseek(fp, (pos-1) * sizeof(t),0); // go back to record 59 | fwrite(&t,sizeof(t),1,fp); // overwrite record with new time 60 | printf("\nUpdated Time!"); 61 | } 62 | 63 | fclose(fp); 64 | } 65 | -------------------------------------------------------------------------------- /files/Valdiate Mobile Numbers: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int is_mobile_number(char * s) 5 | { 6 | if (strlen(s) != 10) 7 | return 0; 8 | 9 | int i; 10 | 11 | for(i=0; i < 10; i++) 12 | { 13 | if ( !isdigit(s[i])) 14 | return 0; 15 | } 16 | 17 | return 1; 18 | } 19 | 20 | // removes newline that is at the end of the string 21 | void remove_newline(char * s) 22 | { 23 | int i; 24 | 25 | for(i = 0; s[i] ; i++) 26 | { 27 | if ( s[i] == '\n') 28 | { 29 | s[i] = '\0'; 30 | break; 31 | } 32 | } 33 | } 34 | 35 | main() 36 | { 37 | FILE * fp; 38 | int lineno = 0; 39 | char * p; 40 | char line[100]; 41 | 42 | 43 | fp = fopen("mobilenumbers.txt","r"); 44 | if(fp == NULL) 45 | { 46 | printf("File cannot be opened!"); 47 | exit(1); 48 | } 49 | 50 | while(1) 51 | { 52 | p = fgets(line,100,fp); 53 | if (p == NULL) 54 | break; 55 | 56 | 57 | // remove newline (\n) at the end 58 | remove_newline(line); 59 | lineno++; 60 | 61 | if (strlen(line) == 0) 62 | continue; 63 | 64 | if ( !is_mobile_number(line)) 65 | printf("%2d: %s\n", lineno, line); 66 | 67 | 68 | } 69 | 70 | fclose(fp); 71 | 72 | } 73 | -------------------------------------------------------------------------------- /files/Write Names to File: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | char name[20]; 5 | FILE * fp; 6 | 7 | fp = fopen("names.txt","w"); 8 | if ( fp == NULL) 9 | { 10 | printf("Sorry! File cannot be opened!"); 11 | exit(1); 12 | } 13 | while(1) 14 | { 15 | printf("Enter a name [end to stop] :"); 16 | gets(name); 17 | if (stricmp(name,"end") == 0) 18 | break; 19 | 20 | fputs(name,fp); 21 | fputc('\n',fp); 22 | } 23 | 24 | fclose(fp); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /files/Write and read marks: -------------------------------------------------------------------------------- 1 | WriteMarks.c 2 | ============= 3 | // Wrte Marks to marks.txt 4 | 5 | #include 6 | main() 7 | { 8 | FILE * fp; 9 | int marks; 10 | 11 | fp = fopen("marks.txt","w"); // open file in write mode 12 | if(fp == NULL) 13 | { 14 | printf("Sorry! Could not open file. Quitting..."); 15 | return; 16 | } 17 | while(1) 18 | { 19 | printf("Enter marks [0 to stop] : "); 20 | scanf("%d",&marks); 21 | 22 | if (marks == 0) 23 | break; 24 | 25 | fprintf(fp,"%d\n",marks); // write a line 26 | } 27 | fclose(fp); 28 | } 29 | 30 | 31 | ReadMarks.c 32 | ============= 33 | 34 | // Reads marks from marks.txt and displays average of marks 35 | #include 36 | main() 37 | { 38 | FILE * fp; 39 | int total = 0, count = 0, marks; 40 | char line[5]; 41 | 42 | fp = fopen("marks.txt","r"); // open file in write mode 43 | if(fp == NULL) 44 | { 45 | printf("Sorry! Could not open file. Quitting..."); 46 | return; 47 | } 48 | while(1) 49 | { 50 | // Read a line from file and stop when it returns NULL 51 | if (fgets(line,5,fp) == NULL) 52 | break; 53 | 54 | marks = atoi(line); // Convert string to int 55 | total += marks; 56 | count ++; 57 | printf("%d %s",strlen(line),line); 58 | } 59 | fclose(fp); 60 | printf("\nCount : %d, Total : %d, Average : %5.2f",count, total, total / (float) count); 61 | 62 | } 63 | -------------------------------------------------------------------------------- /files/char_count.c: -------------------------------------------------------------------------------- 1 | // Counts Alpha, Digits, Whitespaces and others 2 | #include 3 | main() 4 | { 5 | FILE * fp; 6 | int alpha,digits,whitespace, others, ch; 7 | char filename[50]; 8 | 9 | printf("Enter filename :"); 10 | gets(filename); 11 | 12 | fp = fopen(filename,"r"); // open file in write mode 13 | if(fp == NULL) 14 | { 15 | printf("Sorry! Could not open file. Quitting..."); 16 | return; 17 | } 18 | 19 | alpha = digits = whitespace = others = 0; 20 | while(1) 21 | { 22 | ch = fgetc(fp); 23 | if (ch == EOF) 24 | break; 25 | 26 | if(isalpha(ch)) 27 | alpha++; 28 | else 29 | if (isdigit(ch)) 30 | digits ++; 31 | else 32 | if (isspace(ch)) 33 | whitespace ++; 34 | else 35 | others ++; 36 | } 37 | fclose(fp); 38 | 39 | printf("\nAlpha : %d", alpha); 40 | printf("\nDigits : %d", digits); 41 | printf("\nWhitespaces : %d", whitespace); 42 | printf("\nOthers : %d", others); 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /files/compare_files.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | FILE * open(char filename[]) 4 | { 5 | FILE * fp; 6 | fp = fopen(filename,"r"); 7 | if (fp == NULL) 8 | { 9 | printf("\n[%s] not found.\n",filename); 10 | exit(1); // terminate program 11 | } 12 | return fp; 13 | } 14 | main() 15 | { 16 | FILE *fp1,*fp2; 17 | char line1[100],line2[100]; 18 | char *cp1,*cp2; 19 | int res, lineno=0; 20 | char filename1[100] = "f:\\c\\may6\\names1.tt"; 21 | char filename2[100] = "f:\\c\\may6\\names2.txt"; 22 | 23 | // open files 24 | 25 | fp1 = open(filename1); 26 | fp2 = open(filename2); 27 | 28 | while(1) 29 | { 30 | lineno++; 31 | cp1 = fgets(line1,100,fp1); 32 | cp2 = fgets(line2,100,fp2); 33 | 34 | if (cp1 == NULL && cp2 == NULL) 35 | { 36 | printf("Equal"); 37 | break; 38 | } 39 | if ( cp1 == NULL || cp2 == NULL) 40 | { 41 | printf("Files differ in line number [%d]\n", lineno); 42 | break; 43 | } 44 | 45 | res = strcmp(line1,line2); 46 | 47 | if ( res != 0) 48 | { 49 | printf("Files differ in line number [%d]\n", lineno); 50 | break; 51 | } 52 | } 53 | 54 | fclose(fp1); 55 | fclose(fp2); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /files/customers_manager.c: -------------------------------------------------------------------------------- 1 | // Customers manager 2 | 3 | #include 4 | #define FILENAME "customers.dat" 5 | 6 | struct customer 7 | { 8 | char name[30], email[50], phone[50]; 9 | }; 10 | 11 | typedef struct customer CUSTOMER; 12 | 13 | void add_customer() 14 | { 15 | FILE * fp; 16 | CUSTOMER c; 17 | 18 | // Open file in append binary mode 19 | fp = fopen(FILENAME,"ab"); 20 | // check whether file is opened - to do 21 | fflush(stdin); // clear keyboard buffer 22 | printf("\nEnter name :"); 23 | gets(c.name); 24 | printf("Enter email :"); 25 | gets(c.email); 26 | printf("Enter phone :"); 27 | gets(c.phone); 28 | 29 | fwrite(&c,sizeof(c),1,fp); 30 | fclose(fp); 31 | } 32 | 33 | void list_customers() 34 | { 35 | FILE * fp; 36 | CUSTOMER c; 37 | int count; 38 | 39 | fp = fopen(FILENAME,"rb"); 40 | printf("\n\n *** Customers ***\n"); 41 | while(1) 42 | { 43 | count = fread(&c,sizeof(c),1,fp); 44 | // if count is 0 then it is EOF 45 | if (count == 0) 46 | break; 47 | printf("\n%-30s %-30s %s",c.name, c.email,c.phone); 48 | } 49 | fclose(fp); 50 | printf("\n\n****** The End *******\n"); 51 | } 52 | 53 | // Take customer number and new phone number to update file 54 | void update_customer() 55 | { 56 | FILE * fp; 57 | CUSTOMER c; 58 | int count,id; 59 | 60 | fp = fopen(FILENAME,"r+b"); 61 | // customer id is position of customer in the file 62 | printf("Enter customer id : "); 63 | scanf("%d",&id); 64 | 65 | // Move to record for the given customer 66 | fseek(fp, sizeof(c) * (id-1),SEEK_SET); 67 | fread(&c,sizeof(c),1,fp); // read customer details from file 68 | printf("New Phone number : "); 69 | scanf("%s",c.phone); 70 | // Move back to record 71 | fseek(fp, sizeof(c) * (id-1),SEEK_SET); 72 | // Write block from memory to file 73 | fwrite(&c,sizeof(c),1,fp); 74 | fclose(fp); 75 | } 76 | 77 | main() 78 | { 79 | int choice; 80 | 81 | while(1) 82 | { 83 | printf("\n*** Menu ***\n"); 84 | printf("\n 1. Add Customer"); 85 | printf("\n 2. List Customers"); 86 | printf("\n 3. Update Customer"); 87 | printf("\n 4. Exit"); 88 | printf("\n Choice [1-4] : "); 89 | scanf("%d",&choice); 90 | 91 | switch(choice) 92 | { 93 | case 1 : add_customer(); 94 | break; 95 | case 2 : list_customers(); 96 | break; 97 | case 3 : update_customer(); 98 | break; 99 | case 4 : exit(1); 100 | } 101 | } // while 102 | } 103 | -------------------------------------------------------------------------------- /files/delete_marks_from_file.c: -------------------------------------------------------------------------------- 1 | // Program to delete marks from marks.txt that are < 60 2 | 3 | #include 4 | #define FILENAME "marks.txt" 5 | 6 | main() 7 | { 8 | FILE * fp; 9 | char * cp; 10 | int * p; 11 | int count, pos,i; 12 | char line[10]; 13 | 14 | fp = fopen(FILENAME,"rt"); 15 | if(fp == NULL) 16 | { 17 | printf("Sorry! File cannot be opened!"); 18 | exit(1); 19 | } 20 | count = 0; 21 | while(1) 22 | { 23 | cp = fgets(line,10,fp); 24 | if (cp == NULL) 25 | break; 26 | count ++; 27 | } 28 | 29 | // printf("No. of marks : %d\n",count); 30 | 31 | // take memory to store all marks 32 | p = (int*) malloc(count * sizeof(int)); 33 | // take file pointer to beginning of file 34 | fseek(fp,0,SEEK_SET); 35 | 36 | // Read and copy each line into an element of the array 37 | pos = 0; 38 | while(1) 39 | { 40 | cp = fgets(line,10,fp); 41 | if (cp == NULL) 42 | break; 43 | p[pos] = atoi(line); // convert string to int 44 | pos ++; 45 | } 46 | fclose(fp); 47 | 48 | // Write marks >= 60 into file 49 | fp = fopen(FILENAME,"wt"); 50 | for(i = 0; i < count ; i ++) 51 | if (p[i] >= 60) 52 | fprintf(fp,"%d\n", p[i]); 53 | 54 | fclose(fp); 55 | } 56 | 57 | marks.txt 58 | ======== 59 | 66 60 | 90 61 | 68 62 | 99 63 | 88 64 | 77 65 | 78 66 | 85 67 | 67 68 | 98 69 | 33 70 | 44 71 | 68 72 | 91 73 | 81 74 | 55 75 | 77 76 | 78 77 | 45 78 | 85 79 | 67 80 | 98 81 | 82 | marks.txt after program is run 83 | =============================== 84 | 66 85 | 90 86 | 68 87 | 99 88 | 88 89 | 77 90 | 78 91 | 85 92 | 67 93 | 98 94 | 68 95 | 91 96 | 81 97 | 77 98 | 78 99 | 85 100 | 67 101 | 98 102 | 103 | -------------------------------------------------------------------------------- /files/file_char_types_count.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | FILE * fp; 6 | int ch, alpha, digits, others; 7 | char filename[50] = "f:\\c\\feb9\\test.txt"; 8 | 9 | 10 | fp = fopen(filename,"r"); 11 | if ( fp == NULL) 12 | { 13 | printf("Sorry! File [%s] not found!\n",filename); 14 | exit(1); 15 | } 16 | 17 | alpha = digits = others = 0; 18 | ch = fgetc(fp); 19 | while ( ch != -1) // -1 means EOF 20 | { 21 | if (isalpha(ch)) 22 | alpha ++; 23 | else 24 | if ( isdigit(ch)) 25 | digits ++; 26 | else 27 | others ++; 28 | ch = fgetc(fp); 29 | } 30 | 31 | fclose(fp); 32 | 33 | printf("\nNo. of alphabets : %d", alpha); 34 | printf("\nNo. of digits : %d", digits); 35 | printf("\nNo. of other chars : %d", others); 36 | 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /files/file_utilities.cs: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | main() 5 | { 6 | int choice; 7 | 8 | while(1) 9 | { 10 | printf("\nMenu\n"); 11 | printf("=========\n"); 12 | printf("1.Encrypt File\n"); 13 | printf("2.Decrypt File\n"); 14 | printf("3.File stats \n"); 15 | printf("4.Exit\n"); 16 | printf("Your Choice [1-4] :"); 17 | scanf("%d",&choice); 18 | 19 | switch(choice) 20 | { 21 | case 1 : encrypt(); break; 22 | case 2 : decrypt(); break; 23 | case 3 : file_stats(); break; 24 | case 4 : exit(0); 25 | } 26 | } 27 | 28 | } 29 | 30 | void encrypt() 31 | { 32 | char sfilename[50], tfilename[50]; 33 | FILE *sfp,*tfp; 34 | int ch; 35 | 36 | // clear keyboard buffer 37 | fflush(stdin); 38 | printf("\nSource Filename : "); 39 | gets(sfilename); 40 | 41 | printf("\nTarget Filename : "); 42 | gets(tfilename); 43 | 44 | sfp = fopen(sfilename,"r"); 45 | if( sfp == NULL) 46 | { 47 | printf("\nSorry! Source file not found.\n"); 48 | return; 49 | } 50 | 51 | tfp = fopen(tfilename,"w"); 52 | if( tfp == NULL) 53 | { 54 | printf("\nSorry! Target file could not be opened.\n"); 55 | return; 56 | } 57 | 58 | ch = fgetc(sfp); 59 | while ( ch!= -1) 60 | { 61 | fputc(ch+1,tfp); 62 | ch = fgetc(sfp); 63 | } 64 | 65 | fclose(tfp); 66 | fclose(sfp); 67 | 68 | printf("\nSuccessfully encrypted %s and copied to %s\n",sfilename, tfilename); 69 | 70 | } 71 | 72 | 73 | void decrypt() 74 | { 75 | char sfilename[50], tfilename[50]; 76 | FILE *sfp,*tfp; 77 | int ch; 78 | 79 | // clear keyboard buffer 80 | fflush(stdin); 81 | printf("\nSource Filename : "); 82 | gets(sfilename); 83 | 84 | printf("\nTarget Filename : "); 85 | gets(tfilename); 86 | 87 | sfp = fopen(sfilename,"r"); 88 | if( sfp == NULL) 89 | { 90 | printf("\nSorry! Source file not found.\n"); 91 | return; 92 | } 93 | 94 | tfp = fopen(tfilename,"w"); 95 | if( tfp == NULL) 96 | { 97 | printf("\nSorry! Target file could not be opened.\n"); 98 | return; 99 | } 100 | 101 | ch = fgetc(sfp); 102 | while ( ch!= -1) 103 | { 104 | fputc(ch-1,tfp); 105 | ch = fgetc(sfp); 106 | } 107 | 108 | fclose(tfp); 109 | fclose(sfp); 110 | 111 | printf("\nSuccessfully decrypted %s and copied to %s\n",sfilename, tfilename); 112 | } 113 | 114 | void file_stats() 115 | { 116 | char filename[50]; 117 | FILE *fp; 118 | int ch, nolines =0, nowords =0, nochars=0; 119 | 120 | // clear keyboard buffer 121 | fflush(stdin); 122 | printf("\nFilename : "); 123 | gets(filename); 124 | 125 | fp = fopen(filename,"r"); 126 | if(fp == NULL) 127 | { 128 | printf("\nSorry! File not found.\n"); 129 | return; 130 | } 131 | 132 | 133 | ch = fgetc(fp); 134 | while ( ch!= -1) 135 | { 136 | nochars++; 137 | if ( ch == 32){ 138 | nowords ++; 139 | } 140 | else 141 | if ( ch == 10) 142 | { 143 | nolines++; 144 | nowords++; 145 | } 146 | 147 | ch = fgetc(fp); 148 | } 149 | 150 | fclose(fp); 151 | 152 | printf("\nNo. of characters : %d\n", nochars); 153 | printf("\nNo. of words : %d\n", nowords); 154 | printf("\nNo. of Lines : %d\n", nolines); 155 | } 156 | -------------------------------------------------------------------------------- /files/manager_products.c: -------------------------------------------------------------------------------- 1 | // Products Manager 2 | #include 3 | #define FILENAME "products.dat" 4 | #define TEMPFILE "tempproducts.dat" 5 | 6 | struct product 7 | { 8 | char name[20]; 9 | int price; 10 | }; 11 | 12 | main() 13 | { 14 | int choice; 15 | 16 | while(1) 17 | { 18 | printf("\n\nMenu\n"); 19 | printf("============\n"); 20 | printf("1.Add Product\n"); 21 | printf("2.List Products\n"); 22 | printf("3.Change Price\n"); 23 | printf("4.Delete Product\n"); 24 | printf("5.Search Products\n"); 25 | printf("6.Exit\n"); 26 | 27 | printf("\nEnter choice [1-6] : "); 28 | scanf("%d",&choice); 29 | 30 | switch(choice) 31 | { 32 | case 1: add_product(); break; 33 | case 2: list_products(); break; 34 | case 3: change_price(); break; 35 | case 4: delete_product();break; 36 | case 5: search_products(); break; 37 | case 6: exit(0); 38 | } 39 | } // while 40 | } 41 | 42 | void add_product() 43 | { 44 | FILE * fp; 45 | struct product p; 46 | 47 | fp = fopen(FILENAME,"ab"); 48 | if (fp == NULL) 49 | { 50 | printf("Sorry! File could not be opened!"); 51 | return; 52 | } 53 | fflush(stdin); // clear keyboard buffer 54 | printf("Enter product name : "); 55 | gets(p.name); 56 | 57 | printf("Enter product price : "); 58 | scanf("%d",&p.price); 59 | 60 | fwrite(&p,sizeof(p),1,fp); 61 | 62 | fclose(fp); 63 | } 64 | 65 | void list_products() 66 | { 67 | FILE * fp; 68 | struct product p; 69 | int count; 70 | 71 | fp = fopen(FILENAME,"rb"); 72 | if (fp == NULL) 73 | { 74 | printf("Sorry! File could not be opened!"); 75 | return; 76 | } 77 | 78 | printf("\n\nList of Products\n"); 79 | printf("\n\n================\n"); 80 | 81 | while(1) 82 | { 83 | count = fread(&p,sizeof(p),1,fp); 84 | if(count == 0) // didn't read block successfully due to EOF 85 | break; 86 | 87 | printf("\n%-20s %d",p.name,p.price); 88 | } 89 | 90 | printf("\n\nPress any key to continue..."); 91 | getch(); 92 | 93 | fclose(fp); 94 | } 95 | 96 | void search_products() 97 | { 98 | FILE * fp; 99 | struct product p; 100 | char name[20]; 101 | int count; 102 | 103 | fp = fopen(FILENAME,"rb"); 104 | if (fp == NULL) 105 | { 106 | printf("Sorry! File could not be opened!"); 107 | return; 108 | } 109 | 110 | fflush(stdin); 111 | printf("\nEnter product name : "); 112 | gets(name); 113 | 114 | printf("\n\nSearch Result\n"); 115 | printf("================\n"); 116 | 117 | while(1) 118 | { 119 | count = fread(&p,sizeof(p),1,fp); 120 | if(count == 0) // didn't read block successfully due to EOF 121 | break; 122 | if(strstr(p.name,name) != 0) 123 | printf("\n%-20s %d",p.name,p.price); 124 | } 125 | 126 | fclose(fp); 127 | printf("\n\nPress any key to continue..."); 128 | getch(); 129 | } 130 | 131 | void change_price() 132 | { 133 | FILE * fp; 134 | struct product p; 135 | int prodid,pos; 136 | 137 | fp = fopen(FILENAME,"r+b"); 138 | if (fp == NULL) 139 | { 140 | printf("Sorry! File could not be opened!"); 141 | return; 142 | } 143 | 144 | printf("Enter product id : "); 145 | scanf("%d",&prodid); 146 | 147 | // get product for the given product id 148 | pos = (prodid - 1) * sizeof(struct product); 149 | // move file pointer to the required position 150 | fseek(fp,pos,SEEK_SET); 151 | // read product 152 | fread(&p,sizeof(p),1,fp); 153 | 154 | printf("\nPrice of the product : %d\n", p.price); 155 | printf("Enter new price : "); 156 | scanf("%d",&p.price); 157 | 158 | // move file pointer to the required position again 159 | fseek(fp,pos,SEEK_SET); 160 | fwrite(&p,sizeof(p),1,fp); 161 | fclose(fp); 162 | 163 | printf("\n\nChanged price successfully! Press any key to continue..."); 164 | getch(); 165 | } 166 | 167 | void delete_product() 168 | { 169 | FILE * fp, * tfp; 170 | struct product p; 171 | int count,prodid, pos=1; 172 | 173 | fp = fopen(FILENAME,"rb"); 174 | if (fp == NULL) 175 | { 176 | printf("Sorry! File could not be opened!"); 177 | return; 178 | } 179 | 180 | tfp = fopen(TEMPFILE,"wb"); 181 | if (tfp == NULL) 182 | { 183 | printf("Sorry! File could not be created!"); 184 | return; 185 | } 186 | 187 | printf("Enter product id :"); 188 | scanf("%d",&prodid); 189 | 190 | while(1) 191 | { 192 | count = fread(&p,sizeof(p),1,fp); 193 | if(count == 0) // didn't read block successfully due to EOF 194 | break; 195 | 196 | if(pos != prodid) 197 | fwrite(&p,sizeof(p),1,tfp); 198 | 199 | pos ++; 200 | } 201 | 202 | fclose(fp); 203 | fclose(tfp); 204 | //delete original file 205 | remove(FILENAME); 206 | //rename temp file to original file 207 | rename(TEMPFILE,FILENAME); 208 | 209 | printf("\n\nPress any key to continue..."); 210 | getch(); 211 | 212 | } 213 | -------------------------------------------------------------------------------- /files/marks_manager.c: -------------------------------------------------------------------------------- 1 | // Student Marks Manager 2 | # include 3 | # include 4 | # define FILENAME "marks.dat" 5 | 6 | struct student 7 | { 8 | char name[20]; 9 | int marks; 10 | }; 11 | 12 | void add_student() 13 | { 14 | FILE * fp; 15 | struct student s; 16 | 17 | fp = fopen(FILENAME,"ab"); 18 | if (fp == NULL) 19 | { 20 | printf("\nSorry! File cannot be opened!\n"); 21 | return; 22 | } 23 | while(1) 24 | { 25 | fflush(stdin); // clear keyboard buffer 26 | printf("Enter Student Name [Enter to stop] :"); 27 | gets(s.name); 28 | if(strlen(s.name) == 0) // user wants to stop 29 | break; 30 | printf("Enter Marks :"); 31 | scanf("%d",&s.marks); 32 | fwrite(&s,sizeof(s),1,fp); 33 | } 34 | fclose(fp); 35 | } 36 | 37 | void list_students() 38 | { 39 | FILE * fp; 40 | struct student s; 41 | int count; 42 | 43 | fp = fopen(FILENAME,"rb"); 44 | if (fp == NULL) 45 | { 46 | printf("\nSorry! File cannot be opened!\n"); 47 | return; 48 | } 49 | printf("\n\nList Of Students\n"); 50 | printf("=================\n"); 51 | while(1) 52 | { 53 | count = fread(&s,sizeof(s),1,fp); 54 | if (count == 0) 55 | break; 56 | printf("%-20s %3d\n",s.name,s.marks); 57 | } 58 | 59 | fclose(fp); 60 | } 61 | 62 | void update_marks() 63 | { 64 | FILE * fp; 65 | struct student s; 66 | int admno,count,pos; 67 | 68 | fp = fopen(FILENAME,"r+b"); 69 | if (fp == NULL) 70 | { 71 | printf("\nSorry! File cannot be opened!\n"); 72 | return; 73 | } 74 | printf("Enter Admno :"); 75 | scanf("%d",&admno); 76 | pos = (admno -1) * sizeof(s); 77 | fseek(fp,pos,SEEK_SET); // move to required record 78 | count = fread(&s,sizeof(s),1,fp); 79 | if (count == 1) 80 | { 81 | printf("Enter Marks :"); 82 | scanf("%d",&s.marks); 83 | fseek(fp,pos,SEEK_SET); // move back to starting of record 84 | fwrite(&s,sizeof(s),1,fp); 85 | } 86 | else 87 | printf("\nSorry! Student not found!\n"); 88 | 89 | fclose(fp); 90 | } 91 | 92 | 93 | main() 94 | { 95 | int choice; 96 | 97 | while(1) 98 | { 99 | printf("\nStudents Menu\n"); 100 | printf("=============\n"); 101 | printf("1. Add Student\n"); 102 | printf("2. List Students\n"); 103 | printf("3. Update marks\n"); 104 | printf("4. Exit\n"); 105 | printf("\nChoice[1-4]:"); 106 | scanf("%d",&choice); 107 | 108 | switch(choice) 109 | { 110 | case 1: add_student(); 111 | break; 112 | case 2: list_students(); 113 | break; 114 | case 3: update_marks(); 115 | break; 116 | case 4: return; 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /files/print_file_uppercase.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | char name[50]; 5 | int ch; 6 | FILE * fp; 7 | 8 | printf("Enter filename :"); 9 | gets(name); 10 | fp = fopen(name,"r"); 11 | if (fp == NULL) 12 | { 13 | printf("Sorry! File not found!\n"); 14 | exit(1); 15 | } 16 | ch = fgetc(fp); 17 | while ( ch!= -1) { 18 | putchar( toupper(ch)); 19 | ch = fgetc(fp); 20 | } 21 | fclose(fp); 22 | } 23 | -------------------------------------------------------------------------------- /files/print_long_names.c: -------------------------------------------------------------------------------- 1 | // Purpose: Print names that have more length than average length of names 2 | // Date : 20-Aug-2017 3 | // Author : Srikanth Pragada 4 | // Place : Vizag 5 | 6 | #include 7 | 8 | struct node 9 | { 10 | char name[20]; 11 | struct node * next; 12 | }; 13 | 14 | 15 | main() 16 | { 17 | struct node * root, * current, * prev; 18 | char name[20]; 19 | int count =0, total =0; 20 | int avg; 21 | 22 | root = NULL; 23 | 24 | while(1) 25 | { 26 | printf("Enter a name :"); 27 | gets(name); 28 | if ( strcmp(name,"end") == 0) 29 | break; 30 | 31 | total += strlen(name); 32 | count ++; 33 | 34 | // create a node 35 | current = (struct node *) malloc( sizeof(struct node)); 36 | strcpy(current->name,name); // copy name into node 37 | current->next = NULL; // make next point to nothing 38 | 39 | if(root == NULL) // first node 40 | root = current; 41 | else 42 | prev->next = current; // make prev node point to this node 43 | 44 | 45 | prev = current; // make current prev for next round 46 | } 47 | 48 | avg = total / count; 49 | 50 | // print names that have length more than avg length 51 | 52 | current = root; 53 | 54 | while(current != NULL) 55 | { 56 | if (strlen(current->name) >= avg) 57 | puts( current->name); 58 | 59 | current = current -> next; 60 | } 61 | 62 | 63 | } 64 | -------------------------------------------------------------------------------- /files/print_nonblank_lines.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int is_blank_line(char * s) 4 | { 5 | int i; 6 | 7 | for (i=0; s[i] ; i++) 8 | { 9 | if (!isspace(s[i])) 10 | return 0; // false; 11 | } 12 | 13 | return 1; // true 14 | } 15 | main() 16 | { 17 | FILE * fp; 18 | char line[200], filename[50]; 19 | char * p; 20 | int lineno=1; 21 | 22 | printf("Enter filename :"); 23 | gets(filename); 24 | 25 | fp = fopen(filename,"r"); 26 | if (fp == NULL) 27 | { 28 | printf("Sorry! [%s] could not be opened!",filename); 29 | exit(1); 30 | } 31 | while(1) 32 | { 33 | p = fgets(line,200,fp); 34 | if ( p == NULL) 35 | break; 36 | 37 | if( is_blank_line(line) ) 38 | continue; 39 | 40 | printf("%03d : %s",lineno,line); 41 | lineno ++; 42 | } 43 | 44 | fclose(fp); 45 | } 46 | -------------------------------------------------------------------------------- /files/printfile.c: -------------------------------------------------------------------------------- 1 | /* 2 | Program to print files given on command line along with line numbers 3 | */ 4 | 5 | #include 6 | 7 | main(int argc, char * argv[]) 8 | { 9 | FILE *fp; 10 | char * p; 11 | char name[100]; 12 | int lineno,i; 13 | 14 | 15 | if(argc == 1) // no parameters passed 16 | { 17 | printf("Usage : printfile filename1 [filename2] ..."); 18 | printf("\n\nPrints given files with line numbers."); 19 | printf("\n\nExample: printfile hello.c names.txt\n"); 20 | printf("Will print hello.c and names.txt with line numbers one after another.\n"); 21 | 22 | 23 | exit(0); 24 | } 25 | 26 | for(i = 1; i < argc ; i ++) 27 | { 28 | fp = fopen(argv[i],"r"); // Open in write mode 29 | if (fp == NULL) 30 | { 31 | printf("\n***********************************************"); 32 | printf("\nSorry! File [%s] cannot be opened!!\n", argv[i]); 33 | printf("***********************************************\n\n"); 34 | continue; 35 | } 36 | 37 | printf("\n\n*********** %s **************\n\n", argv[i]); 38 | lineno = 1; 39 | while(1) 40 | { 41 | p = fgets(name,100,fp); 42 | if (p == NULL) // EOF 43 | break; 44 | 45 | printf("%3d: %s",lineno,name); 46 | lineno ++; 47 | } 48 | 49 | fclose(fp); 50 | 51 | } // for loop 52 | } 53 | 54 | -------------------------------------------------------------------------------- /files/printfiles.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main(int argc, char * argv[]) 4 | { 5 | FILE * fp; 6 | char filename[30],line[100]; 7 | int lineno; 8 | int i; 9 | 10 | if ( argc == 1) 11 | { 12 | printf("\nSyntax : printfile ... \n"); 13 | exit(0); 14 | } 15 | 16 | for(i = 1; i < argc ; i ++) 17 | { 18 | fp = fopen(argv[i],"r"); 19 | if (fp == NULL) 20 | { 21 | printf("\n\n*********** Sorry! %s not found! **************\n\n",argv[i]); 22 | continue; 23 | } 24 | printf("\n\n*********%s*************\n\n", argv[i]); 25 | 26 | lineno = 1; 27 | while(1) 28 | { 29 | if( fgets(line,100,fp) == NULL) 30 | break; 31 | 32 | printf("%3d: %s",lineno,line); 33 | 34 | lineno++; 35 | } // while 36 | 37 | fclose(fp); 38 | } // for 39 | 40 | } // main 41 | -------------------------------------------------------------------------------- /files/products_manager.c: -------------------------------------------------------------------------------- 1 | // Products manager - Manages products in products.dat 2 | // Date : 26-JUNE-2018 3 | 4 | #include 5 | #define FILENAME "products.dat" 6 | #define TEMPFILE "temp.dat" 7 | 8 | struct product 9 | { 10 | char name [50]; 11 | int price,qoh; 12 | }; 13 | 14 | FILE * open_file(char * mode) 15 | { 16 | FILE * fp; 17 | 18 | fp = fopen(FILENAME, mode); 19 | if (fp == NULL) 20 | { 21 | printf("Sorry! File %s could not be opened in %s mode. Quitting.", FILENAME,mode); 22 | exit(1); 23 | } 24 | return fp; 25 | } 26 | 27 | void add_product() 28 | { 29 | FILE * fp; 30 | struct product p; 31 | 32 | fp = open_file("ab"); 33 | // clear keyboard buffer 34 | fflush(stdin); 35 | printf("Enter product name : "); 36 | gets(p.name); 37 | printf("Enter product price: "); 38 | scanf("%d",&p.price); 39 | printf("Enter product qty : "); 40 | scanf("%d",&p.qoh); 41 | fwrite(&p,sizeof(struct product),1,fp); 42 | fclose(fp); 43 | printf("\nSuccessfully added a new product.\n"); 44 | } 45 | 46 | void list_products() 47 | { 48 | FILE * fp; 49 | struct product p; 50 | int count; 51 | 52 | fp = open_file("rb"); 53 | printf("\nList Of Products\n"); 54 | 55 | while(1) 56 | { 57 | count = fread(&p,sizeof(struct product),1,fp); 58 | if (count == 0) // EOF 59 | break; 60 | printf("%-50s %10d %4d\n", p.name, p.price, p.qoh); 61 | } 62 | fclose(fp); 63 | } 64 | 65 | void get_product_details() 66 | { 67 | FILE * fp; 68 | struct product p; 69 | int count, prodid; 70 | 71 | fp = open_file("rb"); 72 | printf("Enter product id : "); 73 | scanf("%d",&prodid); 74 | fseek(fp, sizeof(struct product) * (prodid - 1), SEEK_SET); 75 | count = fread(&p,sizeof(struct product),1,fp); 76 | if (count == 1) 77 | printf("%-50s %10d %4d\n", p.name, p.price, p.qoh); 78 | else 79 | printf("\nProduct Id Not Found!\n"); 80 | fclose(fp); 81 | 82 | } 83 | 84 | 85 | void update_product_price() 86 | { 87 | FILE * fp; 88 | struct product p; 89 | int count, prodid; 90 | 91 | fp = open_file("r+b"); 92 | printf("Enter product id : "); 93 | scanf("%d",&prodid); 94 | fseek(fp, sizeof(struct product) * (prodid - 1), SEEK_SET); // 1 95 | count = fread(&p,sizeof(struct product),1,fp); // 2 96 | if (count == 1) { 97 | printf("Enter new price : "); 98 | scanf("%d",&p.price); // 3 99 | // write back to file 100 | fseek(fp, sizeof(struct product) * (prodid - 1), SEEK_SET); // 4 101 | count = fwrite(&p,sizeof(struct product),1,fp); // 5 102 | if (count == 1) 103 | printf("\nSuccessfully updated price!\n"); 104 | else 105 | printf("\nSorry! Could not update price!\n"); 106 | } 107 | else 108 | printf("\nProduct Id Not Found!\n"); 109 | fclose(fp); 110 | 111 | } 112 | 113 | void delete_product() 114 | { 115 | FILE * fp,*tp; 116 | struct product p; 117 | int id, count, pos=1, found = 0; 118 | 119 | fp = open_file("rb"); 120 | tp = fopen(TEMPFILE,"wb"); 121 | if (tp == NULL) 122 | { 123 | printf("Sorry! Could not create target file!"); 124 | return; 125 | } 126 | 127 | printf("Enter product id to delete : "); 128 | scanf("%d",&id); 129 | 130 | while(1) 131 | { 132 | count = fread(&p,sizeof(struct product),1,fp); 133 | if (count == 0) // EOF 134 | break; 135 | 136 | if (id != pos) // write except the one to delete 137 | fwrite(&p,sizeof(struct product),1,tp); 138 | else 139 | found = 1; 140 | 141 | pos ++; 142 | } 143 | 144 | fclose(fp); 145 | fclose(tp); 146 | 147 | if (found) 148 | { 149 | remove(FILENAME); 150 | rename(TEMPFILE,FILENAME); 151 | printf("Deleted Product Successfully!"); 152 | } 153 | else 154 | { 155 | printf("\nSorry! Couldn't find product id!\n"); 156 | } 157 | } 158 | 159 | void search_products() 160 | { 161 | FILE * fp; 162 | char name[20], newname[50]; 163 | struct product p; 164 | int count, found = 0; 165 | 166 | fp = open_file("rb"); 167 | printf("\nSearch Products\n"); 168 | printf("\n================\n"); 169 | printf("Enter product name :"); 170 | scanf("%s",name); 171 | 172 | while(1) 173 | { 174 | count = fread(&p,sizeof(struct product),1,fp); 175 | if (count == 0) // EOF 176 | break; 177 | strcpy(newname,p.name); 178 | if(strstr(strupr(newname), strupr(name)) != NULL) { 179 | printf("%-50s %10d %4d\n", p.name, p.price, p.qoh); 180 | found = 1; 181 | } 182 | } 183 | fclose(fp); 184 | 185 | if (!found) 186 | printf("\nSorry! No product found!\n"); 187 | } 188 | 189 | 190 | main() 191 | { 192 | int choice; 193 | 194 | while(1) 195 | { 196 | printf("\nMenu"); 197 | printf("\n====\n"); 198 | printf("1. Add Product\n"); 199 | printf("2. List Products\n"); 200 | printf("3. Product details\n"); 201 | printf("4. Update product\n"); 202 | printf("5. Delete product\n"); 203 | printf("6. Search products\n"); 204 | printf("7. Exit\n"); 205 | printf("Enter your choice [1-7]:"); 206 | scanf("%d",&choice); 207 | printf("\n"); 208 | 209 | switch(choice) 210 | { 211 | case 1 : add_product(); break; 212 | case 2 : list_products(); break; 213 | case 3 : get_product_details(); break; 214 | case 4 : update_product_price(); break; 215 | case 5 : delete_product(); break; 216 | case 6 : search_products(); break; 217 | case 7 : exit(0); 218 | } 219 | } 220 | 221 | } 222 | -------------------------------------------------------------------------------- /files/read_contacts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct contact 4 | { 5 | char name[30]; 6 | char email[50]; 7 | char mobile[25]; 8 | }; 9 | 10 | main() 11 | { 12 | struct contact c; 13 | FILE * fp; 14 | int pos; 15 | 16 | fp = fopen("contacts.dat","r+b"); 17 | 18 | if ( fp == NULL) 19 | { 20 | printf("Sorry! File cannot be opened!"); 21 | exit(1); 22 | } 23 | 24 | while(1) 25 | { 26 | printf("Enter contact number [1-5 , 0 to stop]:"); 27 | scanf("%d",&pos); 28 | if ( pos == 0) 29 | break; 30 | fseek(fp, (pos-1) * sizeof(struct contact),0); 31 | fread(&c,sizeof(c),1,fp); 32 | printf("\n%s %s %s\n", c.name,c.email,c.mobile); 33 | } 34 | 35 | fclose(fp); 36 | } 37 | -------------------------------------------------------------------------------- /files/readstudents.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct student 4 | { 5 | char name[30]; 6 | char mobile[11]; 7 | }; 8 | 9 | main() 10 | { 11 | struct student s; 12 | int rollno; 13 | FILE *fp; 14 | 15 | 16 | fp = fopen("students.dat","rb"); 17 | if (fp == NULL) 18 | { 19 | printf("\nSorry! Could not open file!"); 20 | return; 21 | } 22 | 23 | while(1) 24 | { 25 | printf("\nEnter roll number[0 to stop]:"); 26 | scanf("%d",&rollno); 27 | if (rollno == 0) 28 | break; 29 | 30 | fseek(fp,sizeof(s) * (rollno-1),0); 31 | 32 | fread(&s,sizeof(s),1,fp); 33 | printf("\nName : %s", s.name); 34 | printf("\nMobile : %s\n",s.mobile); 35 | 36 | } 37 | 38 | fclose(fp); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /files/remove_all_blank_lines.c: -------------------------------------------------------------------------------- 1 | // Remove blank lines from the given file 2 | 3 | #include 4 | #define TEMPFILE "tempfile.txt" 5 | 6 | int isnonblank(char * s) 7 | { 8 | int i; 9 | for(i=0; s[i] != 0; i ++) 10 | { 11 | if (!isspace(s[i])) 12 | return 1; // non-blank line 13 | } 14 | return 0; 15 | } 16 | 17 | main(int argc, char * argv[]) 18 | { 19 | char line[100], *p; 20 | FILE * sfp, *tfp; 21 | 22 | if(argc < 2) 23 | { 24 | printf("Usage : removeblanklines filename"); 25 | exit(1); 26 | } 27 | 28 | sfp = fopen(argv[1],"rt"); 29 | if (sfp == NULL) 30 | { 31 | printf("\nFile [%s] not found!\n",argv[1]); 32 | exit(2); 33 | } 34 | 35 | tfp = fopen(TEMPFILE,"wt"); 36 | if (tfp == NULL) 37 | { 38 | printf("\nSorry! Could not create temporary file!\n"); 39 | exit(2); 40 | } 41 | 42 | while(1) 43 | { 44 | p = fgets(line,100,sfp); 45 | if (p == NULL) // EOF 46 | break; 47 | if (isnonblank(line)) // Non-blank line 48 | fputs(line,tfp); 49 | } 50 | 51 | fclose(sfp); 52 | fclose(tfp); 53 | 54 | remove(argv[1]); 55 | rename(TEMPFILE,argv[1]); 56 | } 57 | -------------------------------------------------------------------------------- /files/remove_blank_lines.c: -------------------------------------------------------------------------------- 1 | // Program is to remove blank lines from a text file - names.txt 2 | #include 3 | 4 | struct node 5 | { 6 | char name[50]; 7 | struct node * next; 8 | }; 9 | 10 | main() 11 | { 12 | struct node * root = NULL, * current, * prev; 13 | char line[100], *p; 14 | FILE * fp; 15 | 16 | fp = fopen("names.txt","rt"); 17 | if ( fp == NULL) 18 | { 19 | printf("Sorry! Could not open file!"); 20 | exit(1); 21 | } 22 | 23 | while(1) 24 | { 25 | p = fgets(line,100,fp); 26 | if (p == NULL) // EOF 27 | break; 28 | 29 | // ignore if found blank 30 | if (strlen(line) == 1) // len is 1 because of new line (\n) 31 | continue; 32 | 33 | // add line to linked list 34 | current = (struct node *) malloc(sizeof(struct node)); 35 | strcpy(current->name,line); 36 | current->next = NULL; 37 | 38 | if (root == NULL) // first node 39 | root = current; // root points to first node 40 | else 41 | prev->next = current; // make prev point to current 42 | 43 | prev = current; 44 | } 45 | 46 | fclose(fp); 47 | 48 | 49 | // write lines from list back to file 50 | fp = fopen("names.txt","wt"); 51 | 52 | current = root; 53 | while(current != NULL) 54 | { 55 | printf("%s", current->name); 56 | fprintf(fp,"%s",current->name); // write line to file 57 | current = current -> next; 58 | } 59 | fclose(fp); 60 | 61 | } 62 | 63 | -------------------------------------------------------------------------------- /files/remove_blank_lines_from_file.c: -------------------------------------------------------------------------------- 1 | // Remove blank lines 2 | 3 | #include 4 | 5 | main(int argc, char * argv[]) 6 | { 7 | char line[100], *p; 8 | FILE * sfp, *tfp; 9 | 10 | // check whether required arguments are provided 11 | if (argc < 3) 12 | { 13 | printf("Usage : rbl \n"); 14 | exit(1); 15 | } 16 | 17 | sfp = fopen(argv[1],"r"); 18 | if ( sfp == NULL) 19 | { 20 | printf("Source File [%s] Could Not Be Opened!\n",argv[1]); 21 | exit(1); 22 | } 23 | 24 | tfp = fopen(argv[2],"w"); 25 | if ( tfp == NULL) 26 | { 27 | printf("Target File [%s] Could Not Be Created!\n", argv[2]); 28 | exit(1); 29 | } 30 | 31 | 32 | while(1) 33 | { 34 | p = fgets(line,100,sfp); 35 | if (p == NULL) 36 | break; 37 | // length includes newline. So ignore lines with only 1 char 38 | if (strlen(line) > 1) 39 | fputs(line,tfp); 40 | } 41 | 42 | fclose(sfp); 43 | fclose(tfp); 44 | 45 | } // main() 46 | -------------------------------------------------------------------------------- /files/removeblanklines2.c: -------------------------------------------------------------------------------- 1 | // Remove blank lines from a file using linked list 2 | #include 3 | 4 | struct node 5 | { 6 | char line[100]; 7 | struct node * next; 8 | }; 9 | 10 | int isnonblank(char * s) 11 | { 12 | int i; 13 | for(i=0; s[i] != 0; i ++) 14 | { 15 | if (!isspace(s[i])) 16 | return 1; // non-blank line 17 | } 18 | return 0; 19 | } 20 | 21 | 22 | main(int argc, char * argv[]) 23 | { 24 | struct node * root, * current, * prev; 25 | char line[100], *p; 26 | FILE * fp; 27 | 28 | if(argc < 2) 29 | { 30 | printf("Usage : removeblanklines filename"); 31 | exit(1); 32 | } 33 | 34 | fp = fopen(argv[1],"rt"); 35 | if (fp == NULL) 36 | { 37 | printf("\nFile [%s] not found!\n",argv[1]); 38 | exit(2); 39 | } 40 | 41 | root = current = prev = NULL; 42 | while(1) 43 | { 44 | p = fgets(line,100,fp); 45 | if (p == NULL) // EOF 46 | break; 47 | 48 | if (isnonblank(line)) // Non-blank line 49 | { 50 | // add a new node with this line 51 | current = (struct node *) malloc(sizeof(struct node)); 52 | if (current == NULL) { 53 | printf("Sorry! Memory allocation error. Quitting!"); 54 | exit(3); 55 | } 56 | strcpy(current->line,line); 57 | current->next = NULL; 58 | 59 | if(root == NULL) // first node? 60 | root = current; 61 | else 62 | prev->next = current; // make previous node point to current 63 | 64 | prev = current; 65 | } 66 | 67 | } // while 68 | 69 | fclose(fp); 70 | 71 | // write lines from linked list to file 72 | fp = fopen(argv[1],"wt"); 73 | if (fp == NULL) 74 | { 75 | printf("\nFile [%s] could not be created!\n",argv[1]); 76 | exit(4); 77 | } 78 | 79 | 80 | current = root; 81 | while(current != NULL) 82 | { 83 | fputs(current->line,fp); 84 | current = current -> next; 85 | } 86 | 87 | fclose(fp); 88 | 89 | } 90 | -------------------------------------------------------------------------------- /files/search_contacts.c: -------------------------------------------------------------------------------- 1 | // Search for contacts by take name on command line 2 | // Uses file contacts.dat in current directory 3 | 4 | # include 5 | 6 | struct contact 7 | { 8 | char name[30]; 9 | char email[50]; 10 | char mobile[15]; 11 | }; 12 | 13 | main(int argc, char * argv[]) 14 | { 15 | FILE * fp; 16 | struct contact c; 17 | int count; 18 | 19 | if(argc < 2) 20 | { 21 | printf("Usage: search_contacts \n"); 22 | return; 23 | } 24 | 25 | fp = fopen("contacts.dat","rb"); 26 | if (fp == NULL) 27 | { 28 | printf("Sorry! File cannot be opened!"); 29 | return; 30 | } 31 | 32 | while(1) 33 | { 34 | count = fread(&c,sizeof(c),1,fp); 35 | if (count == 0) 36 | break; 37 | 38 | if (strstr(c.name, argv[1]) != NULL) 39 | printf("%-20s %-30s %s\n",c.name,c.email,c.mobile); 40 | } 41 | 42 | fclose(fp); 43 | } 44 | -------------------------------------------------------------------------------- /files/search_file.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | main() 5 | { 6 | FILE * fp; 7 | char filename[30],search[30], line[100]; 8 | int lineno; 9 | 10 | // take input from user 11 | printf("Enter filename : "); 12 | gets(filename); 13 | 14 | printf("Enter search string : "); 15 | gets(search); 16 | 17 | fp = fopen(filename,"r"); 18 | if (fp == NULL) 19 | { 20 | printf("\nSorry! %s not found!\n",filename); 21 | exit(1); 22 | } 23 | lineno = 1; 24 | while(1) 25 | { 26 | if( fgets(line,100,fp) == NULL) 27 | break; 28 | 29 | if (strstr(line,search))// not null means found 30 | printf("%3d: %s",lineno,line); 31 | 32 | lineno++; 33 | } // while 34 | 35 | fclose(fp); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /files/search_product.c: -------------------------------------------------------------------------------- 1 | // Program to search for a product by product id 2 | struct product 3 | { 4 | int id; 5 | char name[30]; 6 | int price; 7 | }; 8 | 9 | main() 10 | { int i, prodid, found; 11 | // create an array and initialize element with data 12 | struct product prods[5] = 13 | { 14 | { 15 | 101, "iPhone7 plus",60000 16 | }, 17 | { 18 | 102, "iPhone X",80000 19 | }, 20 | { 21 | 106, "Google Pixel",50000 22 | }, 23 | { 24 | 108, "Dell Laptop",55000 25 | }, 26 | { 27 | 110, "Bose Headphones",25000 28 | } 29 | }; 30 | 31 | // Print details 32 | for(i=0; i < 5; i ++) 33 | printf("%5d %-30s %6d\n", prods[i].id, prods[i].name, prods[i].price); 34 | 35 | while(1) 36 | { 37 | printf("\nEnter product id [0 to stop] : "); 38 | scanf("%d",&prodid); 39 | if ( prodid == 0) 40 | break; 41 | 42 | found = 0; 43 | for(i=0; i < 5; i ++) 44 | { 45 | if (prods[i].id == prodid) 46 | { 47 | printf("\n%5d %-30s %6d\n", prods[i].id, prods[i].name, prods[i].price); 48 | found = 1; 49 | break; 50 | } 51 | } // for 52 | 53 | if(!found) 54 | printf("\nSorry! Product Not Found!\n"); 55 | } // while 56 | } 57 | -------------------------------------------------------------------------------- /files/search_string_using_cmd_args.c: -------------------------------------------------------------------------------- 1 | // Search for string in file using command line arguments 2 | #include 3 | 4 | main(int argc, char * argv[]) 5 | { 6 | char line[100], orgline[100]; 7 | FILE * fp; 8 | char * cp; 9 | int lineno; 10 | 11 | if (argc < 3) 12 | { 13 | printf("Usage : searchstring filename string"); 14 | return; 15 | } 16 | 17 | 18 | strupr(argv[2]); // convert search string to upper 19 | fp = fopen(argv[1],"rt"); 20 | lineno = 1; 21 | while(1) 22 | { 23 | if (fgets(line,100,fp) == NULL) 24 | break; 25 | 26 | strcpy(orgline,line); // keep copy of original line 27 | 28 | cp = strstr(strupr(line),argv[2]); 29 | if(cp != NULL) 30 | printf("%5d:%s",lineno, orgline); 31 | 32 | lineno ++; 33 | } 34 | 35 | fclose(fp); 36 | } 37 | -------------------------------------------------------------------------------- /files/search_update_mobile.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct contact 4 | { 5 | char name[30]; 6 | char email[50]; 7 | char mobile[25]; 8 | }; 9 | 10 | main() 11 | { 12 | struct contact c; 13 | FILE * fp; 14 | int pos; 15 | 16 | fp = fopen("contacts.dat","r+b"); 17 | 18 | if ( fp == NULL) 19 | { 20 | printf("Sorry! File cannot be opened!"); 21 | exit(1); 22 | } 23 | 24 | while(1) 25 | { 26 | printf("Enter contact number [1-5 , 0 to stop]:"); 27 | scanf("%d",&pos); 28 | if ( pos == 0) 29 | break; 30 | // read contact from the given position 31 | fseek(fp, (pos-1) * sizeof(struct contact),0); 32 | fread(&c,sizeof(c),1,fp); 33 | 34 | printf("\n%s %s %s\n", c.name,c.email,c.mobile); 35 | 36 | fflush(stdin); // clear keyboard buffer 37 | 38 | printf("\nEnter new mobile number :"); 39 | gets(c.mobile); 40 | // write new mobile back to file 41 | fseek(fp, (pos-1) * sizeof(struct contact),0); 42 | fwrite(&c,sizeof(c),1,fp); 43 | } 44 | 45 | fclose(fp); 46 | } 47 | -------------------------------------------------------------------------------- /files/show_source_code.c: -------------------------------------------------------------------------------- 1 | // Displays source code with line numbers for file provided on command line 2 | #include 3 | 4 | int is_blank_line(char * s) 5 | { 6 | int i; 7 | 8 | for (i=0; s[i] ; i++) 9 | { 10 | if (!isspace(s[i])) 11 | return 0; // false; 12 | } 13 | 14 | return 1; // true 15 | } 16 | 17 | main(int argc, char * argv[]) 18 | { 19 | FILE * fp; 20 | char line[200]; 21 | char * p; 22 | int lineno=1; 23 | 24 | fp = fopen(argv[1],"r"); 25 | if (fp == NULL) 26 | { 27 | printf("Sorry! [%s] could not be opened!",argv[1]); 28 | exit(1); 29 | } 30 | while(1) 31 | { 32 | p = fgets(line,200,fp); 33 | if ( p == NULL) 34 | break; 35 | 36 | if( is_blank_line(line) ) 37 | continue; 38 | 39 | printf("%03d : %s",lineno,line); 40 | lineno ++; 41 | } 42 | 43 | fclose(fp); 44 | } 45 | -------------------------------------------------------------------------------- /files/students_menu.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct student 4 | { 5 | char name[30]; 6 | int marks; 7 | }; 8 | 9 | void add_student(FILE * fp) 10 | { 11 | struct student s; 12 | int count; 13 | 14 | printf("\nAdding Student\n"); 15 | fflush(stdin); // clear keyboard buffer 16 | printf("Enter name :"); 17 | gets(s.name); 18 | printf("Enter marks:"); 19 | scanf("%d",&s.marks); 20 | // take file pointer to end of file 21 | fseek(fp,0,SEEK_END); 22 | count = fwrite(&s,sizeof(s),1,fp); 23 | if (count == 1) 24 | printf("\nAdded Student Successfully!\n"); 25 | } 26 | 27 | void update_student(FILE * fp) 28 | { 29 | struct student s; 30 | int count, rollno, marks,pos; 31 | 32 | printf("\nUpdating Marks of Student\n"); 33 | fflush(stdin); // clear keyboard buffer 34 | printf("Enter rollno :"); 35 | scanf("%d",&rollno); 36 | printf("Enter new marks:"); 37 | scanf("%d",&marks); 38 | pos = (rollno - 1) * sizeof(s); 39 | 40 | // take file pointer to starting of the record 41 | fseek(fp,pos,SEEK_SET); 42 | // read current record 43 | count = fread(&s,sizeof(s),1,fp); 44 | // modify marks 45 | s.marks = marks; 46 | // write record back to same position 47 | fseek(fp,pos,SEEK_SET); 48 | count = fwrite(&s,sizeof(s),1,fp); 49 | if (count == 1) 50 | { 51 | printf("\nUpdated Student Successfully!\n"); 52 | fflush(fp); // send changes to file 53 | } 54 | else 55 | printf("\nSorry! Could not update student!\n"); 56 | } 57 | 58 | 59 | void list_students(FILE *fp) 60 | { 61 | struct student s; 62 | int count; 63 | // move file pointer to beginning 64 | fseek(fp,0,SEEK_SET); 65 | printf("\nStudents List\n"); 66 | 67 | while(1) 68 | { 69 | count = fread(&s,sizeof(s),1,fp); 70 | if (count == 0) 71 | break; 72 | 73 | printf("%-30s %3d\n",s.name,s.marks); 74 | } 75 | } 76 | 77 | main() 78 | { 79 | int choice; 80 | FILE *fp; 81 | 82 | fp = fopen("students.dat","r+b"); 83 | if (fp == NULL) 84 | { 85 | // create file 86 | fp = fopen("students.dat","w+b"); 87 | if (fp == NULL) 88 | { 89 | printf("Sorry! File students.dat cannot be created!"); 90 | exit(1); 91 | } 92 | } 93 | 94 | while(1) 95 | { 96 | printf("\nMenu\n"); 97 | printf("====\n"); 98 | printf("1. Add\n"); 99 | printf("2. List\n"); 100 | printf("3. Update\n"); 101 | printf("4. Exit\n"); 102 | printf("Choice :"); 103 | scanf("%d",&choice); 104 | switch(choice) 105 | { 106 | case 1 : add_student(fp); 107 | break; 108 | 109 | case 2 : list_students(fp); 110 | break; 111 | 112 | case 3 : update_student(fp); 113 | break; 114 | case 4 : exit(0); 115 | 116 | default: printf("\nSorry! Invalid option. Try again!\n"); 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /files/write_contacts_to_file.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct contact 4 | { 5 | char name[30]; 6 | char email[50]; 7 | char mobile[25]; 8 | }; 9 | 10 | main() 11 | { 12 | struct contact c; 13 | FILE * fp; 14 | int i; 15 | 16 | fp = fopen("contacts.dat","w+b"); 17 | 18 | if ( fp == NULL) 19 | { 20 | printf("Sorry! File cannot be opened!"); 21 | exit(1); 22 | } 23 | 24 | for(i=1; i <=5; i ++) 25 | { 26 | printf("Enter name, email and mobile :"); 27 | gets(c.name); 28 | gets(c.email); 29 | gets(c.mobile); 30 | fwrite(&c,sizeof(c),1,fp); 31 | } 32 | 33 | fclose(fp); 34 | } 35 | -------------------------------------------------------------------------------- /files/writestudents.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct student 4 | { 5 | char name[30]; 6 | char mobile[11]; 7 | }; 8 | 9 | main() 10 | { 11 | struct student s; 12 | FILE *fp; 13 | 14 | 15 | fp = fopen("students.dat","wb"); 16 | if (fp == NULL) 17 | { 18 | printf("\nSorry! Could not create file!"); 19 | return; 20 | } 21 | 22 | while(1) 23 | { 24 | printf("Enter name[end to stop]:"); 25 | gets(s.name); 26 | if (stricmp(s.name,"end") == 0) 27 | break; 28 | 29 | printf("Enter mobile number :"); 30 | gets(s.mobile); 31 | 32 | fwrite(&s,sizeof(s),1,fp); 33 | } 34 | 35 | fclose(fp); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /linkedlist/ConvertFileToUpperWithLinkedList: -------------------------------------------------------------------------------- 1 | // Converts names in names.txt to uppercase and writes them back into same file 2 | // Using linked list 3 | #include 4 | 5 | struct node 6 | { 7 | char name[30]; 8 | struct node * next; 9 | }; 10 | 11 | 12 | main() 13 | { 14 | FILE * fp; 15 | char name[30]; 16 | struct node * root = NULL, * current, * prev; 17 | 18 | fp = fopen("f:\\c\\nov28\\names.txt","r"); 19 | if ( fp == NULL) 20 | { 21 | printf("Sorry! File cannot be opened!"); 22 | exit(1); 23 | } 24 | 25 | while(1) 26 | { 27 | if (fgets(name,30,fp) == NULL) 28 | break; 29 | 30 | if (strlen(name)==1) // only \n is present 31 | continue; 32 | 33 | 34 | // create a node and put name in that node 35 | current = (struct node *) malloc( sizeof(struct node)); 36 | if(current == NULL) 37 | { 38 | printf("Sorry! Memory allocation problem!"); 39 | exit(2); 40 | } 41 | 42 | strcpy(current->name, name); 43 | current->next = NULL; 44 | 45 | if (root == NULL) // first node 46 | root = current; 47 | else // second or after, make prev node point to current 48 | prev -> next = current; 49 | 50 | prev = current; // make current prev for next round 51 | } 52 | 53 | fclose(fp); 54 | 55 | fp = fopen("f:\\c\\nov28\\names.txt","w"); 56 | if ( fp == NULL) 57 | { 58 | printf("Sorry! File cannot be opened! for writing"); 59 | exit(3); 60 | } 61 | 62 | // print list 63 | 64 | current = root; 65 | while(current != NULL) 66 | { 67 | fprintf(fp, strupr(current->name)); 68 | current = current -> next; 69 | } 70 | 71 | fclose(fp); 72 | 73 | printf("\nConverted File to uppercase!\n"); 74 | } 75 | 76 | 77 | names.txt (input) 78 | ================== 79 | Bill 80 | Mark 81 | Larry 82 | Sergy 83 | 84 | Tim 85 | Jeff 86 | 87 | Elon 88 | 89 | names.txt (Output) 90 | ================== 91 | BILL 92 | MARK 93 | LARRY 94 | SERGY 95 | TIM 96 | JEFF 97 | ELON 98 | 99 | 100 | -------------------------------------------------------------------------------- /linkedlist/Display Names Using Linked List: -------------------------------------------------------------------------------- 1 | // display names with length more than avg. length 2 | // File is names.txt 3 | #include 4 | 5 | struct node 6 | { 7 | char name[20]; 8 | struct node * next; 9 | }; 10 | 11 | 12 | 13 | int exists(char name[],struct node * root) 14 | { 15 | struct node * cur = root; 16 | 17 | while (cur != NULL) { 18 | 19 | if (strcmp(cur->name, name) == 0) 20 | return 1; // found 21 | 22 | cur = cur->next; // move to next node 23 | } 24 | return 0; // not found 25 | } // end of exists() 26 | 27 | main() 28 | { 29 | struct node * root = NULL, * current, * previous; 30 | 31 | FILE * fp; 32 | char line[100]; 33 | int sum=0, count=0; 34 | char * cp; 35 | 36 | // open file 37 | fp = fopen("names.txt","r"); // assume file is in current directory 38 | if (fp == NULL) 39 | { 40 | printf("Sorry! File not found!\n"); 41 | exit(1); 42 | } 43 | while (1) 44 | { 45 | // read line from file 46 | cp = fgets(line,100,fp); 47 | if (cp == NULL) // reached EOF 48 | break; 49 | 50 | // check whether name is already present in the list 51 | if(exists(line,root)) // name is already present 52 | continue; 53 | 54 | sum += strlen(line); 55 | count ++; 56 | // create node 57 | current = (struct node *) malloc(sizeof(struct node)); 58 | strcpy(current->name,line); 59 | current->next = NULL; 60 | 61 | 62 | if ( root == NULL) // first node 63 | root = current; 64 | else 65 | previous ->next = current; // link it with previous node 66 | 67 | // make current previous for next round 68 | previous = current; 69 | } 70 | 71 | fclose(fp); 72 | 73 | // display names from list where length of name is 74 | // more than average length 75 | 76 | int average = sum / count; 77 | 78 | current = root; 79 | while (current != NULL) { 80 | 81 | if ( strlen( current->name) > average) 82 | puts(current->name); 83 | 84 | current = current->next; // move to next node 85 | } 86 | 87 | } 88 | 89 | 90 | names.txt 91 | ========= 92 | Dennis 93 | Bob 94 | Bill 95 | Scott 96 | Mike 97 | Tim 98 | Tommy 99 | Jason 100 | Jackson 101 | Fedrer 102 | Jack 103 | Jackson 104 | Scott 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /linkedlist/Linked List of Names from File: -------------------------------------------------------------------------------- 1 | // Places marks read from file into linked list in sorted order 2 | #include 3 | 4 | struct node 5 | { 6 | char name[20]; 7 | struct node * next; 8 | }; 9 | 10 | main() 11 | { 12 | struct node * root = NULL, * cur, * prev, *newnode; 13 | 14 | FILE * fp; 15 | char name[20]; 16 | char * p; 17 | 18 | 19 | fp = fopen("names.txt","r"); 20 | if (fp == NULL) 21 | { 22 | printf("Sorry! File not found!"); 23 | return; 24 | } 25 | 26 | while(1) 27 | { 28 | p = fgets(name,100,fp); 29 | if (p == NULL) 30 | break; 31 | // create new node 32 | newnode = (struct node *) malloc(sizeof(struct node)); 33 | strcpy(newnode->name,name); 34 | newnode->next = NULL; 35 | 36 | if(root == NULL){ 37 | root = newnode; 38 | } 39 | else 40 | { 41 | prev->next = newnode; 42 | } // else 43 | prev = newnode; 44 | } 45 | 46 | fclose(fp); 47 | 48 | // print list 49 | cur = root; 50 | while (cur!= NULL) 51 | { 52 | puts(cur->name); 53 | cur = cur->next; 54 | } 55 | } // main() 56 | 57 | -------------------------------------------------------------------------------- /linkedlist/Linked List with Large names and short names: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct node 4 | { 5 | char name[50]; 6 | struct node * next; 7 | }; 8 | 9 | int name_exists(struct node * current, char * name) 10 | { 11 | while(current != NULL) 12 | { 13 | if (strcmp(current->name, name) == 0 ) 14 | return 1; // name found 15 | 16 | current = current -> next; 17 | } 18 | 19 | return 0; // name not found 20 | } 21 | 22 | main() 23 | { 24 | struct node * root, * prev, * current; 25 | FILE * fp; 26 | char line[50]; 27 | 28 | 29 | fp = fopen("..\\names.txt","r"); 30 | if ( fp == NULL) 31 | { 32 | printf("Sorry! File not found!"); 33 | exit(1); 34 | } 35 | 36 | root = NULL; 37 | 38 | int total =0, count = 0; 39 | 40 | while (1) 41 | { 42 | if ( fgets(line,50,fp) == NULL ) 43 | break; 44 | 45 | if ( strlen(line) == 1) // empty line 46 | continue; 47 | 48 | if ( name_exists(root,line)) // ignore duplicates 49 | continue; 50 | 51 | // create a node to add name to list 52 | current = (struct node *) malloc(sizeof(struct node)); 53 | if(current == NULL) 54 | { 55 | printf("Sorry! Insufficient memory!"); 56 | exit(2); 57 | } 58 | 59 | count ++; 60 | total += strlen(line); 61 | 62 | current -> next = NULL; 63 | strcpy(current -> name,line); 64 | 65 | // if it is first node then make root point to it 66 | 67 | if(root == NULL) 68 | root = current; 69 | else 70 | prev -> next = current; // mak prev point to current node 71 | 72 | prev = current; 73 | } 74 | 75 | fclose(fp); 76 | 77 | 78 | // display list 79 | int avg = total / count; 80 | 81 | printf("\nLarge Names\n"); 82 | printf("===============\n"); 83 | current = root; 84 | while( current != NULL) 85 | { 86 | if (strlen(current->name) >= avg) 87 | printf("%s", current -> name); 88 | 89 | current = current -> next; 90 | } 91 | 92 | 93 | 94 | // display list 95 | printf("\nShort Names\n"); 96 | printf("===============\n"); 97 | current = root; 98 | while( current != NULL) 99 | { 100 | if (strlen(current->name) < avg) 101 | printf("%s", current -> name); 102 | 103 | current = current -> next; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /linkedlist/Marks List: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | get_name_marks(char * line, char * name, int * marks) 4 | { 5 | 6 | int i=0, p=0; 7 | char smarks[5]; 8 | 9 | // skip until we find first comma (,) 10 | while (line[i] != ',') 11 | i++; 12 | 13 | i++; 14 | while( line[i] != ',') // look for second comma (,) 15 | { 16 | name[p] = line[i]; 17 | p++; 18 | i++; 19 | } 20 | name[p] = 0; // null char at the end 21 | 22 | // take marks 23 | i++; 24 | p = 0; 25 | while( line[i] != '\n') 26 | { 27 | smarks[p] = line[i]; 28 | p++; 29 | i++; 30 | } 31 | 32 | smarks[p] = 0; // marks in string form 33 | *marks = atoi(smarks); 34 | 35 | } 36 | 37 | 38 | int main() { 39 | FILE *fp; 40 | char line[100], name[20], result[20]; 41 | int marks; 42 | char * cp; 43 | 44 | fp = fopen("marks.txt", "r"); 45 | 46 | printf("\n ********* Marks List *********\n"); 47 | 48 | while (1) 49 | { 50 | cp = fgets(line,100,fp); 51 | if (cp == NULL) 52 | break; 53 | 54 | if(strchr(line, ',')) // ignore line that don't have comma 55 | { 56 | get_name_marks(line,name,&marks); 57 | if (marks >= 50) 58 | strcpy(result,"Passed"); 59 | else 60 | strcpy(result,"Failed"); 61 | 62 | printf("%-20s %5d %s\n", name, marks, result); 63 | } 64 | } 65 | 66 | fclose(fp); 67 | 68 | } 69 | 70 | 71 | marks.txt 72 | ========= 73 | 1,Bill,60 74 | 2,Mike,80 75 | 3,Steve,40 76 | 4,Richards,80 77 | 5,Cathy,70 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /linkedlist/Products_Linked_List.c: -------------------------------------------------------------------------------- 1 | # include 2 | 3 | struct product 4 | { 5 | char name[20]; 6 | int price; 7 | }; 8 | 9 | struct node 10 | { 11 | struct product data; 12 | struct node * next; 13 | }; 14 | 15 | main() 16 | { 17 | struct node * current, * prev, * root = NULL; 18 | struct product prod; 19 | int totalprice = 0, prodcount = 0, avgprice; 20 | int count; 21 | FILE * fp; 22 | 23 | fp = fopen("products.dat","rb"); 24 | while(1) 25 | { 26 | count = fread(&prod,sizeof(prod),1,fp); 27 | if (count == 0) 28 | break; 29 | 30 | totalprice += prod.price; 31 | prodcount ++; 32 | // create a block of memory for node 33 | current = (struct node *) malloc(sizeof(struct node)); 34 | current->data = prod; 35 | current->next = NULL; 36 | 37 | if (root == NULL) // first node 38 | root = current; 39 | else 40 | prev->next = current; 41 | 42 | prev = current; 43 | } 44 | 45 | fclose(fp); 46 | 47 | // display list 48 | 49 | avgprice = totalprice / prodcount; 50 | 51 | current = root; 52 | while(current != NULL) 53 | { 54 | if(current->data.price > avgprice) 55 | { 56 | printf("%s - %d\n", current->data.name, current->data.price); 57 | } 58 | current = current ->next; 59 | } 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /linkedlist/Reverse print using stack: -------------------------------------------------------------------------------- 1 | // Places marks read from file into linked list in sorted order 2 | #include 3 | 4 | struct node 5 | { 6 | char name[20]; 7 | struct node * prev; 8 | }; 9 | 10 | main() 11 | { 12 | struct node * root = NULL,*cur, * prev = NULL, *newnode; 13 | 14 | FILE * fp; 15 | char name[20]; 16 | char * p; 17 | 18 | fp = fopen("names.txt","r"); 19 | if (fp == NULL) 20 | { 21 | printf("Sorry! File not found!"); 22 | return; 23 | } 24 | 25 | while(1) 26 | { 27 | p = fgets(name,100,fp); 28 | if (p == NULL) 29 | break; 30 | // create new node 31 | newnode = (struct node *) malloc(sizeof(struct node)); 32 | strcpy(newnode->name,name); 33 | newnode->prev = prev; 34 | root = newnode; 35 | prev = newnode; 36 | } 37 | 38 | fclose(fp); 39 | 40 | // print list 41 | cur = root; 42 | while (cur!= NULL) 43 | { 44 | printf("%s",cur->name); 45 | cur = cur->prev; 46 | } 47 | } // main() 48 | -------------------------------------------------------------------------------- /linkedlist/SortListByPointers.c: -------------------------------------------------------------------------------- 1 | // Sort a linked list by re-arranging links (Pointers) and not by swapping data 2 | #include 3 | #include 4 | 5 | struct node { 6 | int data; 7 | struct node * next; 8 | }*root, *current, *prev; 9 | 10 | 11 | void sort(struct node ** head); 12 | void print(struct node * start); 13 | 14 | int main() 15 | { 16 | int data,i; 17 | root = NULL; 18 | int a[] = {1,1,2,0,0}; 19 | 20 | // Build a linked list with values from Array 21 | for(i=0; i < 5 ; i ++) 22 | { 23 | // allocate a block 24 | current = (struct node *) malloc(sizeof(struct node)); 25 | current->next = NULL; 26 | current->data = a[i]; 27 | 28 | if (root == NULL) 29 | root = current; // set root to first node 30 | else 31 | prev->next = current; 32 | 33 | prev = current; 34 | } // for 35 | 36 | // print list 37 | 38 | print(root); 39 | sort(&root); 40 | printf("\nFinal List \n"); 41 | print(root); 42 | 43 | } 44 | 45 | void sort(struct node ** head) { 46 | 47 | struct node * start, * current, * prev, * nextnext; 48 | int in_order; 49 | 50 | start = * head; 51 | in_order = 0; 52 | 53 | while(!in_order) 54 | { 55 | 56 | current = prev = start; 57 | in_order = 1; // indicates list is sorted. But it will be set to 0 when we swap nodes 58 | 59 | while ( current -> next != NULL) 60 | { 61 | // current is first node and curent->next is second node 62 | if ( current -> data > current-> next -> data) 63 | { 64 | // Make first node point to next of second and second point to first node 65 | if ( current == start) 66 | { 67 | start = current ->next; // change start to point to second node 68 | } 69 | else 70 | { 71 | prev->next = current -> next; // make prev node, which pointed to first, now point to second node 72 | } 73 | 74 | nextnext = current->next -> next; // remember what second node points to 75 | current -> next -> next = current; // make second node point to first node 76 | prev = current->next; // set prev to second node 77 | current -> next = nextnext; // make first point to what second node used to point 78 | 79 | in_order = 0; // turn off to indicate list is not fully sorted 80 | } 81 | else 82 | { 83 | // when nodes are in orders then move to next node 84 | prev = current; 85 | current = current ->next; 86 | } 87 | } // inner while 88 | 89 | } //outer while 90 | 91 | *head = start; // change head so that it points to new start 92 | 93 | } 94 | 95 | // print the given list from the given starting address 96 | void print(struct node * start) 97 | { 98 | 99 | struct node * current; 100 | 101 | current = start; 102 | while(current != NULL) 103 | { 104 | printf("%d\n", current->data); 105 | current = current ->next; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /linkedlist/Sorted Linked List: -------------------------------------------------------------------------------- 1 | // Places marks read from file into linked list in sorted order 2 | #include 3 | 4 | struct node 5 | { 6 | int number; 7 | struct node * next; 8 | }; 9 | 10 | main() 11 | { 12 | struct node * root = NULL, * cur, * prev, *newnode; 13 | 14 | FILE * fp; 15 | int number,found; 16 | char line[5]; 17 | 18 | // count number of lines in marks.txt 19 | fp = fopen("f:\\c\\feb9\\marks.txt","r"); 20 | if (fp == NULL) 21 | { 22 | printf("Sorry! File not found!"); 23 | return; 24 | } 25 | 26 | do 27 | { 28 | number = read_next_number(fp); 29 | if(number == -1) // EOF then stop 30 | break; 31 | if(number == 0) // invalid value, ignore 32 | continue; 33 | 34 | // create new node 35 | newnode = (struct node *) malloc(sizeof(struct node)); 36 | newnode->number = number; 37 | newnode->next = NULL; 38 | 39 | if(root == NULL){ 40 | root = newnode; 41 | } 42 | else 43 | { 44 | // search for a number higher than the number 45 | cur = root; 46 | found=0; 47 | while(cur != NULL) 48 | { 49 | if(cur->number >= number) 50 | { 51 | if (cur == root) 52 | { 53 | newnode->next = root; 54 | root = newnode; 55 | } 56 | else 57 | { 58 | newnode->next = prev->next; 59 | prev->next = newnode; 60 | } 61 | found=1; 62 | break; 63 | } 64 | else 65 | { 66 | prev = cur; 67 | cur = cur ->next; 68 | } 69 | } // while 70 | 71 | if(!found) // no bigger value found, so add at the end 72 | { 73 | prev->next = newnode; 74 | } 75 | } // else 76 | } 77 | while(1); 78 | 79 | fclose(fp); 80 | 81 | // print list 82 | cur = root; 83 | while (cur!= NULL) 84 | { 85 | printf("%d\n", cur->number); 86 | cur = cur->next; 87 | } 88 | } // main() 89 | 90 | int read_next_number(FILE *fp) 91 | { 92 | char line[10]; 93 | 94 | if(fgets(line,5,fp) == NULL) 95 | return -1; // EOF 96 | 97 | remove_newline(line); 98 | if(strlen(line)>0) 99 | return atoi(line); // convert string to int 100 | else 101 | return 0; // invalid line 102 | } 103 | 104 | void remove_newline(char *s) 105 | { 106 | int i =0; 107 | 108 | while(isdigit(s[i])) // go until non-digit is found 109 | i++; 110 | 111 | s[i] = '\0'; 112 | 113 | } 114 | -------------------------------------------------------------------------------- /linkedlist/delete_marks_from_file_with_list.c: -------------------------------------------------------------------------------- 1 | // Program to delete marks from marks.txt that are < 60 using linked list 2 | 3 | #include 4 | #define FILENAME "marks.txt" 5 | 6 | struct node { 7 | int marks; 8 | struct node * next; 9 | }; 10 | 11 | main() 12 | { 13 | FILE * fp; 14 | char * cp; 15 | struct node * root = NULL, * current, *prev; 16 | int marks; 17 | char line[10]; 18 | 19 | fp = fopen(FILENAME,"rt"); 20 | if(fp == NULL) 21 | { 22 | printf("Sorry! File cannot be opened!"); 23 | exit(1); 24 | } 25 | 26 | // Build linked list to store all marks >= 60 27 | 28 | while(1) 29 | { 30 | cp = fgets(line,10,fp); 31 | if (cp == NULL) 32 | break; 33 | 34 | marks = atoi(line); 35 | if (marks < 60) 36 | continue; // ignore marks 37 | 38 | // add line to list 39 | current = (struct node *) malloc(sizeof(struct node)); 40 | current->marks = marks; 41 | current->next = NULL; 42 | 43 | if (root == NULL) // first node 44 | root = current; 45 | else // second or after 46 | prev->next = current; 47 | 48 | prev = current; 49 | } 50 | 51 | // Write marks from list into file 52 | 53 | fp = fopen(FILENAME,"wt"); 54 | current = root; 55 | while(current != NULL) 56 | { 57 | fprintf(fp,"%d\n", current->marks); 58 | current = current -> next; 59 | } 60 | 61 | fclose(fp); 62 | } 63 | -------------------------------------------------------------------------------- /linkedlist/manage_students.c: -------------------------------------------------------------------------------- 1 | // Manage Students in students.dat using Binary file concept and Linked List 2 | 3 | #define FILENAME "students.dat" 4 | #include 5 | 6 | struct student 7 | { 8 | char name [30]; 9 | int marks; 10 | }; 11 | 12 | struct node 13 | { 14 | struct student data; 15 | struct node * next; 16 | }; 17 | 18 | void add_student() 19 | { 20 | FILE * fp; 21 | struct student stud; 22 | 23 | fp = fopen(FILENAME, "ab"); // Append and Binary 24 | 25 | fflush(stdin); 26 | printf("Enter student name : "); 27 | gets(stud.name); 28 | 29 | printf("Enter student marks : "); 30 | scanf("%d",&stud.marks); 31 | 32 | fwrite(&stud,sizeof(struct student),1,fp); // Write to file 33 | 34 | fclose(fp); 35 | printf("\nSuccessfully added new student with %d bytes!\n", sizeof(struct student)); 36 | } 37 | 38 | // Take student number and new marks and update 39 | void update_student() 40 | { 41 | FILE * fp; 42 | struct student stud; 43 | int pos; 44 | 45 | fp = fopen(FILENAME, "r+b"); // Read-Write and Binary 46 | 47 | fflush(stdin); 48 | printf("Enter student number : "); 49 | scanf("%d",&pos); 50 | 51 | // Move to required position 52 | fseek(fp, (pos-1) * sizeof(struct student), SEEK_SET); 53 | fread(&stud,sizeof(struct student),1,fp); // read record from file 54 | 55 | 56 | printf("Enter student marks : "); 57 | scanf("%d",&stud.marks); 58 | 59 | // Write back to file after moving to same record 60 | fseek(fp, (pos-1) * sizeof(struct student), SEEK_SET); 61 | fwrite(&stud,sizeof(struct student),1,fp); // Write to file 62 | 63 | fclose(fp); 64 | printf("\nSuccessfully updated marks!\n"); 65 | } 66 | 67 | 68 | 69 | void list_students() 70 | { 71 | FILE * fp; 72 | struct student stud; 73 | int count,pos; 74 | 75 | fp = fopen(FILENAME, "rb"); // Read and Binary 76 | 77 | printf("\n*** List of Students ***\n"); 78 | pos = 1; 79 | while(1) 80 | { 81 | count = fread(&stud,sizeof(struct student),1,fp); // read from file 82 | if(count == 0) // EOF 83 | break; 84 | printf("\n%3d %-20s %3d",pos, stud.name, stud.marks); 85 | pos ++; 86 | } 87 | fclose(fp); 88 | printf("\n\n"); 89 | } 90 | 91 | void delete_student() 92 | { 93 | FILE * fp; 94 | struct student stud; 95 | struct node * root = NULL, *current, * prev; 96 | int pos, delpos,count; 97 | 98 | fp = fopen(FILENAME, "rb"); // Read and Binary 99 | printf("Enter student number :"); 100 | scanf("%d",&delpos); 101 | 102 | pos = 1; 103 | while(1) 104 | { 105 | count = fread(&stud,sizeof(struct student),1,fp); // read from file 106 | if(count == 0) // EOF 107 | break; 108 | 109 | // Ignore student to be deleted 110 | if (pos != delpos) 111 | { 112 | // Create a node 113 | current = (struct node *) malloc(sizeof(struct node)); 114 | current->next = NULL; 115 | current->data = stud; 116 | if(root == NULL) // first node 117 | root = current; 118 | else 119 | prev->next = current; 120 | 121 | prev = current; 122 | } 123 | pos++; 124 | } 125 | fclose(fp); 126 | 127 | // Write list back to file 128 | fp = fopen(FILENAME, "wb"); 129 | current = root; 130 | while(current != NULL) 131 | { 132 | fwrite(¤t->data, sizeof(current->data),1,fp); 133 | current = current -> next; 134 | } 135 | fclose(fp); 136 | printf("\nDeleted Student Successfully!\n\n"); 137 | } 138 | 139 | main() 140 | { 141 | int choice; 142 | 143 | while(1) 144 | { 145 | printf("\n*** Menu ***"); 146 | printf("\n1. Add Student"); 147 | printf("\n2. List Students"); 148 | printf("\n3. Update Student"); 149 | printf("\n4. Delete Student"); 150 | printf("\n5. Exit"); 151 | printf("\nChoice [1-5]: "); 152 | scanf("%d",&choice); 153 | 154 | switch(choice) 155 | { 156 | case 1 : add_student(); 157 | break; 158 | case 2 : list_students(); 159 | break; 160 | case 3 : update_student(); 161 | break; 162 | case 4 : delete_student(); 163 | break; 164 | case 5 : exit(0); // Stop Program 165 | } 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /linkedlist/marks_linked_list.c: -------------------------------------------------------------------------------- 1 | // linked list 2 | #include 3 | 4 | struct node { 5 | int marks; 6 | struct node * next; 7 | }; 8 | 9 | main() 10 | { 11 | struct node * root = NULL, *current, *prev; 12 | int marks, total = 0, count = 0, avg; 13 | 14 | while(1) 15 | { 16 | 17 | printf("Enter marks [-1 to stop] :"); 18 | scanf("%d", &marks); 19 | if (marks < 0) 20 | break; 21 | 22 | // allocate a block 23 | current = (struct node *) malloc(sizeof(struct node)); 24 | current->next = NULL; 25 | current->marks = marks; 26 | 27 | total += marks; 28 | count++; 29 | 30 | if (root == NULL) 31 | root = current; // set root to first node 32 | else 33 | prev->next = current; 34 | 35 | prev = current; 36 | } // while 37 | 38 | avg = total / count; 39 | 40 | // display marks that are > average 41 | 42 | current = root; 43 | while(current != NULL) 44 | { 45 | if ( current -> marks > avg) 46 | printf("\n%d", current -> marks); 47 | 48 | current = current-> next; 49 | } 50 | 51 | 52 | } 53 | -------------------------------------------------------------------------------- /linkedlist/remove_blank_lines_with_linked_list.c: -------------------------------------------------------------------------------- 1 | // Removing blank lines from names.txt using linked list 2 | #include 3 | 4 | struct node { 5 | char name[30]; 6 | struct node * next; 7 | }; 8 | 9 | main() 10 | { 11 | FILE * fp; 12 | char * p, name[30]; 13 | struct node * current, *prev, * root = NULL; 14 | 15 | fp = fopen("names.txt","rt"); 16 | while(1) 17 | { 18 | p = fgets(name, 30, fp); 19 | if(p == NULL) 20 | break; 21 | // Ignore blank line 22 | if( strlen(name) <= 1) 23 | continue; 24 | 25 | // Allocate memory for a node 26 | current = (struct node *) malloc(sizeof(struct node)); 27 | current->next = NULL; 28 | strcpy(current->name, name); 29 | 30 | if (root == NULL) 31 | root = current; 32 | else 33 | prev -> next = current; 34 | 35 | prev = current; 36 | } 37 | 38 | fclose(fp); 39 | 40 | fp = fopen("names.txt","wt"); 41 | current = root; 42 | while (current != NULL) 43 | { 44 | fputs(current->name,fp); 45 | current = current -> next; 46 | } 47 | 48 | fclose(fp); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /linkedlist/sort names using linked list: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | struct node 5 | { 6 | char name[20]; 7 | struct node * next; 8 | }; 9 | 10 | 11 | struct node *root,*current; 12 | 13 | main() 14 | { 15 | FILE * fp; 16 | char line[30], *p; 17 | 18 | fp = fopen("names.txt","rt"); 19 | if ( fp == NULL) 20 | { 21 | printf("Sorry! File not opened!"); 22 | exit(1); 23 | } 24 | 25 | root = NULL; 26 | 27 | while(1) 28 | { 29 | p = fgets(line,30,fp); 30 | if (p == NULL) 31 | break; 32 | 33 | if (strlen(line) < 2) 34 | continue; 35 | 36 | printf("\nRead : %s", line); 37 | // create a node to store name 38 | current = (struct node *) malloc(sizeof(struct node)); 39 | strcpy(current->name,line); 40 | 41 | if (current == NULL) 42 | { 43 | printf("Sorry! Insufficient memory!"); 44 | exit(2); 45 | } 46 | 47 | if (root == NULL) { 48 | current ->next = NULL; 49 | root = current; 50 | printf("Setting root\n"); 51 | } 52 | else 53 | { 54 | // find out position of the new node 55 | struct node *curnode ,*prenode; 56 | 57 | curnode = prenode = root; 58 | while(curnode != NULL) 59 | { 60 | puts(curnode ->name); 61 | if (strcmp(curnode->name,line) > 0) 62 | break; 63 | prenode = curnode; 64 | curnode = curnode ->next; 65 | } 66 | // now insert node 67 | if (root == curnode) // insert at the beginning 68 | { 69 | current->next = root; 70 | root = current; 71 | } 72 | else // insert in the middle or at end 73 | { 74 | current -> next = prenode->next; 75 | prenode->next = current; 76 | } 77 | } // else 78 | } // end of while() 79 | 80 | fclose(fp); 81 | 82 | current = root; 83 | while(current != NULL) { 84 | printf("%s",current->name); 85 | current = current -> next; 86 | } 87 | 88 | } 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /linkedlist/sort_numbers_from_file.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | FILE * fp; 6 | int ch,lines,i; 7 | char line[5]; 8 | int * p; 9 | 10 | // count number of lines in marks.txt 11 | fp = fopen("f:\\c\\feb9\\marks.txt","r"); 12 | 13 | if ( fp == NULL) 14 | { 15 | printf("Sorry! File not found!"); 16 | return; 17 | } 18 | 19 | lines = number_of_lines(fp); 20 | // allocate memory 21 | p = (int*) malloc(sizeof(int)*lines); 22 | 23 | load_file(fp,p); 24 | 25 | sort_numbers(p,lines); 26 | 27 | // print numbers after sorting 28 | for(i=0; i < lines; i++) 29 | { 30 | printf("\n%d", p[i]); 31 | } 32 | 33 | fclose(fp); 34 | } 35 | 36 | void sort_numbers(int * a, int size) 37 | { 38 | int i,j,temp; 39 | 40 | for(i=0; i < size-1; i++) 41 | { 42 | for (j=i+1; j < size; j++) 43 | { 44 | if ( a[i] > a[j]) 45 | { 46 | temp = a[i]; 47 | a[i] = a[j]; 48 | a[j] = temp; 49 | } 50 | } 51 | } 52 | } 53 | 54 | int number_of_lines(FILE *fp) 55 | { 56 | int lines = 0; 57 | char line[5]; 58 | do 59 | { 60 | if(fgets(line,5,fp) == NULL) 61 | break; 62 | remove_newline(line); 63 | if(strlen(line)>0) 64 | lines ++; 65 | } 66 | while(1); 67 | 68 | return lines; 69 | } 70 | 71 | int load_file(FILE *fp, int * p) 72 | { 73 | int pos = 0; 74 | char line[5]; 75 | fseek(fp,0,0); 76 | do 77 | { 78 | if(fgets(line,5,fp) == NULL) 79 | break; 80 | remove_newline(line); 81 | 82 | if(strlen(line)>0) 83 | { 84 | p[pos] = atoi(line); // convert string to int 85 | pos++; 86 | } 87 | } 88 | while(1); 89 | } 90 | 91 | void remove_newline(char *s) 92 | { 93 | int i =0; 94 | 95 | while(s[i]) 96 | { 97 | if(!isdigit(s[i])) 98 | { 99 | s[i] = '\0'; 100 | break; 101 | } 102 | i++; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /linkedlist/stack.c: -------------------------------------------------------------------------------- 1 | // Purpose: Print names in reverse order - STACK 2 | // Date : 20-Aug-2017 3 | // Author : Srikanth Pragada 4 | // Place : Vizag 5 | 6 | #include 7 | 8 | struct node 9 | { 10 | char name[20]; 11 | struct node * next; 12 | }; 13 | 14 | 15 | main() 16 | { 17 | struct node * root, * current, * prev; 18 | char name[20]; 19 | 20 | prev = root = NULL; 21 | 22 | while(1) 23 | { 24 | printf("Enter a name :"); 25 | gets(name); 26 | if ( strcmp(name,"end") == 0) 27 | break; 28 | 29 | // create a node 30 | current = (struct node *) malloc( sizeof(struct node)); 31 | strcpy(current->name,name); // copy name into node 32 | 33 | root = current; 34 | current -> next = prev; 35 | 36 | prev = current; // make current prev for next round 37 | } 38 | 39 | 40 | // print list 41 | 42 | current = root; 43 | 44 | while(current != NULL) 45 | { 46 | puts( current->name); 47 | current = current -> next; 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /linkedlist/stack_data_structure.c: -------------------------------------------------------------------------------- 1 | // STACK 2 | #include 3 | 4 | 5 | struct node 6 | { 7 | char name[30]; 8 | struct node * prev; 9 | } *root,*current, *prev = NULL; 10 | 11 | 12 | main() 13 | { 14 | FILE * fp; 15 | char line[50]; 16 | char * p; 17 | 18 | fp = fopen("names.txt","r"); 19 | 20 | while(1) 21 | { 22 | p = fgets(line,50,fp); 23 | if (p == NULL) // EOF 24 | break; 25 | 26 | // allocate memory for node 27 | current = (struct node *) malloc( sizeof(struct node)); 28 | strcpy(current->name, line); 29 | 30 | current->prev = prev; 31 | prev = current; 32 | root = current; 33 | } 34 | 35 | 36 | current = root; 37 | while(current != NULL) 38 | { 39 | printf(current->name); 40 | current = current -> prev; 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /linkedlist/std_of_marks.c: -------------------------------------------------------------------------------- 1 | // Build linked list to show STD of marks taken from user 2 | #include 3 | 4 | struct node 5 | { 6 | int marks; 7 | struct node * next; 8 | }; 9 | 10 | main() 11 | { 12 | struct node * root = NULL, *current, *previous; 13 | int marks, total = 0, count = 0, avg; 14 | 15 | while(1) 16 | { 17 | printf("Enter marks :"); 18 | scanf("%d",&marks); 19 | if (marks < 0) 20 | break; 21 | 22 | // allocate memory for a node 23 | current = (struct node *) malloc(sizeof(struct node)); 24 | current->next = NULL; 25 | current->marks = marks; 26 | total += marks; 27 | count ++; 28 | 29 | if(root == NULL) // first node of the list 30 | root = current; 31 | else 32 | previous -> next = current; 33 | 34 | previous = current; 35 | } 36 | 37 | avg = total / count; 38 | // print marks 39 | current = root; 40 | while(current != NULL) 41 | { 42 | printf("%3d - %3d\n", current->marks, current->marks - avg); 43 | current = current -> next; 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /linkedlist/std_of_marks_from_file.c: -------------------------------------------------------------------------------- 1 | // Build linked list to show STD of marks taken from marks.txt 2 | #include 3 | 4 | struct node 5 | { 6 | int marks; 7 | struct node * next; 8 | }; 9 | 10 | int isnumber(char st[]) 11 | { 12 | int i; 13 | 14 | for(i=0; st[i]; i ++) 15 | { 16 | if (! (isdigit(st[i]) || st[i] == '\n')) 17 | return 0; // false 18 | } 19 | return 1; // true 20 | } 21 | 22 | main() 23 | { 24 | struct node * root = NULL, *current, *previous; 25 | int marks, total = 0, count = 0, avg; 26 | FILE * fp; 27 | char line[10]; 28 | 29 | // open file 30 | fp = fopen("marks.txt","rt"); 31 | if ( fp == NULL) 32 | { 33 | printf("\nSorry! File cannot be opened!\n"); 34 | exit(1); 35 | } 36 | 37 | 38 | while(1) 39 | { 40 | if (fgets(line,10,fp) == NULL) // stop if EOF reached 41 | break; 42 | 43 | if (strlen(line) == 1) // blank line, so ignore 44 | continue; 45 | 46 | if (!isnumber(line)) // non-number, so ignore 47 | continue; 48 | 49 | marks = atoi(line); 50 | 51 | // allocate memory for a node 52 | current = (struct node *) malloc(sizeof(struct node)); 53 | current->next = NULL; 54 | current->marks = marks; 55 | total += marks; 56 | count ++; 57 | 58 | if(root == NULL) // first node of the list 59 | root = current; 60 | else 61 | previous -> next = current; 62 | 63 | previous = current; 64 | } 65 | 66 | fclose(fp); 67 | avg = total / count; 68 | 69 | // print marks 70 | current = root; 71 | while(current != NULL) 72 | { 73 | printf("%3d - %3d\n", current->marks, current->marks - avg); 74 | current = current -> next; 75 | } 76 | } 77 | 78 | -------------------------------------------------------------------------------- /loops/ArmStrongNumber.c: -------------------------------------------------------------------------------- 1 | /* Armstrong Number */ 2 | #include 3 | 4 | int main() 5 | { 6 | int i, temp, n, digit, sum=0; 7 | 8 | printf("Enter a number :"); 9 | scanf("%d", &n); 10 | 11 | temp=n; 12 | 13 | while(temp!=0) 14 | { 15 | digit=temp%10; 16 | sum+=digit*digit*digit; 17 | temp=temp/10; 18 | } 19 | 20 | if(sum==n) 21 | printf("%d is an Armstrong Number", n); 22 | else 23 | printf("%d is not an Armstrong Number", n); 24 | } 25 | -------------------------------------------------------------------------------- /loops/FlyodTriangle.c: -------------------------------------------------------------------------------- 1 | /* Floyd Triangle - Praneeth Pragada */ 2 | #include 3 | main() 4 | { 5 | int i=1, a, b=1; 6 | 7 | printf("Enter number of rows of Floyd triangle to be displayed :"); 8 | scanf("%d", &a); 9 | 10 | while(i<=a) 11 | { 12 | int j=i; 13 | 14 | while(j>0) 15 | { 16 | printf("%2d ", b); 17 | j--; 18 | b++; 19 | } 20 | 21 | printf("\n"); 22 | i++; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /loops/GCD.c: -------------------------------------------------------------------------------- 1 | // GCD Version 1 2 | 3 | main() 4 | { 5 | int n1,n2,i, small; 6 | 7 | printf("Enter two numbers :"); 8 | scanf("%d%d",&n1,&n2); 9 | 10 | if (n1 % n2 == 0) 11 | printf("%d", n2); 12 | else 13 | if(n2 % n1 == 0) 14 | printf("%d",n1); 15 | else 16 | { 17 | small = n1 < n2 ? n1 : n2; 18 | for (i = small/2; i >=1 ; i --) 19 | { 20 | if ( n1 % i == 0 && n2 % i == 0) 21 | { 22 | printf("%d",i); 23 | break; 24 | } 25 | } 26 | } 27 | } 28 | 29 | GCD Version 2 30 | ============= 31 | main() 32 | { 33 | int n1,n2,i, small; 34 | 35 | printf("Enter two numbers :"); 36 | scanf("%d%d",&n1,&n2); 37 | 38 | small = n1 < n2 ? n1 : n2; 39 | for (i = small; i >=1 ; i --) 40 | { 41 | if ( n1 % i == 0 && n2 % i == 0) 42 | { 43 | printf("%d",i); 44 | break; 45 | } // if 46 | } // for 47 | 48 | } // main 49 | 50 | -------------------------------------------------------------------------------- /loops/Perfect.c: -------------------------------------------------------------------------------- 1 | // Perfect Number 2 | 3 | #include 4 | 5 | main() 6 | { 7 | int num,sum=0,i; 8 | 9 | printf("Enter a number :"); 10 | scanf("%d",&num); 11 | 12 | for(i=1; i <= num/2; i++) 13 | { 14 | if( num % i == 0) 15 | sum += i; 16 | } 17 | 18 | if (num == sum) 19 | printf("Perfect Number!"); 20 | else 21 | printf("Not a perfect number!"); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /loops/Prime.c: -------------------------------------------------------------------------------- 1 | // Checks whether the given numbers on command line are prime numbers 2 | 3 | // Prime number program 4 | #include 5 | 6 | 7 | int valid_number(char * s) 8 | { 9 | int i; 10 | 11 | for(i=0; s[i] ; i ++) 12 | { 13 | if (! (isdigit(s[i]) || s[i] == '-' || s[i] == '+') ) 14 | return 0; // false; 15 | } 16 | return 1; 17 | } 18 | 19 | main(int argc, char * argv[]) 20 | { 21 | int num,n, i, prime; 22 | 23 | if (argc < 2) 24 | { 25 | printf("Usage: prime number ... \n"); 26 | exit(1); 27 | } 28 | 29 | for(i=1; i < argc ; i ++) 30 | { 31 | if(!valid_number(argv[i])) 32 | { 33 | printf("[%s] is not a valid number!\n", argv[i]); 34 | continue; 35 | } 36 | num = atoi(argv[i]); 37 | prime = 1; 38 | for(n=2; n <= num/2; n ++) 39 | { 40 | if (num % n == 0 ) 41 | { 42 | printf("[%d ] Not a prime number!\n", num); 43 | prime = 0; 44 | break; 45 | } 46 | } // inner for 47 | 48 | if (prime) 49 | printf("[%d] Prime number!\n", num); 50 | } // outer for 51 | } // main 52 | 53 | 54 | Sample output 1: 55 | ================ 56 | 57 | F:\c\nov28>prime 98 17 58 | [98 ] Not a prime number! 59 | [17] Prime number! 60 | 61 | Sample output 2: 62 | ================ 63 | 64 | F:\c\nov28>prime 98 abc 87 bbc 93 65 | [98 ] Not a prime number! 66 | [abc] is not a valid number! 67 | [87 ] Not a prime number! 68 | [bbc] is not a valid number! 69 | [93 ] Not a prime number! 70 | 71 | -------------------------------------------------------------------------------- /loops/Strong Number: -------------------------------------------------------------------------------- 1 | // Strong number 2 | 3 | #include 4 | 5 | main() 6 | { 7 | int num, org_num, digit,fact,i, total = 0; 8 | 9 | printf("Enter a number :"); 10 | scanf("%d",&num); 11 | 12 | org_num = num; 13 | while(num) 14 | { 15 | digit = num % 10; // take rightmost digit 16 | 17 | // calculate factorial of digit 18 | fact = 1; 19 | for(i=1; i <= digit; i ++) 20 | fact *= i; 21 | 22 | total += fact; 23 | 24 | num /= 10; // remove rightmost digit 25 | } 26 | 27 | if (org_num == total) 28 | printf("Strong number"); 29 | else 30 | printf("Not a strong number"); 31 | } 32 | -------------------------------------------------------------------------------- /loops/get_single_digit_sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int n,sum; 6 | 7 | printf("Enter a number :"); 8 | scanf("%d",&n); 9 | while(1) 10 | { 11 | sum=0; 12 | while(n) 13 | { 14 | sum += n % 10; 15 | n/=10; 16 | } 17 | if (sum < 10) 18 | break; 19 | n = sum; 20 | } 21 | 22 | printf("Single digit sum = %d",sum); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /loops/guess_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | int number, input, attempts=1; 6 | 7 | srand(time(0)); 8 | number = rand() % 10; 9 | // printf("%d\n",number); 10 | while(attempts <= 3) 11 | { 12 | printf("\nEnter number :"); 13 | scanf("%d",&input); 14 | if (input == number) 15 | { 16 | printf("Congrats!"); 17 | break; 18 | } 19 | 20 | if (number < input) 21 | printf("\nNumber is smaller!"); 22 | else 23 | printf("\nNumber is bigger!"); 24 | 25 | attempts ++; 26 | } 27 | 28 | if(attempts > 3) 29 | printf("\nSorry! Better luck next time!"); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /loops/palindrome.c: -------------------------------------------------------------------------------- 1 | // Palindrome program 2 | #include 3 | 4 | void main() 5 | { 6 | int num,pnum, digit, rnum = 0; 7 | 8 | printf("Enter a number :"); 9 | scanf("%d",&num); 10 | pnum = num; 11 | 12 | while(num > 0) 13 | { 14 | digit = num % 10; 15 | rnum = digit + rnum * 10; 16 | num /= 10; 17 | } 18 | 19 | if(rnum == pnum) 20 | printf("Palindrome"); 21 | else 22 | printf("Not a Palindrome"); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /loops/print_digits_in_reverse.c: -------------------------------------------------------------------------------- 1 | // Print digits in reverse order 2 | #include 3 | 4 | main() 5 | { 6 | int num,digit; 7 | 8 | printf("Enter a number :"); 9 | scanf("%d",&num); 10 | 11 | while(num > 0) 12 | { 13 | digit = num % 10; // take rightmost digit 14 | printf("%d", digit); 15 | num /= 10; // remove rigthmost digit 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /loops/single_digit_sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int n,sum; 6 | 7 | printf("Enter a number :"); 8 | scanf("%d",&n); 9 | while(1) 10 | { 11 | sum=0; 12 | while(n) 13 | { 14 | sum += n % 10; 15 | n/=10; 16 | } 17 | if (sum < 10) 18 | break; 19 | n = sum; 20 | } 21 | 22 | printf("Single digit sum = %d",sum); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /loops/table.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | main(int argc, char * argv[]) 5 | { 6 | int i; 7 | int end=20; 8 | int num; 9 | 10 | if (argc == 1) // check whether no command line arguments are given 11 | { 12 | printf("Enter a number :"); 13 | scanf("%d",&num); 14 | } 15 | else 16 | num = atoi(argv[1]); // string to int 17 | 18 | 19 | if (argc > 2) 20 | end = atoi(argv[2]); // string to int 21 | 22 | 23 | for(i=1; i <= end ; i ++) 24 | { 25 | printf("%5d * %5d = %5d\n", i, num, i * num); 26 | } 27 | 28 | printf("\nPress a key to continue..."); 29 | getch(); 30 | 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /strings/Count common chars in two string: -------------------------------------------------------------------------------- 1 | // Common chars count 2 | 3 | int is_present(char s[20], char ch) 4 | { 5 | int i; 6 | 7 | for(i=0; s[i] ; i ++) 8 | { 9 | if(s[i] == ch) 10 | return 1; 11 | } 12 | return 0; 13 | } 14 | 15 | 16 | main() 17 | { 18 | char st1[20] = "Programming"; 19 | char st2[20] = "Java Program"; 20 | int count =0,i,j, found; 21 | 22 | for(i=0; st2[i]; i++) 23 | { 24 | found = 0; 25 | for(j=0; j < i; j++) 26 | { 27 | if(st2[j] == st2[i]) 28 | { 29 | found = 1; 30 | break; 31 | } 32 | } 33 | 34 | if (found) 35 | continue; 36 | 37 | 38 | if(is_present(st1, st2[i])) 39 | count ++; 40 | } 41 | 42 | printf("%d",count); 43 | 44 | } 45 | 46 | 47 | -------------------------------------------------------------------------------- /strings/Largest_word_in_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | char s[100], w[20], lw[20]; 6 | int i, j=0; 7 | 8 | 9 | printf("Enter a string :"); 10 | gets(s); 11 | 12 | strcat(s," "); // add a space to process last word at the end 13 | strcpy(lw,""); // put empty string 14 | 15 | for(i=0; s[i] ; i++) 16 | { 17 | if (s[i] == 32) 18 | { 19 | w[j] = '\0'; // put null char at the end 20 | j = 0; 21 | /* 22 | // for largest string by length 23 | if (strlen(w) > strlen(lw)) 24 | strcpy(lw,w); 25 | */ 26 | // compare strings by characters 27 | if ( strcmp(w,lw) > 0) 28 | strcpy(lw,w); 29 | } 30 | else 31 | { 32 | 33 | w[j] = s[i]; 34 | j++; 35 | } 36 | } 37 | puts(lw); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /strings/Print Digits in Words: -------------------------------------------------------------------------------- 1 | char words[10][10] = 2 | {"Zero","One","Two","Three","Four","Five", 3 | "Six","Seven","Eight","Nine"}; 4 | 5 | 6 | main() 7 | { 8 | print_digit(345); 9 | printf("\n"); 10 | print_digit(34080); 11 | } 12 | 13 | 14 | void print_digit(int n) 15 | { 16 | if ( n > 0) { 17 | print_digit(n / 10); 18 | printf("%s ", words[n % 10]); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /strings/concate_names.c: -------------------------------------------------------------------------------- 1 | // Program to concatenate names by separating them with , (comma) 2 | #include 3 | 4 | main() 5 | { 6 | char names[1000]; 7 | char name[50]; 8 | 9 | names[0] = '\0'; // start with empty string 10 | while(1) 11 | { 12 | printf("Enter a name [end to stop] : "); 13 | gets(name); 14 | 15 | if (strcmp(name,"end") == 0) // stop if name is "end" 16 | break; 17 | 18 | if (strlen(names) > 0) 19 | strcat(names,","); // add , only for second string onwards 20 | 21 | strcat(names,name); // add name at the end of names 22 | } 23 | 24 | puts(names); 25 | } 26 | -------------------------------------------------------------------------------- /strings/largest_word.c: -------------------------------------------------------------------------------- 1 | // Program to print largest word in the given string 2 | 3 | #include 4 | 5 | main() 6 | { 7 | char st[100],w[20],lw[20]; 8 | int i,j; 9 | 10 | printf("Enter a string : "); 11 | gets(st); 12 | j=0; 13 | strcpy(lw,""); 14 | for(i=0; ;i ++) 15 | { 16 | if (isspace(st[i]) || st[i] == 0) // end of word 17 | { 18 | w[j]='\0'; 19 | if ( strcmp(w,lw) > 0) 20 | strcpy(lw,w); 21 | j = 0; 22 | if(st[i] == 0) 23 | break; 24 | } 25 | else 26 | { 27 | w[j] = st[i]; 28 | j++; 29 | } 30 | } 31 | 32 | printf("Largest Word : %s",lw); 33 | } 34 | -------------------------------------------------------------------------------- /strings/number_to_words.c: -------------------------------------------------------------------------------- 1 | /* print words of digits in a number using recursion */ 2 | 3 | char words[10][10]= 4 | { "Zero","One","Two","Three","Four", 5 | "Five","Six","Seven","Eight","Nine"}; 6 | 7 | main() 8 | { 9 | print(6717695); 10 | } 11 | 12 | void print(int n) 13 | { 14 | if ( n > 0) 15 | { 16 | print( n / 10); 17 | printf("%s ", words[n % 10]); 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /strings/print_number_in_words.c: -------------------------------------------------------------------------------- 1 | 2 | char * words[] = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}; 3 | 4 | main() 5 | { 6 | int num; 7 | 8 | printf("Enter a number :"); 9 | scanf("%d",&num); 10 | 11 | print_in_words(num); 12 | } 13 | 14 | void print_in_words(int num) 15 | { 16 | char digits[10]; 17 | int i; 18 | 19 | itoa(num,digits, 10); // convert number to string 20 | 21 | for(i=0; digits[i] ; i ++) 22 | { 23 | // get numeric value for each digit as it stored in ascii code 24 | printf("%s ", words[digits[i] - 48]); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /strings/print_words_reverse.c: -------------------------------------------------------------------------------- 1 | /* print each word in reverse */ 2 | 3 | main() 4 | { 5 | char line[100], word[20]; 6 | int i,p; 7 | 8 | printf("Enter a line :"); 9 | gets(line); 10 | 11 | for(i=0,p=0; ; i++) 12 | { 13 | if(line[i] == 32 || line[i] == '\0') 14 | { 15 | 16 | word[p] = '\0'; 17 | puts(strrev(word)); 18 | if ( line[i] == '\0') 19 | break; 20 | p = 0; 21 | } 22 | else 23 | { 24 | word[p] = line[i]; 25 | p++; 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /strings/string_count.c: -------------------------------------------------------------------------------- 1 | // count no. of occurrences of a string 2 | #include 3 | #include 4 | main() 5 | { 6 | char line[100], st[20]; 7 | char * p; 8 | int count = 0; 9 | 10 | printf("Enter a line :"); 11 | gets(line); 12 | 13 | printf("Enter a string :"); 14 | gets(st); 15 | 16 | p = line; // start at the beginning of string 17 | while(1) 18 | { 19 | p = strstr(p,st); 20 | if (p == NULL) // not found 21 | break; 22 | 23 | count++; 24 | p++; 25 | } 26 | 27 | printf("Count = %d",count); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /structs/Contacts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define FILENAME "f:\\c\\dec15\\contacts.txt" 5 | 6 | struct contact 7 | { 8 | char name[30]; 9 | char mobile[30]; 10 | struct contact * next; 11 | }; 12 | 13 | void split_line(char *line, char * name, char * mobile) 14 | { 15 | char *p; 16 | int pos,len; 17 | 18 | p = strchr(line,','); 19 | pos = p - line; 20 | // printf("%d",pos); 21 | strncpy(name,line,pos); 22 | name[pos] = '\0'; 23 | strcpy(mobile, p+1); 24 | 25 | // remove newline if it is present in mobile 26 | len = strlen(mobile); 27 | if (isspace(mobile[len-1])) // if last char is newline 28 | mobile[len-1]= '\0'; 29 | } 30 | 31 | // return pointer to the first node 32 | struct contact * load() 33 | { 34 | struct contact * start = NULL, * current, *prev; 35 | FILE * fp; 36 | char line[100], *p; 37 | 38 | fp = fopen(FILENAME,"r"); 39 | if ( fp == NULL) 40 | { 41 | printf("Sorry! File could not be opened!"); 42 | exit(1); 43 | } 44 | 45 | p = fgets(line,100,fp); 46 | while(p != NULL) 47 | { 48 | if ( strlen(line) > 0) 49 | { 50 | current = (struct contact *) malloc(sizeof(struct contact)); 51 | split_line(line, current->name,current->mobile); 52 | current ->next = NULL; 53 | 54 | if (start == NULL) // first node 55 | start = current; 56 | else 57 | prev ->next = current; 58 | 59 | prev = current; 60 | } 61 | p = fgets(line,100,fp); 62 | } // while 63 | fclose(fp); 64 | printf("\nLoaded successfully!\n"); 65 | return start; 66 | 67 | } 68 | 69 | struct contact * add_contact(struct contact * start) 70 | { 71 | struct contact *current, *prev, *newnode; 72 | char name[30],mobile[30]; 73 | 74 | 75 | newnode = (struct contact *) malloc(sizeof(struct contact)); 76 | printf("Enter name : "); 77 | gets(newnode->name); 78 | printf("Enter mobile : "); 79 | gets(newnode->mobile); 80 | 81 | newnode ->next = NULL; 82 | // list is empty 83 | if (start == NULL) 84 | return newnode; // this becomes start 85 | else 86 | { // add node at the end of the list 87 | current = start; 88 | while(current != NULL) 89 | { 90 | prev = current; 91 | current = current->next; 92 | } 93 | prev ->next = newnode; 94 | return start; 95 | } 96 | 97 | } 98 | 99 | struct contact * search_name(struct contact * start, char * name) 100 | { 101 | while(start != NULL ) { 102 | if ( strcmpi(start->name,name) == 0) 103 | return start; // found and return address of the node 104 | start = start->next; 105 | } 106 | 107 | return NULL; // not found 108 | }; 109 | 110 | // inserts a new node after the given name 111 | void insert_contact(struct contact * start) 112 | { 113 | struct contact *current, *prev, *newnode; 114 | char name[30],mobile[30]; 115 | 116 | printf("Enter name : "); 117 | gets(name); 118 | 119 | current = search_name(start,name); 120 | if (current == NULL) 121 | { 122 | printf("\nSorry! Name not found!"); 123 | return; 124 | } 125 | 126 | newnode = (struct contact *) malloc(sizeof(struct contact)); 127 | printf("Enter Name : "); 128 | gets(newnode->name); 129 | printf("Enter mobile : "); 130 | gets(newnode->mobile); 131 | 132 | newnode ->next = current->next; 133 | current->next = newnode; 134 | } 135 | 136 | 137 | struct contact * delete_contact(struct contact * start) 138 | { 139 | struct contact *current, *prev, *newnode; 140 | char name[30]; 141 | 142 | printf("Enter name : "); 143 | gets(name); 144 | 145 | // if first node 146 | if (strcmpi(start->name, name) == 0) 147 | { 148 | current = start; 149 | start = start->next; 150 | free(current); // delete node from memory 151 | return start; 152 | } 153 | 154 | // delete non-start node 155 | prev = start; 156 | current = start->next; 157 | while(current != NULL) 158 | { 159 | if (strcmpi(current->name, name) == 0) 160 | { 161 | prev->next = current->next; 162 | free(current); 163 | break; 164 | } 165 | prev = current; 166 | current = current->next; 167 | } 168 | return start; 169 | } 170 | 171 | 172 | void list(struct contact * start) 173 | { 174 | printf("\nList of Contacts"); 175 | printf("\n==================\n"); 176 | 177 | while (start != NULL) 178 | { 179 | printf("\n%-30s %-30s", start->name, start->mobile); 180 | start = start->next; 181 | } 182 | } 183 | 184 | void search_contacts(struct contact * start) 185 | { 186 | char name[30]; 187 | 188 | printf("Enter search name :"); 189 | gets(name); 190 | 191 | printf("\nList of Contacts"); 192 | printf("\n==================\n"); 193 | 194 | while (start != NULL) 195 | { 196 | if (strstr(start->name,name)!= NULL) 197 | { 198 | printf("\n%-30s %-30s", start->name, start->mobile); 199 | } 200 | start = start->next; 201 | } 202 | 203 | } 204 | 205 | void save_contacts(struct contact * start) 206 | { 207 | FILE * fp; 208 | 209 | fp = fopen(FILENAME,"w"); 210 | if ( fp == NULL) 211 | { 212 | printf("\nSorry! File cannot be opened.\n"); 213 | return; 214 | } 215 | 216 | while (start != NULL) 217 | { 218 | fprintf(fp,"%s,%s\n",start->name, start->mobile); 219 | start = start->next; 220 | } 221 | fclose(fp); 222 | printf("\nSaved contacts successfully!\n"); 223 | } 224 | 225 | main() 226 | { 227 | struct contact * start = NULL; 228 | int ch; 229 | 230 | while(1) 231 | { 232 | 233 | printf("\nMenu"); 234 | printf("\n======"); 235 | printf("\n1.Load from file"); 236 | printf("\n2.Display Contacts"); 237 | printf("\n3.Add Contact"); 238 | printf("\n4.Delete Contact"); 239 | printf("\n5.Insert Contact"); 240 | printf("\n6.Search Contact"); 241 | printf("\n7.Save Contacts"); 242 | printf("\n8.Exit"); 243 | printf("\nEnter choice [1-8] :"); 244 | ch = getchar(); 245 | fflush(stdin); 246 | switch(ch) 247 | { 248 | case '1' : start = load(); break; 249 | case '2' : list(start); break; 250 | case '3' : start = add_contact(start); break; 251 | case '4' : start = delete_contact(start); break; 252 | case '5' : insert_contact(start); break; 253 | case '6' : search_contacts(start); break; 254 | case '7' : save_contacts(start); break; 255 | case '8' : exit(0); 256 | } 257 | } 258 | } 259 | 260 | 261 | -------------------------------------------------------------------------------- /structs/DeleteBook.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct book 4 | { 5 | char title[50], author[30]; 6 | }; 7 | 8 | struct node 9 | { 10 | struct book book; 11 | struct node * next; 12 | }; 13 | 14 | 15 | main() 16 | { 17 | struct node * root = NULL, * current, * prev; 18 | struct book book; 19 | FILE * fp; 20 | int bookno,pos, count; 21 | 22 | 23 | printf("\nEnter book number to delete :"); 24 | scanf("%d",&bookno); 25 | 26 | fp = fopen("books.dat", "rb"); 27 | pos= 0; 28 | while(1) 29 | { 30 | count = fread(&book,sizeof(book),1,fp); // read a book 31 | pos++; 32 | if(count == 0) 33 | break; 34 | 35 | if(bookno == pos) 36 | continue; // ignore book to be deleted 37 | 38 | current = (struct node *) malloc( sizeof(struct node)); 39 | current->next = NULL; 40 | current->book = book; 41 | 42 | if (root == NULL) 43 | root = current; 44 | else 45 | prev->next = current; 46 | 47 | prev = current; 48 | } 49 | fclose(fp); 50 | 51 | // reopen file for writing 52 | 53 | fp = fopen("books.dat", "wb"); 54 | 55 | current = root; 56 | while(current != NULL) 57 | { 58 | printf("%-30s %-20s\n", current->book.title, current->book.author); 59 | fwrite(&(current->book),sizeof(struct book),1,fp); 60 | current = current->next; 61 | } 62 | fclose(fp); 63 | } 64 | -------------------------------------------------------------------------------- /structs/Largest Time Of 10 Times: -------------------------------------------------------------------------------- 1 | struct time 2 | { 3 | int h,m,s; 4 | }; 5 | 6 | void init_time(struct time * tp) 7 | { 8 | tp -> h = rand() % 24; 9 | tp -> m = rand() % 60; 10 | tp -> s = rand() % 60; 11 | } 12 | void print_time(struct time t) 13 | { 14 | printf("\n%02d:%02d:%02d",t.h, t.m,t.s); 15 | } 16 | 17 | int total_seconds(struct time t) 18 | { 19 | return t.h * 3600 + t.m * 60 + t.s; 20 | } 21 | 22 | int compare_times(struct time t1, struct time t2) 23 | { 24 | return total_seconds(t1) - total_seconds(t2); 25 | } 26 | 27 | main() 28 | { int i; 29 | struct time times[10]; 30 | struct time largest; 31 | 32 | srand(time(0)); 33 | 34 | // fill array 35 | for(i =0; i < 10; i ++) 36 | { 37 | init_time(×[i]); 38 | print_time(times[i]); 39 | } 40 | 41 | largest = times[0]; 42 | 43 | for(i=1; i <10; i ++) 44 | { 45 | if( compare_times(times[i], largest) > 0 ) 46 | largest = times[i]; 47 | } 48 | 49 | printf("\n"); 50 | print_time(largest); 51 | 52 | } 53 | -------------------------------------------------------------------------------- /structs/Month_sales.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct sale 4 | { 5 | char month[4]; 6 | int amount; 7 | }; 8 | 9 | int is_month_present(char * month, struct sale sales[12]) 10 | { 11 | int i; 12 | 13 | for (i=0; i < 12; i ++) 14 | if (stricmp(sales[i].month, month) == 0) 15 | return 1; 16 | 17 | return 0; 18 | } 19 | main() 20 | { 21 | struct sale sales[12]; 22 | int i, pos = 0; 23 | char month[4]; 24 | int amount, status; 25 | 26 | 27 | while(1) 28 | { 29 | fflush(stdin); // clear keyboard buffer 30 | printf("Enter month name : "); 31 | gets(month); 32 | if (stricmp(month,"end") == 0) 33 | break; 34 | status = is_month_present(month,sales); 35 | if(status == 1) // month found 36 | continue; 37 | 38 | printf("Enter sales : "); 39 | scanf("%d",&amount); 40 | strcpy(sales[pos].month, month); 41 | sales[pos].amount = amount; 42 | pos++; 43 | if (pos == 12) // Array is filled 44 | break; 45 | } 46 | 47 | // Print sales 48 | 49 | for (i=0; i < pos; i ++) 50 | { 51 | printf("%s %d\n", sales[i].month, sales[i].amount); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /structs/Print Total Seconds: -------------------------------------------------------------------------------- 1 | struct time 2 | { 3 | int h,m,s; 4 | }; 5 | 6 | struct time read_time() 7 | { 8 | struct time t; 9 | 10 | printf("Enter time [h:m:s] :"); 11 | scanf("%d:%d:%d",&t.h,&t.m,&t.s); 12 | return t; 13 | } 14 | 15 | struct time random_time() 16 | { 17 | struct time t; 18 | 19 | t.h = rand() % 24; 20 | t.m = rand() % 60; 21 | t.s = rand() % 60; 22 | return t; 23 | } 24 | 25 | int total_seconds(struct time t) 26 | { 27 | 28 | return t.h * 3600 + t.m * 60 + t.s; 29 | } 30 | 31 | main() 32 | { 33 | struct time times[5]; 34 | int i; 35 | 36 | srand(time(0)); 37 | 38 | for (i=0; i < 5; i ++) 39 | { 40 | times[i] = random_time(); 41 | printf("%02d:%02d:%02d\n", times[i].h, times[i].m,times[i].s); 42 | } 43 | 44 | for (i=0; i < 5; i ++) 45 | { 46 | printf("%d\n",total_seconds(times[i])); 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /structs/Sort Struct Time Array: -------------------------------------------------------------------------------- 1 | struct time { 2 | int h,m,s; 3 | }; 4 | 5 | int total_seconds(struct time t) { 6 | return t.h * 3600 + t.m * 60 + t.s; 7 | } 8 | 9 | int timecmp(struct time t1, struct time t2) { 10 | return total_seconds(t1) - total_seconds(t2); 11 | } 12 | 13 | void fill_times(struct time t[]) { 14 | int i; 15 | 16 | srand(time(0)); 17 | for(i=0; i <10; i ++) 18 | { 19 | t[i].h = rand() % 24; 20 | t[i].m = rand() % 60; 21 | t[i].s = rand() % 60; 22 | } 23 | } 24 | 25 | void print(struct time t[]) { 26 | int i; 27 | 28 | for(i=0; i <10; i ++) 29 | { 30 | printf("\n%02d:%02d:%02d",t[i].h, t[i].m, t[i].s); 31 | } 32 | } 33 | 34 | main() 35 | { 36 | struct time t[10], temp; 37 | int i,j; 38 | 39 | fill_times(t); 40 | print(t); 41 | 42 | for(i=0; i < 9; i ++) 43 | { 44 | for (j= i + 1; j < 10; j ++) 45 | { 46 | if ( timecmp(t[i], t[j]) > 0) 47 | { 48 | temp = t[i]; 49 | t[i] = t[j]; 50 | t[j] = temp; 51 | } 52 | } 53 | } 54 | printf("\nSorted Array\n"); 55 | print(t); 56 | } 57 | 58 | 59 | -------------------------------------------------------------------------------- /structs/biggest_time.c: -------------------------------------------------------------------------------- 1 | struct time 2 | { 3 | int h,m,s; 4 | }; 5 | 6 | int total_seconds(struct time t) { 7 | return t.h * 3600 + t.m * 60 + t.s; 8 | } 9 | 10 | int timecompare(struct time t1, struct time t2) 11 | { 12 | return total_seconds(t1) - total_seconds(t2); 13 | } 14 | 15 | void print_time(struct time t) 16 | { 17 | printf("%02d:%02d:%02d\n", t.h, t.m, t.s); 18 | } 19 | 20 | struct time max(struct time t1, struct time t2) 21 | { 22 | return total_seconds(t1) > total_seconds(t2) ? t1 : t2; 23 | } 24 | 25 | main() 26 | { 27 | int i; 28 | struct time times[5]; 29 | struct time largest = {0,0,0}; 30 | 31 | srand(time(0)); 32 | for(i=0; i < 5; i ++) 33 | { 34 | times[i].h = rand() % 24; 35 | times[i].m = rand() % 60; 36 | times[i].s = rand() % 60; 37 | print_time(times[i]); 38 | if (timecompare(times[i],largest) > 0) 39 | largest = times[i]; 40 | } 41 | 42 | print_time(largest); 43 | } 44 | -------------------------------------------------------------------------------- /structs/digits_to_words.c: -------------------------------------------------------------------------------- 1 | /* print words of digits in a number using recursion, string array and reverse number algorithms */ 2 | 3 | char words[10][10]= 4 | { "Zero","One","Two","Three","Four", 5 | "Five","Six","Seven","Eight","Nine"}; 6 | 7 | main() 8 | { 9 | print(695); 10 | putchar('\n'); 11 | print3(695); 12 | putchar('\n'); 13 | print3(695); 14 | } 15 | 16 | void print(int n) 17 | { 18 | if ( n > 0) 19 | { 20 | print( n / 10); 21 | printf("%s ", words[n % 10]); 22 | 23 | } 24 | } 25 | 26 | void print2(int n) 27 | { 28 | char w[10][10]; 29 | int pos=0; 30 | 31 | while (n) 32 | { 33 | strcpy(w[pos],words[ n % 10]); 34 | pos ++; 35 | n /=10; 36 | } 37 | 38 | for(pos --; pos>=0; pos --) 39 | printf("%s ", w[pos]); 40 | } 41 | 42 | // reverse number first and then print words 43 | void print3(int n) 44 | { 45 | int rn=0; 46 | // reverse number 47 | while (n) 48 | { 49 | rn = rn * 10 + n % 10; 50 | n/=10; 51 | }; 52 | 53 | // print words for digits in reverse number 54 | while (rn) 55 | { 56 | printf("%s ", words[ rn % 10]); 57 | rn /=10; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /structs/largest_time.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct time 4 | { 5 | int hours, mins, secs; 6 | }; 7 | 8 | struct time get_largest_time(struct time []); 9 | 10 | main() 11 | { 12 | struct time times[10]; 13 | struct time largest; 14 | int i; 15 | 16 | // initialize random seed 17 | srand(time(NULL)); 18 | 19 | // fill array 20 | for(i=0; i <10 ;i++) 21 | { 22 | times[i].hours = rand() % 24; 23 | times[i].mins = rand() % 60; 24 | times[i].secs = rand() % 60; 25 | print_time(times[i]); 26 | } 27 | 28 | largest = get_largest_time(times); 29 | printf("\nLargest Time"); 30 | print_time(largest); 31 | } 32 | 33 | int total_seconds(struct time t) 34 | { 35 | return t.hours * 3600 + t.mins * 60 + t.secs; 36 | } 37 | 38 | void print_time(struct time t) 39 | { 40 | printf("\n%02d:%02d:%02d",t.hours, t.mins, t.secs); 41 | } 42 | 43 | // returns 0 - t1 == t2, > 0 - t1 > t2 , < 0 - t1 < t2 44 | 45 | int compare_times(struct time t1, struct time t2) 46 | { 47 | return total_seconds(t1) - total_seconds(t2); 48 | } 49 | 50 | struct time get_largest_time(struct time times[10]) 51 | { 52 | int i; 53 | struct time largest = times[0]; 54 | 55 | for(i=1; i <10; i ++) 56 | { 57 | // check whether element is bigger than largest 58 | if ( compare_times(times[i], largest) > 0 ) 59 | largest = times[i]; 60 | } 61 | 62 | return largest; 63 | } 64 | -------------------------------------------------------------------------------- /structs/sort_students_struct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct student 4 | { 5 | char name[20]; 6 | int marks; 7 | }; 8 | 9 | void print_students(struct student s[]) 10 | { int i; 11 | 12 | for(i=0; i < 10; i ++) 13 | { 14 | printf("\n%-10s %3d", s[i].name, s[i].marks); 15 | } 16 | } 17 | 18 | main() 19 | { 20 | struct student studs[10]; 21 | struct student temp; 22 | int i,j; 23 | 24 | srand(time(NULL)); 25 | // load data into array 26 | 27 | for(i=0; i < 10; i ++) 28 | { 29 | sprintf(studs[i].name,"Name%d",i + 1); 30 | studs[i].marks = rand() % 100; 31 | } 32 | printf("\nOriginal List\n"); 33 | print_students(studs); 34 | 35 | // sort array of struct student 36 | for(i=0; i < 9; i ++) 37 | { 38 | for(j = i + 1; j < 10; j ++) 39 | { 40 | if (studs[i].marks > studs[j].marks) 41 | { 42 | temp = studs[i]; 43 | studs[i] = studs[j]; 44 | studs[j] = temp; 45 | } 46 | } 47 | } 48 | 49 | printf("\n\nSorted List\n"); 50 | 51 | print_students(studs); 52 | } 53 | -------------------------------------------------------------------------------- /structs/struct time operations: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct time { 4 | int h,m,s; 5 | }; 6 | 7 | int total_seconds(struct time t) 8 | { 9 | return t.h * 3600 + t.m * 60 + t.s; 10 | } 11 | 12 | /* 0 - t1 == t2, > 0 - t1 > t2, < 0 - t1 < t2 */ 13 | int compare(struct time t1, struct time t2) 14 | { 15 | return total_seconds(t1) - total_seconds(t2); 16 | } 17 | 18 | void print(struct time t) 19 | { 20 | printf("\n%02d:%02d:%02d",t.h, t.m, t.s); 21 | } 22 | 23 | void init(struct time * t) 24 | { 25 | t -> h = rand() % 24; 26 | t -> m = rand() % 60; 27 | t -> s = rand() % 60; 28 | } 29 | 30 | void main() 31 | { 32 | struct time a[5], lt; 33 | int i; 34 | 35 | srand(time(0)); 36 | 37 | for(i = 0; i < 5; i ++) 38 | { 39 | init(&a[i]); 40 | print(a[i]); 41 | } 42 | 43 | lt = a[0]; 44 | 45 | for(i=1; i < 5; i ++) 46 | { 47 | // if a[i] > lt 48 | if ( compare(a[i], lt) > 0) 49 | lt = a[i]; 50 | } 51 | 52 | printf("\nLargest Time\n"); 53 | print(lt); 54 | } 55 | -------------------------------------------------------------------------------- /structs/time_operations.c: -------------------------------------------------------------------------------- 1 | struct time 2 | { 3 | int h,m,s; 4 | }; 5 | 6 | struct time read_time(); // declaration 7 | 8 | main() 9 | { 10 | struct time t1 = {1,20,30}; 11 | struct time t2; 12 | 13 | t2 = read_time(); 14 | 15 | printf("Result : %d\n", compare(t1,t2)); 16 | 17 | print_time(t1); 18 | } 19 | 20 | void print_time(struct time t) 21 | { 22 | printf("%02d:%02d:%02d", t.h,t.m,t.s); 23 | } 24 | 25 | int compare(struct time t1, struct time t2) 26 | { 27 | return get_total_seconds(t1) - get_total_seconds(t2); 28 | } 29 | 30 | int get_total_seconds(struct time t) 31 | { 32 | return t.h * 3600 + t.m *60 + t.s; 33 | } 34 | 35 | struct time read_time() 36 | { 37 | struct time t; 38 | 39 | printf("Enter time [hh:mm:ss] : "); 40 | scanf("%d:%d:%d", &t.h,&t.m, &t.s); 41 | return t; 42 | } 43 | 44 | struct time next_second(struct time t) 45 | { 46 | t.s ++; 47 | if ( t.s == 60) 48 | { 49 | t.s = 0; 50 | t.m ++; 51 | if ( t.m == 60) 52 | { 53 | t.m =0; 54 | t.h ++; 55 | if (t.h == 24) 56 | t.h = 0; 57 | } 58 | } 59 | 60 | return t; 61 | 62 | } 63 | -------------------------------------------------------------------------------- /upload.bat: -------------------------------------------------------------------------------- 1 | rem add all files 2 | git add . 3 | 4 | rem commit with the given message 5 | git commit -m %1 6 | 7 | rem push master branch of this remote project 8 | git push c_github master 9 | 10 | 11 | --------------------------------------------------------------------------------