Character Array(String) Programs
Program
-1 : Array of characters
//
compile time initialization
#include<conio.h>
#include<stdio.h>
void
main()
{
char
nm[]={ 's','h','i','v','\0'};
char
name[]="Parvati";
int i;
clrscr();
printf("\n Name : %s",nm);
printf("\n Name1 :
%s",name);
for(i=0;i<5;i++)
{
printf("\n %5c",nm[i]);
}
getch();
}
Program
-2 : Write a program to create triangle of given string
/*
character triangle
S
S h
S h i
S h i v
*/
#include<conio.h>
#include<stdio.h>
void
main()
{
char nm[20];
int i,j;
clrscr();
printf("\n Enter your name :
");
gets(nm);
for(i=0;nm[i]!=NULL;i++)
{
for(j=0;j<=i;j++)
{
printf("
%c",nm[j]);
}
printf("\n");
}
getch();
}
Program
-3 : Write a program to calculate length of String without using
any Library function.
#include<conio.h>
#include<stdio.h>
void
main()
{
char nm[100];
int i,l=0;
clrscr();
printf("\n Enter nay string :
");
gets(nm);
// find length
for(i=0;nm[i]!=NULL;i++)
{
l++;
}
printf("\n Length = %d",l);
// print each character
for(i=0;i<l;i++)
{
printf("\n
%c",nm[i]);
}
getch();
}
Program
-4 : Write a program to count no. of vowels and consonant from
given string.
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void
main()
{
char nm[20];
int i,v=0,c=0;
clrscr();
printf("\n Enter any string :
" );
gets(nm);
for(i=0;nm[i]!=NULL;i++)
{
if(isalpha(nm[i]))
{
if(nm[i]=='a' || nm[i]=='e' || nm[i]=='i' || nm[i]=='o' || nm[i]=='u'||
nm[i]=='A' || nm[i]=='E' || nm[i]=='I' || nm[i]=='O' || nm[i]=='U')
v++;
else
c++;
}
}
printf("\n Vowels = %d",v);
printf("\n Consonents =
%d",c);
getch();
}
Program
5 : Write a program to copy one string
to other string without using liberary function.
#include<conio.h>
#include<stdio.h>
void
main()
{
char s1[30],s2[30];
int l=0,i;
clrscr();
printf("\n Enter any string :
");
gets(s1);
while(s1[l]!='\0')
{
l++;
}
for(i=0;i<=l;i++)
{
s2[i]=s1[i];
}
s2[i+1]='\0';
printf("\n Copy = %s",s2);
getch();
}
Program-6 : Write a program to count total number of
words from given string and display each word in new line.
Ex : I
like C Language
I
Like
C
Language
Total words = 4
#include<conio.h>
#include<stdio.h>
void
main()
{
char s[100];
int l=0,i,c=1;
clrscr();
printf("\n Enter any string :
");
gets(s);
while(s[l]!='\0')
{
l++;
}
printf("\n");
for(i=0;i<l;i++)
{
printf("%c",s[i]);
if(s[i]==' '&&
s[i+1]!=' ')
{
c++;
printf("\n");
}
}
printf("\n
Total No. Of Words = %d",c);
getch();
}
Program-7 : Write a program to reverse given string.
Ex :
Original
string : Shiv
Reverse
String : vihS
#include<conio.h>
#include<stdio.h>
void
main()
{
char s1[30],s2[30];
int l=0,i,j=0;
clrscr();
printf("\n Enter any string :
");
gets(s1);
while(s1[l]!='\0')
{
l++;
}
printf("\n");
for(i=l-1;i>=0;i--)
{
s2[j]=s1[i];
j++;
}
s2[j]='\0';
printf("\n
Original String = %s",s1);
printf("\n
Reverse String = %s",s2);
getch();
}
Program-8 : Write a program to check whether given string
is palindrome or not.
Ex:
String
: nayan
This
is palindrome
#include<conio.h>
#include<stdio.h>
void
main()
{
char s1[30],s2[30];
int l=0,i,j=0,flag=0;
clrscr();
printf("\n Enter any string :
");
gets(s1);
while(s1[l]!='\0')
{
l++;
}
printf("\n");
for(i=l-1;i>=0;i--)
{
s2[j]=s1[i];
j++;
}
s2[j]='\0';
printf("\n Original String =
%s",s1);
printf("\n Reverse String = %s",s2);
// logic
to check palindrome string
for(i=0;i<l;i++)
{
if(s1[i]!=s2[i])
{
flag=1;
break;
}
}
if(flag==1)
printf("\n String is not
palindrome");
else
printf("\n String is palindrome");
getch();
}
Program-9 : Write a program to convert all lowercase
letter to uppercase letter of given
string.
#include<conio.h>
#include<stdio.h>
void
main()
{
char s[30];
int l=0,i,c=1;
clrscr();
printf("\n Enter any string :
");
gets(s);
while(s[l]!='\0')
{
l++;
}
printf("\n");
for(i=0;i<l;i++)
{
if(s[i]>='a' &&
s[i]<='z')
{
s[i]=s[i]-32;
}
}
printf("\n Name = %s",s);
getch();
}
Program-10 : Write a program to convert all uppercase
letter to lowercase
And lowercase letter to uppercase of the given
string.
#include<conio.h>
#include<stdio.h>
void
main()
{
char s[30];
int l=0,i,c=1;
clrscr();
printf("\fn Enter any string :
");
gets(s);
while(s[l]!='\0')
{
l++;
}
printf("\n");
for(i=0;i<l;i++)
{
if(s[i]>='a' &&
s[i]<='z')
{
s[i]=s[i]-32;
}
else if(s[i]>='A'
&& s[i]<='Z')
{
s[i]=s[i]+32;
}
else
{}
}
printf("\n Name = %s",s);
getch();
}
Posted By :Ruchita Pandya