0% found this document useful (0 votes)
3 views

STRING PROGRAMS

The document contains a series of C programs that demonstrate various string manipulation techniques. These include converting strings between upper and lower case, sorting strings, finding string length without using built-in functions, concatenating strings, and reversing a string using recursion. Each program is accompanied by code snippets and explanations of their functionality.

Uploaded by

thanoojpamanji
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)
3 views

STRING PROGRAMS

The document contains a series of C programs that demonstrate various string manipulation techniques. These include converting strings between upper and lower case, sorting strings, finding string length without using built-in functions, concatenating strings, and reversing a string using recursion. Each program is accompanied by code snippets and explanations of their functionality.

Uploaded by

thanoojpamanji
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
You are on page 1/ 3

STRING PROGRAMS​


// 1. Convert string from upper case to lower case
#include <stdio.h>
int main() {
char str[100];
printf("Enter string: ");
scanf("%s", str);
for (int i = 0; str[i]; i++)
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] += 32;
printf("Lowercase: %s\n", str);
return 0;
}

c
CopyEdit
// 2. Convert string from lower case to upper case
#include <stdio.h>
int main() {
char str[100];
printf("Enter string: ");
scanf("%s", str);
for (int i = 0; str[i]; i++)
if (str[i] >= 'a' && str[i] <= 'z')
str[i] -= 32;
printf("Uppercase: %s\n", str);
return 0;
}

c
CopyEdit
// 3. Sort a set of strings in ascending alphabetical order
#include <stdio.h>
#include <string.h>
void sortStrings(char arr[][100], int n) {
char temp[100];
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() {
int n;
printf("Enter number of strings: ");
scanf("%d", &n);
char arr[n][100];
printf("Enter strings:\n");
for (int i = 0; i < n; i++)
scanf("%s", arr[i]);
sortStrings(arr, n);
printf("Sorted strings:\n");
for (int i = 0; i < n; i++)
printf("%s\n", arr[i]);
return 0;
}

c
CopyEdit
// 4. Find length of a string without using strlen()
#include <stdio.h>
int main() {
char str[100];
int len = 0;
printf("Enter string: ");
scanf("%s", str);
while (str[len]) len++;
printf("Length: %d\n", len);
return 0;
}

c
CopyEdit
// 5. String concatenation without using strcat
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i = 0, j = 0;
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
while (str1[i]) i++;
while (str2[j]) str1[i++] = str2[j++];
str1[i] = '\0';
printf("Concatenated String: %s\n", str1);
return 0;
}

c
CopyEdit
// 6. Reverse a String using recursion
#include <stdio.h>
void reverse(char str[], int start, int end) {
if (start >= end) return;
char temp = str[start];
str[start] = str[end];
str[end] = temp;
reverse(str, start + 1, end - 1);
}
int main() {
char str[100];
printf("Enter string: ");
scanf("%s", str);
int len = 0;
while (str[len]) len++;
reverse(str, 0, len - 1);
printf("Reversed String: %s\n", str);
return 0;
}

You might also like