1. Program to Input a String and Print Its Length
#include <stdio.h>
int main() {
char str[100];
int length = 0;
// Input the string
printf("Enter a string: ");
scanf("%s", str);
// Calculate length of the string
while (str[length] != '\0') {
length++;
}
// Print the length
printf("The length of the string is: %d\n", length);
return 0;
}
2. Program to Concatenate Two Strings Without Using strcat()
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i = 0, j = 0;
// Input two strings
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
// Find the end of the first string
while (str1[i] != '\0') {
i++;
}
// Append the second string to the first string
while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0'; // Null-terminate the concatenated string
// Print the concatenated string
printf("Concatenated string: %s\n", str1);
return 0;
}
3. Program to Compare Two Strings Without Using strcmp()
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i = 0, flag = 0;
// Input two strings
printf("Enter first string: ");
scanf("%s", str1); // Read first string
printf("Enter second string: ");
scanf("%s", str2); // Read second string
// Compare the strings character by character
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
flag = 1; // Strings are not equal
break;
}
i++;
}
// Check if the strings are equal
if (flag == 0 && str1[i] == '\0' && str2[i] == '\0') {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
4. Program to Copy One String Into Another Without Using strcpy()
#include <stdio.h>
int main() {
char src[100], dest[100];
// Input the source string
printf("Enter a string: ");
scanf("%s", src);
// Copy source string into destination string
while (src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
// Print the copied string
printf("Copied string: %s\n", dest);
return 0;
}
5. Program to Reverse a String Without Using strrev()
#include <stdio.h>
int main() {
char str[100];
int i = 0, j = 0;
char temp;
// Input the string
printf("Enter a string: ");
scanf("%s", str);
// Find the length of the string
while (str[j] != '\0') {
j++;
}
j--; // Set j to the last character of the string (before null terminator)
// Reverse the string using two-pointer technique
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
// Print the reversed string
printf("Reversed string: %s\n", str);
return 0;
}