Posts

Showing posts with the label c

✅ Day 5 Solution - DSA with C

  ✅ 1. Implement your own strlen() # include <stdio.h> int my_strlen ( char str[]) { int i = 0 ; while (str[i] != '\0' ) { i++; } return i; } int main () { char str[ 100 ]; printf ( "Enter a string: " ); scanf ( "%s" , str); printf ( "Length of string = %d\n" , my_strlen(str)); return 0 ; } ✅ 2. Implement your own strcpy() # include <stdio.h> void my_strcpy ( char dest[], char src[]) { int i = 0 ; while (src[i] != '\0' ) { dest[i] = src[i]; i++; } dest[i] = '\0' ; // Null-terminate } int main () { char src[ 100 ], dest[ 100 ]; printf ( "Enter a string to copy: " ); scanf ( "%s" , src); my_strcpy(dest, src); printf ( "Copied string: %s\n" , dest); return 0 ; } ✅ 3. Implement your own strcmp() # include <stdio.h> int my_strcmp ( char str1[], char st...

๐Ÿš€ DSA with C – Day 5: Strings in C

๐ŸŽฏ Goal: Understand how strings work in C Perform common string operations: Reverse a string Check for palindrome Count character frequency Check if two strings are anagrams ๐Ÿง  What is a String in C? A string is a character array ending with a null character ( \0 ) . ๐Ÿ”ง Declaration char str[ 100 ]; // Character array ๐Ÿ“ฅ Input scanf ( "%s" , str); // No spaces fgets(str, sizeof (str), stdin ); // With spaces ๐Ÿ‘จ‍๐Ÿ’ป 1. Reverse a String # include <stdio.h> # include <string.h> int main () { char str[ 100 ]; printf ( "Enter a string: " ); scanf ( "%s" , str); // Use fgets if you want spaces int len = strlen (str); for ( int i = 0 ; i < len / 2 ; i++) { char temp = str[i]; str[i] = str[len - i - 1 ]; str[len - i - 1 ] = temp; } printf ( "Reversed string: %s\n" , str); return 0 ; } ๐Ÿ‘จ‍๐Ÿ’ป 2. Check if a String is a Palindrome ...

✅ Day 4 solution - DSA with C

 Find the GCD of Two Numbers Using Recursion GCD (Greatest Common Divisor) using Euclidean Algorithm : # include <stdio.h> int gcd ( int a, int b) { if (b == 0 ) return a; return gcd(b, a % b); } int main () { int num1, num2; printf ( "Enter two numbers: " ); scanf ( "%d %d" , &num1, &num2); printf ( "GCD of %d and %d is %d\n" , num1, num2, gcd(num1, num2)); return 0 ; } ✅ 2. Recursive Function to Calculate Power (xโฟ) # include <stdio.h> int power ( int x, int n) { if (n == 0 ) return 1 ; return x * power(x, n - 1 ); } int main () { int base, exponent; printf ( "Enter base and exponent: " ); scanf ( "%d %d" , &base, &exponent); printf ( "%d^%d = %d\n" , base, exponent, power(base, exponent)); return 0 ; } ✅ 3. Reverse an Array Using Recursion # include <stdio.h> void reverse ( int ...

๐Ÿš€ DSA with C – Day 4: Recursion + Factorial + Fibonacci + Array Sum

  ๐ŸŽฏ Goal: Understand the concept of Recursion Write recursive functions for: Factorial Fibonacci series Sum of array elements Compare recursion with iteration ๐Ÿง  What is Recursion? A function calling itself to solve a smaller version of the same problem. ๐Ÿ”„ Recursion Structure: return_type function_name (parameters) { if (base_condition) return result; else return function_name(smaller_problem); } ๐Ÿ‘จ‍๐Ÿ’ป 1. Factorial Using Recursion # include <stdio.h> int factorial ( int n) { if (n == 0 || n == 1 ) return 1 ; else return n * factorial(n - 1 ); } int main () { int num; printf ( "Enter a number: " ); scanf ( "%d" , &num); printf ( "Factorial of %d is %d\n" , num, factorial(num)); return 0 ; } ๐Ÿ‘จ‍๐Ÿ’ป 2. Fibonacci Series Using Recursion # include <stdio.h> int fibonacci ( int n) { if (n == 0 ) return 0 ; if ...

