In the next few posts we will work on C array – char type. We will see Different type of programs on C array.
C program on array to search a specific character from it
Take a string and a special character from the user and display whether the character is available in the string. If available then how many times ?
#include< stdio.h >
#include< string.h >
void main()
{
char ch[20],c;
int i=0,counter=0;
clrscr();
puts("Enter the string:-");
gets(ch);
puts("Enter the character:-");
scanf("%c",&c);
for(i=0;i< strlen(ch);i++)
{
if(c==ch[i])
counter++;
}
if(counter!=0)
printf("The character '%c' occurs %d times in the string.",c,counter);
else
printf("The character '%c' is not available in the string.",c);
getch();
}
C program to display a string in reverse order
#include< stdio.h >
#include< conio.h >
void main()
{
char str1[100],str2[100];
int i=0,n=0,j;
clrscr();
printf("\nEnter the string::");
do{
str1[i]=getchar();
i++;
}while(str1[i-1]!='\n');
str1[i-1]='\0';
printf("\nThe string is \t%s",str1);
printf("\nNow the string will be displayed in reversed order\n");
for(j=i-2;j >=0;j--)
{
str2[n]=str1[j];
n++;
}
str2[n]='\0';
printf("\nReversed string\t%s",str2);
getch();
}
No comments:
Post a Comment