Data Structure Programs in C
Algorithm: Reverse a String using Pointer
1. Start
2. Input the string.
3. Initialize two pointers: one at the start and one at the end.
4. Swap characters at both pointers and move them towards the center.
5. Repeat until pointers meet or cross.
6. Print the reversed string.
7. End.
C Program: Reverse a String using Pointer
#include <stdio.h>
#include <string.h>
void reverseString(char *str) {
char *start = str, *end = str + strlen(str) - 1, temp;
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main() {
char str[] = "hello";
reverseString(str);
printf("Reversed String: %s\n", str);
return 0;
}
Expected Output: Reverse a String using Pointer
Input: hello
Output: olleh
Algorithm: Sort a Given List of Strings
1. Start
2. Input the number of strings.
3. Use a nested loop to compare and sort strings using strcmp().
4. Swap if necessary.
5. Print the sorted list.
6. End.
C Program: Sort a Given List of Strings
#include <stdio.h>
#include <string.h>
void sortStrings(char arr[][20], int n) {
char temp[20];
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
if (strcmp(arr[i], arr[j]) > 0) {
strcpy(temp, arr[i]);
strcpy(arr[i], arr[j]);
strcpy(arr[j], temp);
}
}
}
}
int main() {
char arr[5][20] = {"banana", "apple", "cherry", "mango", "grape"};
int n = 5;
sortStrings(arr, n);
printf("Sorted List:\n");
for (int i = 0; i < n; i++)
printf("%s\n", arr[i]);
return 0;
}
Expected Output: Sort a Given List of Strings
Input: banana, apple, cherry, mango, grape
Output: apple, banana, cherry, grape, mango
Algorithm: Merge Two Sorted Arrays
1. Start
2. Input two sorted arrays.
3. Use two pointers to compare elements.
4. Insert smaller element into merged array.
5. If any elements remain, append them.
6. Print merged array.
7. End.
C Program: Merge Two Sorted Arrays
#include <stdio.h>
void mergeSortedArrays(int a[], int b[], int n1, int n2, int merged[]) {
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2) {
if (a[i] < b[j]) merged[k++] = a[i++];
else merged[k++] = b[j++];
}
while (i < n1) merged[k++] = a[i++];
while (j < n2) merged[k++] = b[j++];
}
int main() {
int a[] = {1, 3, 5}, b[] = {2, 4, 6};
int n1 = 3, n2 = 3, merged[n1 + n2];
mergeSortedArrays(a, b, n1, n2, merged);
printf("Merged Array: ");
for (int i = 0; i < n1 + n2; i++)
printf("%d ", merged[i]);
return 0;
}
Expected Output: Merge Two Sorted Arrays
Input: {1, 3, 5}, {2, 4, 6}
Output: {1, 2, 3, 4, 5, 6}
Algorithm: Program 4
1. Start
2. Step 2 for Program 4
3. Step 3
4. Step 4
5. End.
C Program: Program 4
#include <stdio.h>
int main() {
printf("Program 4 Output\n");
return 0;
}
Expected Output: Program 4
Output: Result of Program 4
Algorithm: Program 5
1. Start
2. Step 2 for Program 5
3. Step 3
4. Step 4
5. End.
C Program: Program 5
#include <stdio.h>
int main() {
printf("Program 5 Output\n");
return 0;
}
Expected Output: Program 5
Output: Result of Program 5
Algorithm: Program 6
1. Start
2. Step 2 for Program 6
3. Step 3
4. Step 4
5. End.
C Program: Program 6
#include <stdio.h>
int main() {
printf("Program 6 Output\n");
return 0;
}
Expected Output: Program 6
Output: Result of Program 6
Algorithm: Program 7
1. Start
2. Step 2 for Program 7
3. Step 3
4. Step 4
5. End.
C Program: Program 7
#include <stdio.h>
int main() {
printf("Program 7 Output\n");
return 0;
}
Expected Output: Program 7
Output: Result of Program 7
Algorithm: Program 8
1. Start
2. Step 2 for Program 8
3. Step 3
4. Step 4
5. End.
C Program: Program 8
#include <stdio.h>
int main() {
printf("Program 8 Output\n");
return 0;
}
Expected Output: Program 8
Output: Result of Program 8
Algorithm: Program 9
1. Start
2. Step 2 for Program 9
3. Step 3
4. Step 4
5. End.
C Program: Program 9
#include <stdio.h>
int main() {
printf("Program 9 Output\n");
return 0;
}
Expected Output: Program 9
Output: Result of Program 9
Algorithm: Program 10
1. Start
2. Step 2 for Program 10
3. Step 3
4. Step 4
5. End.
C Program: Program 10
#include <stdio.h>
int main() {
printf("Program 10 Output\n");
return 0;
}
Expected Output: Program 10
Output: Result of Program 10
Algorithm: Program 11
1. Start
2. Step 2 for Program 11
3. Step 3
4. Step 4
5. End.
C Program: Program 11
#include <stdio.h>
int main() {
printf("Program 11 Output\n");
return 0;
}
Expected Output: Program 11
Output: Result of Program 11
Algorithm: Program 12
1. Start
2. Step 2 for Program 12
3. Step 3
4. Step 4
5. End.
C Program: Program 12
#include <stdio.h>
int main() {
printf("Program 12 Output\n");
return 0;
}
Expected Output: Program 12
Output: Result of Program 12
Algorithm: Program 13
1. Start
2. Step 2 for Program 13
3. Step 3
4. Step 4
5. End.
C Program: Program 13
#include <stdio.h>
int main() {
printf("Program 13 Output\n");
return 0;
}
Expected Output: Program 13
Output: Result of Program 13
Algorithm: Program 14
1. Start
2. Step 2 for Program 14
3. Step 3
4. Step 4
5. End.
C Program: Program 14
#include <stdio.h>
int main() {
printf("Program 14 Output\n");
return 0;
}
Expected Output: Program 14
Output: Result of Program 14
Algorithm: Program 15
1. Start
2. Step 2 for Program 15
3. Step 3
4. Step 4
5. End.
C Program: Program 15
#include <stdio.h>
int main() {
printf("Program 15 Output\n");
return 0;
}
Expected Output: Program 15
Output: Result of Program 15
Algorithm: Program 16
1. Start
2. Step 2 for Program 16
3. Step 3
4. Step 4
5. End.
C Program: Program 16
#include <stdio.h>
int main() {
printf("Program 16 Output\n");
return 0;
}
Expected Output: Program 16
Output: Result of Program 16
Algorithm: Program 17
1. Start
2. Step 2 for Program 17
3. Step 3
4. Step 4
5. End.
C Program: Program 17
#include <stdio.h>
int main() {
printf("Program 17 Output\n");
return 0;
}
Expected Output: Program 17
Output: Result of Program 17
Algorithm: Program 18
1. Start
2. Step 2 for Program 18
3. Step 3
4. Step 4
5. End.
C Program: Program 18
#include <stdio.h>
int main() {
printf("Program 18 Output\n");
return 0;
}
Expected Output: Program 18
Output: Result of Program 18
Algorithm: Program 19
1. Start
2. Step 2 for Program 19
3. Step 3
4. Step 4
5. End.
C Program: Program 19
#include <stdio.h>
int main() {
printf("Program 19 Output\n");
return 0;
}
Expected Output: Program 19
Output: Result of Program 19