Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Sunday, 14 August 2016

Delete all occurrences of pattern from a string

Delete all occurrences of pattern (pat) from a string (txt)


#include<stdio.h>
#include<string.h>
void main()
{
                char txt[30],  pat[30];
                int m, n;
                int i, j, found = 0;
                printf("\nEnter the main string:");
                gets(txt);
                printf("\nEnter the pattern to be searched: ");
                gets(pat);
                m = strlen(txt);
                n = strlen(pat);

                for(i=0;  i<=m-n;  i++)
                {
                                found  = 1;
                                for(j=0; j<n; j++)
                                {
                                                if(txt[i+j] != pat[j])
                                                {
                                                                found = 0;
                                                                break;
                                                }
                                }
                                if(found == 1)
                                {
                    /* If pattern is found then shift all characters to left and decrement the string length */
                                                for(j=i;  j<m -n; j++)
                                                {
                                                                txt[j] = txt[j+n];
                                                }
                                                txt[j] = '\0';
                                                m = m - n;
                                 }
                }
                if(found == 0)
                                printf("\nPattern not found");
                else
                {
                                printf("\nAfter deleting pattern the string is: ");
                                puts(txt);
                }
}

Output:
Enter the main string:    hello hii how hii
Enter the pattern to be searched:   hii
After deleting pattern the string is: hello    how


Enter the main string:    hello hii how hii
Enter the pattern to be searched: abc

Pattern not found

Friday, 12 August 2016

Find all occurrences of pattern in a string

Find all occurrences of pattern(pat) in a a string(txt)


#include<stdio.h>
#include<string.h>
void main()
{
                char txt[30],  pat[30];
                int m, n;
                int i, j, found = 0;
                printf("\nEnter the main string:");
                gets(txt);
                printf("\nEnter the pattern to be searched: ");
                gets(pat);

                m = strlen(txt);
                n = strlen(pat);

                for(i=0;  i<=m-n;  i++)
                {
                                found  = 1;
                                for(j=0; j<n; j++)
                                {
                                                if(txt[i+j] != pat[j])
                                                {
                                                                found = 0;
                                                                break;
                                                }
                                }
                                if(found == 1)
                                                printf("\n The pattern found at index:%d",  i);
                }
                if(found  ==  0)
                                printf("\n   Pattern not found");
}

Output:
Enter the main string:    hello hii how hii
Enter the pattern to be searched:    hii

The pattern found at index:  6
The pattern found at index:   14

Extract a substring from a string from specified position

Extract a substring (substr) of length n from a string (str) from specified position (pos)


#include<stdio.h>
void main()
{
                char str[20], substr[20];
                int n, pos, i, j=0;

                printf("\nEnter the string:");
                gets(str);
                printf("\nEnter the position:");
                scanf("%d", &pos);
                printf("\nEnter the length of substring:");
                scanf("%d", &n);

                i = pos;
                while(str[i] != '\0'  &&  n>0)
                {
                                substr[j] = str[i];
                                i++;
                                j++;
                                n--;
                }
                substr[j] = '\0';

                printf("\nThe substring is:");
                puts(substr);

}

Output:
Enter the string:               hello hii abc
Enter the position:          2
Enter the length of substring:     5
The substring is:               llo h

Insert a substring to a string at specified position

Insert a substring (str) to a string (txt) at specified position (pos)


#include<stdio.h>
#include<string.h>

void main()
{
          char txt[30], str[30];
          int pos, i, k=0,m,n;
          printf("\nEnter the text: ");
          gets(txt);
          printf("\nEnter the string to be inserted: ");
          gets(str);
          printf("\nEnter the position where to be inserted: ");
          scanf("%d", &pos);
          m = strlen(txt);
          n = strlen(str);

          for(i=m-1; i>=pos; i--)
          {
                    txt[i+n] = txt[i];
          }
           i = pos;
           while(str[k] != '\0')
          {
                  txt[i] = str[k];
                   i++;
                   k++;
           }
           m = m+n;
           txt[m] = '\0';

           printf("\nThe new string is: ");
           puts(txt);
}

Output:
Enter the text: abcdefghij
Enter the string to be inserted: yyyy
Enter the position where to be inserted: 2
The new string is:  abyyyycdefghij

---------------------------------------------------------------------------------------------------
/*Method 2*/

#include<stdio.h>
void main()
{
                char txt[30],  str[30], new[40];
                int pos,  j=0, i=0, k=0;

                printf("\nEnter the text: ");
                gets(txt);
                printf("\nEnter the string to be inserted: ");
                gets(str);
                printf("\nEnter the position where to be inserted: ");
                scanf("%d", &pos);

                while(txt[i] != '\0' )
                {
                                if(i == pos)
                                {
                                                while(str[k] != '\0')
                                                {
                                                                new[j] = str[k];
                                                                j++;
                                                                k++;
                                                }
                                }
                                new[j] = txt[i];
                                j++;
                                i++;
                }
                new[j] = '\0';

                printf("\nThe new string is: ");
                puts(new);
}

Output:
Enter the text:                                            how are you
Enter the string to be inserted:                  abcde
Enter the position where to be inserted:    1

The new string is:                                      habcdeow are you