0% found this document useful (0 votes)
4 views4 pages

Assignment (More On String) .C

The document contains three C programs. The first program counts the number of alphabets, digits, and special characters in a string. The second program sorts a string array in ascending order, and the third program replaces lowercase characters with uppercase and vice versa in a given sentence.

Uploaded by

papercloudds
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)
4 views4 pages

Assignment (More On String) .C

The document contains three C programs. The first program counts the number of alphabets, digits, and special characters in a string. The second program sorts a string array in ascending order, and the third program replaces lowercase characters with uppercase and vice versa in a given sentence.

Uploaded by

papercloudds
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/ 4

1.

Write a program in C to count total number of


alphabets, digits and special characters in a string.
#include <stdio.h>

#include <ctype.h>

int main() {

char str[100];

int alphabets = 0, digits = 0, specialChars = 0;

printf("Enter a string : ");

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

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

if (isalpha(str[i])) {

alphabets++;

else if (isdigit(str[i])) {

digits++;

else if (isspace(str[i])) {

specialChars++;

printf("No. Of Alphabets: %d\n", alphabets);

printf("No. Of Digits: %d\n", digits);

printf("No. Of Special characters: %d\n", specialChars);

return 0;

}
OUTPUT:-

2.Write a C program to sort a string array in ascending


order.

#include <stdio.h>

#include <string.h>

#include <ctype.h>

void sortString(char str[]) {

int n = strlen(str);

for (int i = 0; i < n - 1; i++) {

for (int j = i + 1; j < n; j++) {

if (str[i] > str[j]) {

char temp = str[i];

str[i] = str[j];

str[j] = temp;

int main() {

char str[100];
printf("Input the string: ");

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

sortString(str);

printf("After sorting the string appears like: %s", str);

return 0;

OUTPUT:-

3.Write a program in C to read a sentence and replace


lowercase characters by uppercase and vice-versa.

#include <stdio.h>

#include <ctype.h>

int main() {

char str[100];

printf("Input the string: ");

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

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

if (islower(str[i])) {

str[i] = toupper(str[i]);

} else if (isupper(str[i])) {

str[i] = tolower(str[i]);

}
printf("After case conversion, the string is: %s", str);

return 0;

OUTPUT:-

You might also like