Below is a C program which allows a user to enter a maximum of 10 characters. The program prints the count of how many times a given character occurs in the array.
/*Program to print how many times a given character occurs in an array*/
#include<stdio.h>
#include<conio.h>
void main()
{
char arr[10],ch,i,num,flag=0,count=0;
printf("How many characters you want to enter:");
scanf("%d",&num);
if(num>10)
{
printf("Size cannot be greater than 10.Try again...!");
}
else
{
/*Reading the array elements*/
printf("Enter the array elements:\n");
for(i=0;i<num;i++)
{
fflush(stdin);
scanf("%c",&arr[i]);
}
/*Reading the character to search in the array*/
printf("Enter the character you want to search:");
fflush(stdin);
scanf("%c",&ch);
/*Code to check if the character is available in the array or not*/
for(i=0;i<num;i++)
{
if(arr[i]==ch)
{
flag = 1;
count++;
}
}
if(flag==0)
printf("Given character is not available in the array.");
else
printf("Given character '%c' occurs %d times in the array",ch,count);
}
getch();
}
Sample input and output:
How many characters you want to enter:6
Enter the array elements:
a
b
c
a
d
a
Enter the character you want to search:a
Given character 'a' occurs 3 times in the array
Example programs in various programming languages like c, java, c# etc and web development languages like html, javascript, jquery and more.
Welcome...
Monday, 2 December 2013
C program to search for a given character in an array
Below is a C program which allows an user to enter a maximum of 10 characters . It also allows an user to search whether a given character is
available in the array or not. If the given character is available, the program displays
the index within the array at which the character is found.
/*Program to search for a given character in an array*/
#include<stdio.h>
#include<conio.h>
void main()
{
char arr[10],ch,i,num,flag=0;
printf("How many characters you want to enter:");
scanf("%d",&num);
if(num>10)
{
printf("Size cannot be greater than 10.Try again...!");
}
else
{
/*Reading the array elements*/
printf("Enter the array elements:\n");
for(i=0;i<num;i++)
{
fflush(stdin);
scanf("%c",&arr[i]);
}
/*Reading the character to search in the array*/
printf("Enter the character you want to search:");
fflush(stdin);
scanf("%c",&ch);
/*Code to check if the character is available in the array or not*/
for(i=0;i<num;i++)
{
if(arr[i]==ch)
{
printf("%c is found at index %d\n",ch,i);
flag = 1;
}
}
if(flag==0)
printf("Given character is not available in the array.");
}
getch();
}
Sample input and output:
How many characters you want to enter:5
Enter the array elements:
a
b
c
d
e
Enter the character you want to search:b
b is found at index 1
/*Program to search for a given character in an array*/
#include<stdio.h>
#include<conio.h>
void main()
{
char arr[10],ch,i,num,flag=0;
printf("How many characters you want to enter:");
scanf("%d",&num);
if(num>10)
{
printf("Size cannot be greater than 10.Try again...!");
}
else
{
/*Reading the array elements*/
printf("Enter the array elements:\n");
for(i=0;i<num;i++)
{
fflush(stdin);
scanf("%c",&arr[i]);
}
/*Reading the character to search in the array*/
printf("Enter the character you want to search:");
fflush(stdin);
scanf("%c",&ch);
/*Code to check if the character is available in the array or not*/
for(i=0;i<num;i++)
{
if(arr[i]==ch)
{
printf("%c is found at index %d\n",ch,i);
flag = 1;
}
}
if(flag==0)
printf("Given character is not available in the array.");
}
getch();
}
Sample input and output:
How many characters you want to enter:5
Enter the array elements:
a
b
c
d
e
Enter the character you want to search:b
b is found at index 1
C program to search for a given number in an array
Below is a C program which allows an user to enter a maximum of 10 numbers. It also allows an user to search whether a given number is available or not. If the given number is available, the program displays the index within the array at which the number is found.
/*Program to search for a given number in an array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],n,i,num,flag=0;
printf("How many numbers you want to enter:");
scanf("%d",&num);
if(num>10||num<1)
{
printf("Size cannot be greater than 10 and less than 1.Try again...!");
}
else
{
/*Reading the array elements*/
printf("Enter the array elements:\n");
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
/*Reading the number to search in the array*/
printf("Enter the number you want to search:");
scanf("%d",&n);
/*Code to check if the number is available in the array or not*/
for(i=0;i<num;i++)
{
if(arr[i]==n)
{
printf("%d is found at index %d\n",n,i);
flag = 1;
}
}
if(flag==0)
printf("Given number is not available in the array.");
}
getch();
}
Sample input and output:
How many numbers you want to enter:5
Enter the array elements:
1 2 3 4 5
Enter the number you want to search:3
3 is found at index 2
/*Program to search for a given number in an array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],n,i,num,flag=0;
printf("How many numbers you want to enter:");
scanf("%d",&num);
if(num>10||num<1)
{
printf("Size cannot be greater than 10 and less than 1.Try again...!");
}
else
{
/*Reading the array elements*/
printf("Enter the array elements:\n");
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
/*Reading the number to search in the array*/
printf("Enter the number you want to search:");
scanf("%d",&n);
/*Code to check if the number is available in the array or not*/
for(i=0;i<num;i++)
{
if(arr[i]==n)
{
printf("%d is found at index %d\n",n,i);
flag = 1;
}
}
if(flag==0)
printf("Given number is not available in the array.");
}
getch();
}
Sample input and output:
How many numbers you want to enter:5
Enter the array elements:
1 2 3 4 5
Enter the number you want to search:3
3 is found at index 2
C program to read integers into an array and print them back
Below is a C program which accepts a maximum of 10 numbers from the user, stores them in an array and prints them back.
/*Program to read n numbers into an array and print them*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],num,i;
printf("Enter the number of elements:");
scanf("%d",&num);
if(num>10)
{
printf("No of elements cannot be more than 10. Try again..!");
}
else
{
/*Reading array elements*/
for(i=0;i<num;i++)
scanf("%d",&a[i]);
/*Printing the array elements*/
printf("Elements in the array are:\n");
for(i=0;i<num;i++)
printf("%d ",a[i]);
}
getch();
}
Sample input and output:
Enter the number of elements:6
1 3 5 7 9 0
Elements in the array are:
1 3 5 7 9 0
/*Program to read n numbers into an array and print them*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],num,i;
printf("Enter the number of elements:");
scanf("%d",&num);
if(num>10)
{
printf("No of elements cannot be more than 10. Try again..!");
}
else
{
/*Reading array elements*/
for(i=0;i<num;i++)
scanf("%d",&a[i]);
/*Printing the array elements*/
printf("Elements in the array are:\n");
for(i=0;i<num;i++)
printf("%d ",a[i]);
}
getch();
}
Sample input and output:
Enter the number of elements:6
1 3 5 7 9 0
Elements in the array are:
1 3 5 7 9 0
C program to read characters into an array and print them
Below is a C program which accepts a maximum of 10 characters from the user, stores them in an array and prints the array back.
/*Program to read n characters into an array and print them*/
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10],num,i;
printf("Enter the number of elements:");
scanf("%d",&num);
if(num>10)
{
printf("No of elements cannot be more than 10. Try again..!");
}
else
{
/*Reading the array elements*/
printf("Enter the elements:\n");
for(i=0;i<num;i++)
{
fflush(stdin);
scanf("%c",&a[i]);
}
/*Printing the array elements*/
printf("Elements in the array are:\n");
for(i=0;i<num;i++)
printf("%c ",a[i]);
}
getch();
}
Sample input and output:
Enter the number of elements:5
Enter the elements:
a
f
u
w
k
Elements in the array are:
a f u w k
/*Program to read n characters into an array and print them*/
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10],num,i;
printf("Enter the number of elements:");
scanf("%d",&num);
if(num>10)
{
printf("No of elements cannot be more than 10. Try again..!");
}
else
{
/*Reading the array elements*/
printf("Enter the elements:\n");
for(i=0;i<num;i++)
{
fflush(stdin);
scanf("%c",&a[i]);
}
/*Printing the array elements*/
printf("Elements in the array are:\n");
for(i=0;i<num;i++)
printf("%c ",a[i]);
}
getch();
}
Sample input and output:
Enter the number of elements:5
Enter the elements:
a
f
u
w
k
Elements in the array are:
a f u w k
C program to reverse the elements in an array and print it
Below is C program which allows a user to enter a maximum of 10 numbers, then reverses the array using a temporary variable and finally prints the elements of the reversed array.
/*Program to reverse an array using a temporary variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],n,i,j,temp;
printf("Enter the number of elements:");
scanf("%d",&n);
if(n>10 || n<1)
{
printf("Invalid size.Try again...!");
}
else
{
/*Reading the array elements*/
printf("Enter the elements into the array:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
/*Reversing the elements in the array*/
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*Prinitng the array elements after reversing it*/
printf("After reversing, the array elements are:\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
}
getch();
}
Sample input and output:
Enter the number of elements:6
Enter the elements into the array:
1 2 3 4 5 6
After reversing, the array elements are:
6 5 4 3 2 1
/*Program to reverse an array using a temporary variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],n,i,j,temp;
printf("Enter the number of elements:");
scanf("%d",&n);
if(n>10 || n<1)
{
printf("Invalid size.Try again...!");
}
else
{
/*Reading the array elements*/
printf("Enter the elements into the array:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
/*Reversing the elements in the array*/
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*Prinitng the array elements after reversing it*/
printf("After reversing, the array elements are:\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
}
getch();
}
Sample input and output:
Enter the number of elements:6
Enter the elements into the array:
1 2 3 4 5 6
After reversing, the array elements are:
6 5 4 3 2 1
Subscribe to:
Comments (Atom)