1. Program to Input a String and Print It Using a Pointer
#include <stdio.h>
int main() {
char str[100]; // Declare a character array to store the string
char *ptr = str; // Pointer to the first character of the string
// Input the string
printf("Enter a string: ");
scanf("%s", str); // Use scanf to read the string
// Print the string using a pointer
printf("The string is: ");
while (*ptr != '\0') {
putchar(*ptr); // Print each character until null terminator
ptr++; // Move to the next character
}
printf("\n");
return 0;
}
2. Program to Count the Number of Vowels and Consonants in a String Using Pointers
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100]; // Declare a character array to store the string
char *ptr = str; // Pointer to the first character of the string
int vowels = 0, consonants = 0;
// Input the string
printf("Enter a string: ");
scanf("%s", str); // Use scanf to read the string
// Count vowels and consonants
while (*ptr != '\0') {
char ch = tolower(*ptr); // Convert to lowercase for comparison
if (ch >= 'a' && ch <= 'z') { // Check if the character is an alphabet
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++; // Count vowels
} else {
consonants++; // Count consonants
}
}
ptr++; // Move to the next character
}
// Print the results
printf("Number of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
return 0;
}
3. Program to Reverse a String Using a Pointer
#include <stdio.h>
#include <string.h>
int main() {
char str[100]; // Declare a character array to store the string
char *ptr = str; // Pointer to the first character of the string
int len, i;
// Input the string
printf("Enter a string: ");
scanf("%s", str); // Use scanf to read the string
// Get the length of the string
len = strlen(str);
// Reverse the string using pointer
printf("The reversed string is: ");
for (i = len - 1; i >= 0; i--) {
putchar(*(ptr + i)); // Print each character from the end
}
printf("\n");
return 0;
}
4. Program to Compare Two Strings Using Pointers
#include <stdio.h>
int main() {
char str1[100], str2[100];
char *ptr1 = str1;
char *ptr2 = str2;
printf("Enter first string: ");
scanf("%s", str1); // Read first string
printf("Enter second string: ");
scanf("%s", str2); // Read second string
// Compare strings using pointers
while (*ptr1 && (*ptr1 == *ptr2)) {
ptr1++;
ptr2++;
}
// Check the result of the comparison
if (*ptr1 == *ptr2) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
5. Program to Concatenate Two Strings Using Pointers
#include <stdio.h>
int main() {
char str1[100], str2[100];
char *ptr1 = str1;
char *ptr2 = str2;
char *ptr;
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
// Concatenate the strings using pointers
ptr = str1;
while (*ptr) {
ptr++;
}
while (*ptr2) {
*ptr = *ptr2;
ptr++;
ptr2++;
}
*ptr = '\0'; // Null-terminate the concatenated string
// Print the concatenated string
printf("Concatenated string: %s\n", str1);
return 0;
}