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

C_Programming_Codes

The document contains various C programming code snippets that demonstrate string manipulation techniques. These include finding the length of a string, counting vowels and consonants, replacing spaces, extracting substrings, counting different character types, converting strings to uppercase, and checking if a character is uppercase. Each code snippet includes a main function for user input and output display.

Uploaded by

s73646079
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

C_Programming_Codes

The document contains various C programming code snippets that demonstrate string manipulation techniques. These include finding the length of a string, counting vowels and consonants, replacing spaces, extracting substrings, counting different character types, converting strings to uppercase, and checking if a character is uppercase. Each code snippet includes a main function for user input and output display.

Uploaded by

s73646079
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/ 5

C Programming Codes

Find the length of a string without using library function

#include <stdio.h>

int stringLength(char *str) {

int length = 0;

while (str[length]) length++;

return length;

int main() {

char str[100];

printf("Enter a string: ");

gets(str);

printf("Length: %d\n", stringLength(str));

return 0;

Count vowels and consonants in a string

#include <stdio.h>

#include <ctype.h>

void countVC(char *str, int *vowels, int *consonants) {

while (*str) {

char ch = tolower(*str++);

if (ch >= 'a' && ch <= 'z')

(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ?

(*vowels)++ : (*consonants)++;

}
}

int main() {

char str[100];

int v = 0, c = 0;

printf("Enter a string: ");

gets(str);

countVC(str, &v, &c);

printf("Vowels: %d, Consonants: %d\n", v, c);

return 0;

Replace spaces in a string with a character

#include <stdio.h>

void replaceSpaces(char *str, char ch) {

while (*str) {

if (*str == ' ') *str = ch;

str++;

int main() {

char str[100], ch;

printf("Enter a string: ");

gets(str);

printf("Replacement character: ");

scanf(" %c", &ch);

replaceSpaces(str, ch);

printf("Modified: %s\n", str);

return 0;
}

Extract a substring

#include <stdio.h>

void substring(char *str, int start, int len, char *result) {

for (int i = 0; i < len && str[start + i]; i++)

result[i] = str[start + i];

result[len] = '\0';

int main() {

char str[100], result[100];

int start, len;

printf("Enter a string: ");

gets(str);

printf("Start position: ");

scanf("%d", &start);

printf("Length: ");

scanf("%d", &len);

substring(str, start - 1, len, result);

printf("Substring: %s\n", result);

return 0;

Count alphabets, digits, and special characters

#include <stdio.h>

#include <ctype.h>

void countChars(char *str, int *alphabets, int *digits, int *special) {

while (*str) {
if (isalpha(*str)) (*alphabets)++;

else if (isdigit(*str)) (*digits)++;

else (*special)++;

str++;

int main() {

char str[100];

int a = 0, d = 0, s = 0;

printf("Enter a string: ");

gets(str);

countChars(str, &a, &d, &s);

printf("Alphabets: %d, Digits: %d, Special Characters: %d\n", a, d, s);

return 0;

Convert a string to uppercase

#include <stdio.h>

void toUpperCase(char *str) {

while (*str) {

if (*str >= 'a' && *str <= 'z') *str -= 32;

str++;

int main() {

char str[100];

printf("Enter a string: ");

gets(str);
toUpperCase(str);

printf("Uppercase: %s\n", str);

return 0;

Check if a character is uppercase

#include <stdio.h>

int isUppercase(char ch) {

return (ch >= 'A' && ch <= 'Z');

int main() {

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

if (isUppercase(ch))

printf("The character is uppercase.\n");

else

printf("The character is not uppercase.\n");

return 0;

You might also like