Tuesday, December 16, 2014

Replace it with the Specified Word



Search a Word & Replace it with the Specified Word
#include 
#include 
#include 
#include 
char *rep_str(const char *s, const char *old, const char *new1)
{
    char *ret;
    int i, count = 0;
    int newlen = strlen(new1);
    int oldlen = strlen(old);
 
    for (i = 0; s[i] != '\0'; i++)    
    {
        if (strstr(&s[i], old) == &s[i]) 
        {
            count++;
            i += oldlen - 1;
        }
    }
    ret = (char *)malloc(i + count * (newlen - oldlen));
    if (ret == NULL)
        exit(EXIT_FAILURE);
    i = 0;
    while (*s)
    {
        if (strstr(s, old) == s)
        {
            strcpy(&ret[i], new1);
            i += newlen;
            s += oldlen;
        }
        else
        ret[i++] = *s++;
    }
    ret[i] = '\0';
    return ret;
}
 
int main(void)
{
    char mystr[100], c[10], d[10];
    printf("Enter a string along with characters to be rep_strd:\n");
    gets(mystr);
    printf("Enter the character to be rep_strd:\n");
    scanf(" %s",c);
    printf("Enter the new character:\n");
    scanf(" %s",d);
    char *newstr = NULL;
 
    puts(mystr);
    newstr = rep_str(mystr, c,d);
    printf("%s\n", newstr);
    free(newstr);
    return 0;
getch();
}
 
 
 
Output
 
Enter a string along with characters to be rep_strd:
prrrogram C prrrogramming
Enter the character to be rep_strd:
rr
Enter the new character:
mmm
prrrogram C prrrogramming
pmmmrogram C pmmmrogramming
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
!

Count Number of Words in a given Text Or Sentence
#include 
#include 
#include 
void main()
{
    char s[200];
    int count = 0, i;
 
    printf("enter the string\n");
    scanf("%[^\n]s", s);
    for (i = 0;s[i] != '\0';i++)
    {
        if (s[i] == ' ')
            count++;    
    }
    printf("number of words in given string are: %d\n", count + 1);
    getch();
}

Output
 
enter the string
welcome to sanfoundry's c-programming class!
number of words in given string are: 5
Find the Frequency of Substring in the given String
#include 
#include 
#include 
 
 
void main()
{
    int count = 0, i, j = 0, k;
    char str[100], str1[20];
    clrscr();
    printf("Enter the string\n");
    scanf(" %[^\n]s", str);
    printf("Enter the substring to be matched\n");
    scanf(" %[^\n]s", str1);
    k = strlen(str1);
    for (i = 0;str[i] != '\0';)
    {
        if (str[i] == ' ')
        {
            i++;
        }
        else
        {
            if (str[i] == str1[j])
            {
                j++;
                i++;
            }
            else if (j == k)
            {
                j = 0;
                count++;
                i--;
            }
            else
            {
                i++;
                j = 0;
            }
        }
    }
    printf("No of matches of substring in main string is %d\n", count);
getch();
}
 
Output
Enter the string
prrrogram is prrrogramming
Enter the substring to be matched
rr
No of matches of substring in main string is 4

No comments:

Post a Comment