Wednesday, December 17, 2014

Prime Position a given String in a c



Find First and Last Occurrence of given Character in a String
#include 
#include 
#include 
void main()
{
    int i, count = 0, pos1, pos2;
    char str[50], key, a[10];
    clrscr(); 
    printf("enter the string\n");
    scanf(" %[^\n]s", str);
    printf("enter character to be searched\n");
    scanf(" %c", &key);
    for (i = 0;i <= strlen(str);i++)
    {
        if (key == str[i])
        {
            count++;
            if (count  == 1)
            {
                pos1 = i;
                pos2 = i;
                printf("%d\n", pos1 + 1);
            }
            else 
            {
                pos2 = i;
            }
        }
    }
    printf("%d\n", pos2 + 1);
getch();
}

Output
 
enter the string
welcome to sanfoundry's c programming class!
enter character to be searched
m
6 
34
Display the Characters in Prime Position a given String
#include 
#include 
#include 
void main()
{
    int i, j, k, count = 0;
    char str[50];
    clrscr();
    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]);
        }
    }
}
Output
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
Find All Possible Subsets of given Length in String
#include 
#include 
#include 
 
char a[20];
int n, len, j;
 
void main()
{
    int i, index = 0, start = 0;
    clrscr();
    printf("Enter the string\n");
    scanf("%s", a);
    n = strlen(a);
    printf("enter input length\n");
    scanf("%d", &len);
    printf("The subsets are\n");
    for (i  =  1;i < = n;i++)
    {
        if (index - start + 1  ==   i)
        {
            if (i  ==   len)
            {
                for (j  =  index;j < n;j++)
                {
                    for (i  =  start;i < index;i++)
                        printf("%c", a[i]);
                    printf("%c\n", a[j]);
                }
                if (start != i)
                {
                    start++;
                    index = start;
                }
            }
            else
            {
                index++;
            }
        }
    }
 getch();
}
 
Output
 
Enter the string
programming
enter input length
2
The subsets are
pr
po
pg
pr
pa
pm
pm
pi
pn
pg
enter the string
programming
enter input length
4
The subsets are
prog
pror
proa
prom
prom
proi
pron
prog
Delete All Repeated Words in String
#include 
#include 
#include 
void main()
{
    char a[100], b[20][20];
    int i, j = 0, k = 0, n, m;
 
    printf("enter the string\n");
    scanf("%[^\n]s", a);
    for (i = 0;a[i] != '\0';i++)
    {
        if (a[i] == ' ')
        {
            b[k][j] = '\0';
            k++;
            j = 0;
        }
        else
        {
            b[k][j] = a[i];
            j++;
        }
    }
    b[k][j] = '\0';
    for (i = 0;i <= k;i++)
    {
        for (j = i + 1;j <= k;j++)
        {
            if (strcmp(b[i], b[j]) == 0)
            {
                for (m = j;m <= k;m++)
                    strcpy(b[m], b[m + 1]);
                k--;
            }
        }
    }
    for (n = 0;n <= k;n++)
    {
        printf("%s\n", b[n]);
    }
 getch();
}

Output
 
enter the string
welcome to sanfoundry's c programming class ,  welcome again to c class !
welcome
to
sanfoundry's
c
programming
class
, 
again
!

No comments:

Post a Comment