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

C Programming String Example Programs

This program takes 10 words as input from the user and sorts them in lexicographical or dictionary order. It uses a two dimensional character array to store the words and the strcmp() and strcpy() string functions to compare and swap words during the sorting process. The words are then printed out in alphabetical order.

Uploaded by

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

C Programming String Example Programs

This program takes 10 words as input from the user and sorts them in lexicographical or dictionary order. It uses a two dimensional character array to store the words and the strcmp() and strcpy() string functions to compare and swap words during the sorting process. The words are then printed out in alphabetical order.

Uploaded by

John Mark Carpio
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

C Program to Find the Frequency of

Characters in a String
This program asks user to enter a string and a character and this program checks how
many times that character is repeated in the string entered by user.

Source Code to Find the Frequency of Characters


#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;
}
Output

Enter a string: This website is awesome.

Enter a character to find frequency: e

Frequency of e = 4
C Program to Find the Number of Vowels,
Consonants, Digits and White space in a
String
This program takes a string from user and finds the total number of vowels, consonants,
digits and white space present in that string.

Source Code to Find Number of Vowels, Consonants,


Digits and White Space Character
#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;
}
Output

Enter a line of string:

This program is easy 2 understand

Vowels: 9

Consonants: 18

Digits: 1

White spaces: 5
C Program to Find the Length of a String
You can use standard library function strlen()  to find the length of a string but, this program
computes the length of a string manually without using strlen() funtion.

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;
}
Output

Enter a string: Programiz

Length of string: 9

This program asks user to enter a string and computes the length of string manually
using for loop .
C Program to Concatenate Two Strings
You can concatenate two strings easily using standard library function  strcat()  but, this
program concatenates two strings manually without using   strcat()  function.

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;
}
Output

Enter first string: lol

Enter second string: :)

After concatenation: lol:)


C Program to Copy String Without Using
strcpy()
You can use the strcpy() function  to copy the content of one string to another but, this
program copies the content of one string to another manually without
using  strcpy()  function.

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;
}
Output

Enter String s1: programiz

String s2: programiz

This above program copies the content of string s1 to string s2 manually.


C Program to Remove all Characters in a
String Except Alphabet
This program takes a strings from user and removes all characters in that string except
alphabets.

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;
}
Output

Enter a string: p2'r"o@gram84iz./

Output String: programiz

This program takes a string from user and for loop executed until all characters of string is
checked. If any character inside a string is not a alphabet, all characters after it including
null character is shifted by 1 position backwards.
C Program to Sort Elements in
Lexicographical Order (Dictionary Order)
This program takes 10 words from user and sorts elements in lexicographical order. To
perform this task, two dimensional string is used.

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;
}
Output

Enter 10 words:

fortran

java

perl

python

php

javascript
c

cpp

ruby

csharp

In lexicographical order:

cpp

csharp

fortran

java

javascript

perl

php

python

ruby

You might also like