๐Ÿš€ DSA with C – Day 3: Bubble Sort & Binary Search

  ๐ŸŽฏ Goal: Learn Bubble Sort and sort arrays. Understand how Binary Search works. Compare Linear vs Binary Search. Practice problems on sorted arrays. ๐Ÿง  Theory ✅ Bubble Sort: Repeatedly compares adjacent elements and swaps them if they’re in the wrong order. Worst-case time complexity: O(n²) Best-case (already sorted): O(n) with optimization ๐Ÿ‘จ‍๐Ÿ’ป Bubble Sort in C # include <stdio.h> int main () { int arr[ 100 ], n, i, j, temp; printf ( "Enter number of elements: " ); scanf ( "%d" , &n); printf ( "Enter %d elements:\n" , n); for (i = 0 ; i < n; i++) scanf ( "%d" , &arr[i]); // Bubble Sort for (i = 0 ; i < n - 1 ; i++) { int swapped = 0 ; for (j = 0 ; j < n - i - 1 ; j++) { if (arr[j] > arr[j + 1 ]) { // Swap temp = arr[j]; arr[j] = arr[j + 1 ]; ...

๐Ÿงช Practice Problems Answer Day 2 DSA with C

  ✅ 1. Find Maximum and Minimum in an Array # include <stdio.h> int main () { int n, i; int arr[ 100 ], max, min; printf ( "Enter number of elements: " ); scanf ( "%d" , &n); printf ( "Enter %d elements:\n" , n); for (i = 0 ; i < n; i++) { scanf ( "%d" , &arr[i]); } max = min = arr[ 0 ]; for (i = 1 ; i < n; i++) { if (arr[i] > max) max = arr[i]; if (arr[i] < min) min = arr[i]; } printf ( "Maximum: %d\n" , max); printf ( "Minimum: %d\n" , min); return 0 ; } ✅ 2. Count Occurrences of a Given Number # include <stdio.h> int main () { int n, x, count = 0 ; int arr[ 100 ]; printf ( "Enter number of elements: " ); scanf ( "%d" , &n); printf ( "Enter %d elements:\n" , n); for ( int i = 0 ; i < n; i++) { ...

๐Ÿš€ DSA with C – Day 2: Arrays & Linear Search

  ๐ŸŽฏ Goal: Understand arrays in C. Learn how to use loops with arrays . Implement Linear Search algorithm. Practice multiple array-based programs. ๐Ÿง  Theory: What is an Array? An array is a collection of elements stored in contiguous memory . All elements must be of the same type . Indexing starts from 0 in C. ๐Ÿ”ง Declaration and Initialization int arr[ 5 ]; // Declaration int arr[ 5 ] = { 1 , 2 , 3 , 4 , 5 }; // Initialization ๐Ÿ‘จ‍๐Ÿ’ป Basic Program: Input & Output in Arrays # include <stdio.h> int main () { int arr[ 5 ]; printf ( "Enter 5 numbers:\n" ); for ( int i = 0 ; i < 5 ; i++) { scanf ( "%d" , &arr[i]); } printf ( "You entered:\n" ); for ( int i = 0 ; i < 5 ; i++) { printf ( "%d " , arr[i]); } return 0 ; } ๐Ÿ” Linear Search in C ➕ Problem: Given an array and a number x , find if x exists in the array. ✅ Code...

๐Ÿš€ DSA with C – Day 1: Introduction & Setup

  ๐Ÿš€ DSA with C – Day 1: Introduction & Setup ๐ŸŽฏ Goal: Understand what DSA is. Set up your C environment. Write your first C program. Learn about time & space complexity (theory). Practice basic input/output and loops. ๐Ÿง  Theory: What is DSA? Data Structures = Ways to organize and store data efficiently. Algorithms = Step-by-step instructions to solve problems. Why DSA matters: Faster apps, better problem-solving, cracking tech interviews. ๐Ÿ”ง Setup for C Programming Install a C compiler: Windows : Use Code::Blocks or install MinGW and use VS Code. Mac/Linux : Already comes with gcc . Use VS Code or terminal. Create your first .c file. ๐Ÿ‘จ‍๐Ÿ’ป Hello World Program # include <stdio.h> int main () { printf ( "Hello, World!\n" ); return 0 ; } Compile using: gcc hello.c -o hello ./hello ๐Ÿ“š Time & Space Complexity (Intro) Big O Notation (O(n), O(1), etc.) Example: Loop runs n times → ...