This is a C Program to display the characters in prime position of a given string.
This program prints the characters in prime position of a given string.
1. Take a string as input.
2. Find the number which gets divided only once and consecutively print the character of the obtained position.
Here is source code of the C Program to display the characters in prime position a given string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Display the Characters in Prime Position a given String
*/
#include <stdio.h>
#include <string.h>
void main()
{
int i, j, k, count = 0;
char str[50];
printf("enter string\n");
scanf("%[^\n]s", str);
k = strlen(str);
printf("prime characters in a string are\n");
for (i = 2;i <= k;i++)
{
count = 0;
for (j = 2;j <= k;j++)
{
if (i % j == 0)
{
count++;
}
}
if (count == 1)
{
printf("%c\n", str[i - 1]);
}
}
}
1. Take a string as input and store it in the array str[].
2. Store the length of the input string in the variable k.
3. Use two loops to divide the numbers upto the value of k.
4. Increment the variable count when the remainder is zero.
5. If the variable count is equal to 1, then print the corresponding character.
enter string welcome to sanfoundry c-programming class! prime characters in a string are e l o e a u d c r m c s
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Check C Books
- Apply for Computer Science Internship
- Apply for C Internship
- Check Computer Science Books
- Practice Computer Science MCQs