Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

String Based Solved Programs

The document provides code examples for string manipulation functions in C including finding character frequency, counting vowels/consonants, calculating string length, concatenating strings, copying strings, removing non-alphabetic characters, sorting strings lexicographically, and removing a given word from a string.

Uploaded by

vickroid007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

String Based Solved Programs

The document provides code examples for string manipulation functions in C including finding character frequency, counting vowels/consonants, calculating string length, concatenating strings, copying strings, removing non-alphabetic characters, sorting strings lexicographically, and removing a given word from a string.

Uploaded by

vickroid007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

String Programs In C

 C Program to Find the Frequency of Characters in a String

#include <stdio.h>
int main()
{
char c[1000],ch;
int i,count=0;
printf("Enter a string: ");
gets(c);
printf("Enter a character to find frequency: ");
scanf("%c",&ch);
for(i=0;c[i]!='\0';++i)
{
if(ch==c[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
 C Program to Find the Number of Vowels, Consonants, Digits and White space in a String

#include<stdio.h>
int main(){
char line[150];
int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
printf("Enter a line of string:\n");
gets(line);
for(i=0;line[i]!='\0';++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
else if (line[i]==' ')
++s;
}
printf("Vowels: %d",v);
printf("\nConsonants: %d",c);
printf("\nDigits: %d",d);
printf("\nWhite spaces: %d",s);
return 0;
}
 Source Code to Calculated Length without Using strlen() Function

#include <stdio.h>
int main()
{
char s[1000],i;
printf("Enter a string: ");
scanf("%s",s);
for(i=0; s[i]!='\0'; ++i);
printf("Length of string: %d",i);
return 0;
}
 Source Code to Concatenate Two Strings Manually

#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;
printf("Enter first string: ");
scanf("%s",s1);
printf("Enter second string: ");
scanf("%s",s2);
for(i=0; s1[i]!='\0'; ++i); /* i contains length of string s1. */
for(j=0; s2[j]!='\0'; ++j, ++i)
{
s1[i]=s2[j];
}
s1[i]='\0';
printf("After concatenation: %s",s1);
return 0;
}
 Source Code to Copy String Manually

#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
scanf("%s",s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("String s2: %s",s2);
return 0;
}
 Source Code to Remove Characters in String Except Alphabets

#include<stdio.h>
int main(){
char line[150];
int i,j;
printf("Enter a string: ");
gets(line);
for(i=0; line[i]!='\0'; ++i)
{
while (!((line[i]>='a'&&line[i]<='z') || (line[i]>='A'&&line[i]<='Z' || line[i]=='\0')))
{
for(j=i;line[j]!='\0';++j)
{
line[j]=line[j+1];
}
line[j]='\0';
}
}
printf("Output String: ");
puts(line);
return 0;
}
 Source Code to Sort Words in Dictionary Order

#include<stdio.h>
#include <string.h>
int main(){
int i,j;
char str[10][50],temp[50];
printf("Enter 10 words:\n");
for(i=0;i<10;++i)
gets(str[i]);
for(i=0;i<9;++i)
for(j=i+1;j<10 ;++j){
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("In lexicographical order: \n");
for(i=0;i<10;++i){
puts(str[i]);
}
return 0;
}
 Source Code to Remove given Word from a String

#include <stdio.h>
#include <string.h>
void main()
{
int i, j = 0, k = 0, count = 0;
char str[100], key[20];
char str1[10][20];

printf("enter string:");
scanf("%[^\n]s",str);

/* Converts the string into 2D array */


for (i = 0; str[i]!= '\0'; i++)
{
if (str[i]==' ')
{
str1[k][j] = '\0';
k++;
j = 0;
}
else
{
str1[k][j] = str[i];
j++;
}
}
str1[k][j] = '\0';
printf("enter key:");
scanf("%s", key);

/* Compares the string with given word */


for (i = 0;i < k + 1; i++)
{
if (strcmp(str1[i], key) == 0)
{
for (j = i; j < k + 1; j++)
strcpy(str1[j], str1[j + 1]);
k--;
}

}
for (i = 0;i < k + 1; i++)
{
printf("%s ", str1[i]);
}
}}

You might also like