0% found this document useful (0 votes)
39 views25 pages

Stings Coding Questions

The document contains a collection of C programming exercises focused on string manipulation. It includes various programs that demonstrate reading strings, calculating string length, separating characters, counting words, comparing strings, and extracting substrings, among other tasks. Each program is provided with code snippets and explanations of their functionality.

Uploaded by

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

Stings Coding Questions

The document contains a collection of C programming exercises focused on string manipulation. It includes various programs that demonstrate reading strings, calculating string length, separating characters, counting words, comparing strings, and extracting substrings, among other tasks. Each program is provided with code snippets and explanations of their functionality.

Uploaded by

22p61a05g7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Strings Coding Questions in

C Programming

By
P Penchala Swamy
Write a program to show the difference between
scanf(), gets() fuctions to perform read/write
operations
#include <stdio.h>
void main()
{
char a[100];
printf("Enter string\n");
scanf("%s",a);
printf("String is %s\n",a);

printf("Enter string\n");
gets(a);
//fgets(a, sizeof a, stdin);
printf("output string is %s\n",a);
puts(a);
Write a program in C to find the length of a string
without using library functions
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[100];
int l = 0;
printf("enter Input the string : ");
fgets(str, sizeof str, stdin);
while (str[l] != '\0') {
l++;
}
printf("Length of the string is : %d\n\n", l - 1);
return 0;
}
Write a program in C to separate individual
characters from a string
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[100];
int l = 0;
printf("enter Input the string : ");
fgets(str, sizeof str, stdin);
while (str[l] != '\0') {
printf("%c ", str[l]);
l++;
}
printf("Length of the string is : %d\n\n", l - 1);
return 0;
Write a program to print individual characters of
a string in reverse order.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str[100];
int l, i;
printf("enter input the string : ");
fgets(str, sizeof str, stdin);
l = strlen(str);
printf("The characters of the string in reverse are : \n");
for (i = l - 1; i >= 0; i--) {
printf("%c ", str[i]);
}
printf("\n");
}
Write a program in C to count the total number
of words in a string
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str[100];
int i, wrd;
printf("enter input the string : ");
fgets(str, sizeof str, stdin);
i = 0;
wrd = 1;
while (str[i] != '\0') {
if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t') {
wrd++;
}
i++;
}
printf("Total number of words in the string is : %d\n", wrd-1);
return 0;
Write a program in C to compare two strings
without using string library functions
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i = 0;
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
while (str1[i] == str2[i] && str1[i] != '\0') {
i++;
}
if (str1[i] > str2[i]) {
printf("String 1 comes after String 2.\n");
} else if (str1[i] < str2[i]) {
printf("String 1 comes before String 2.\n");
} else {
printf("Both strings are the same.\n");
}
return 0;
}
Write a program to count the total number of
alphabets, digits and special characters in a string.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str[100];
int alp, digit, splch, i;
alp = digit = splch = i = 0;
printf("Input the string : ");
fgets(str, sizeof str, stdin);
while (str[i] != '\0') {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
alp++;
} else if (str[i] >= '0' && str[i] <= '9') {
digit++;
} else {
splch++;
}
i++;
}
Write a program in C to copy one string to
another string
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str1[100], str2[100];
int i;
printf("Input the string : ");
fgets(str1, sizeof str1, stdin);
i = 0;
while (str1[i] != '\0') {
str2[i] = str1[i];
i++;
}
str2[i] ='\0';
printf("\nThe First string is : %s\n", str1);
printf("The Second string is : %s\n", str2);
printf("Number of characters copied : %d\n\n", i);
return 0;
}
Write a program in C to count the total number
of vowels or consonants in a string
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str[100];
int i, len, vowel, cons;
printf("Input the string : ");
fgets(str, sizeof str, stdin);
vowel = 0;
cons = 0;
len = strlen(str);
for (i = 0; i < len; i++) {
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++;
}
else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
cons++;
}
}
Write a program in C to find Character frequency
in a string
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for (int i = 0; str[i] != '\0'; ++i) {
if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
Write a C program to sort a string array in
ascending order.
#include <stdio.h>
#include <string.h>
int main() {
char str[100], ch;
int i, j, l;
printf("Input the string : ");
fgets(str, sizeof str, stdin);
l = strlen(str);
for (i = 1; i < l; i++) {
for (j = 0; j < l - i; j++) {
if (str[j] > str[j + 1]) {
ch = str[j];
str[j] = str[j + 1];
str[j + 1] = ch;
}
}
}
printf("After sorting the string appears like : \n");
printf("%s\n\n", str);
return 0;
Write a program in C to extract a substring from
a given string.
#include <stdio.h>
int main() {
char str[100], sstr[100];
int pos, l, c = 0;
printf("Input the string : ");
fgets(str, sizeof str, stdin);
printf("Input the position to start extraction :");
scanf("%d", &pos);
printf("Input the length of substring :");
scanf("%d", &l);
while (c < l) {
sstr[c] = str[pos + c - 1];
c++;
}
sstr[c] = '\0';
printf("The substring retrieved from the string is : \" %s\" \n\n", sstr);
return 0;
}

You might also like