0% found this document useful (0 votes)
7 views9 pages

String

The document contains multiple C programs demonstrating string manipulation techniques, including separating characters, reversing strings, comparing strings, counting vowels and consonants, concatenating strings, and concatenating user-input strings. Each program includes user prompts, string input handling, and relevant output display. The code snippets illustrate fundamental programming concepts in C related to string operations.

Uploaded by

Shashank Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

String

The document contains multiple C programs demonstrating string manipulation techniques, including separating characters, reversing strings, comparing strings, counting vowels and consonants, concatenating strings, and concatenating user-input strings. Each program includes user prompts, string input handling, and relevant output display. The code snippets illustrate fundamental programming concepts in C related to string operations.

Uploaded by

Shashank Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Characters Separate of String

#include <stdio.h>

#include <stdlib.h>

int main() {

char str[100]; /* Declares a string of size 100 */

int i = 0; // Initialize a variable to store the index of the string

printf("\n\nSeparate the individual characters from a string :\n"); // Display information


about the task

printf("------------------------------------------------------\n");

printf("Input the string : ");

// Read a string from the standard input (keyboard) using fgets()

fgets(str, sizeof(str), stdin);

printf("The characters of the string are : \n");

// Loop to print each individual character of the string until the null terminator '\0' is
encountered

while (str[i] != '\0') {

printf("%c ", str[i]); // Print each character

i++; // Move to the next character in the string

printf("\n");

return 0; // Return 0 to indicate successful execution of the program


}

Reverse String
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int main() {

char str[100]; /* Declares a string of size 100 */

int n, i; // Declare variables for string length and loop iteration

printf("\n\nPrint individual characters of string in reverse order :\n"); // Display


information about the task

printf("------------------------------------------------------\n");

printf("Input the string : ");

// Read a string from the standard input (keyboard) using fgets()

fgets(str, sizeof(str), stdin);

n = strlen(str); // Calculate the length of the string

printf("The characters of the string in reverse are : \n");

// Loop to print each individual character of the string in reverse order

for (i = n-1 ; i >= 0; i--) {

printf("%c ", str[i]); // Print each character in reverse order

}
printf("\n");

return 0; // Return 0 to indicate successful execution of the program

Compare Strings
#include <stdio.h>

#define SIZE 100

int compareStrings(char s1[], char s2[]) {

int i = 0, flag = 0;

while (s1[i] != '\0' || s2[i] != '\0') {

if (s1[i] != s2[i]) {

flag = 1; // Strings are not equal

break;

i++;

return flag;

int main() {

char str1[SIZE], str2[SIZE];


int flag;

printf("Enter first string: ");

fgets(str1, SIZE, stdin);

printf("Enter second string: ");

fgets(str2, SIZE, stdin);

flag = compareStrings(str1, str2);

if (flag == 0)

printf("Strings are equal.\n");

else

printf("Strings are not equal.\n");

return 0;

Vowel and Consonant


#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define str_size 100 // Declare the maximum size of the string

int main() {

char str[str_size]; // Declare a character array to store the string


int i, len, vowel, cons; // Declare variables for iteration, string length, vowel count, and
consonant count

printf("\n\nCount total number of vowel or consonant :\n"); // Display information about


the task

printf("----------------------------------------------\n");

printf("Input the string : ");

fgets(str, sizeof str, stdin); // Read a string from the standard input (keyboard)

vowel = 0; // Initialize vowel count to zero

cons = 0; // Initialize consonant count to zero

len = strlen(str); // Get the length of the input string

// Loop to check each character of the string

for (i = 0; i < len; i++) {

// Check if the character is a vowel (both lowercase and uppercase)

if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i] == 'A' ||
str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {

vowel++; // Increment the vowel count if the character is a vowel

// Check if the character is an alphabet (both lowercase and uppercase)

else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {

cons++; // Increment the consonant count if the character is an alphabet but not a
vowel

// Display the total number of vowels and consonants in the string

printf("\nThe total number of vowel in the string is : %d\n", vowel);


printf("The total number of consonant in the string is : %d\n\n", cons);

return 0; // Return 0 to indicate successful execution of the program

Concatenate string
#include <stdio.h>

#include <string.h>

int main() {

char str1[100], str2[100], i, j, l, m, k; // Declare character arrays and variables for iteration

printf("\n\nConcatenate Two Strings Manually :\n"); // Display information about the task

printf("-------------------------------------\n");

printf("Input the first string : ");

fgets(str1, sizeof str1, stdin); // Read the first string from standard input

printf("Input the second string : ");

fgets(str2, sizeof str2, stdin); // Read the second string from standard input

l = strlen(str1); // Get the length of the first string

m = strlen(str2); // Get the length of the second string

for (i = 0; i < l - 1; ++i); /* value i contains reaches the end of string str1. */

str1[i] = ' '; /* Add a space with string str1. */

i++; /* Increase i by 1 for the blank space */


for (j = 0; j < m - 1; ++j, ++i) {

str1[i] = str2[j]; // Concatenate str2 to str1 after the added space

k = strlen(str1); // Get the length of the concatenated string

printf("After concatenation the string is : \n ");

for (i = 0; i < k; ++i) { // Display the concatenated string

printf("%c", str1[i]);

printf("\n\n");

return 0; // Return 0 to indicate successful execution of the program

Concat strings with user input


#include <stdio.h>

// Function to remove newline character if present

void skip(char s[]) {

for (int i = 0; s[i] != '\0'; i++) {

if (s[i] == '\n') {

s[i] = '\0';

break;

}
}

// Function to concatenate two strings without using pointers

void concat(char s1[], char s2[]) {

int i = 0, j = 0;

// Find the end of s1

while (s1[i] != '\0') {

i++;

// Append s2 to s1

while (s2[j] != '\0') {

s1[i] = s2[j];

i++;

j++;

// Add null terminator at the end

s1[i] = '\0';

int main() {

char s1[100], s2[100];

printf("Enter first string: ");

fgets(s1, sizeof(s1), stdin);

skip(s1); // Fix: pass s1


printf("Enter second string: ");

fgets(s2, sizeof(s2), stdin);

skip(s2); // Fix: pass s2

concat(s1, s2);

printf("After concatenation: %s\n", s1);

return 0;

You might also like