Showing posts with label Module 1. Show all posts
Showing posts with label Module 1. Show all posts

Sunday 14 August 2016

Strings

Module  1: Strings
  • Printing individual characters of strings
  • String concatenation using library function
  • String concatenation without using library function  
  • String comparison using library function
  • String comparison without using library function
  • Array of strings







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

Sparse Matrix: Fast Transpose

Sparse Matrix: Fast Transpose

#include<stdio.h>
#include<stdlib.h>
#define MAX 100

typedef struct
{
            int row;
            int col;
            int val;
}sparse;


void readsparse(sparse a[], int m, int n)
{
            int i, j, k, item, p;
            a[0].row = m;
            a[0].col = n;
            k = 1;

            printf("\nEnter the elements:\n");
            for(i=0; i<m; i++)
            {
                        for(j=0; j<n; j++)
                        {
                                    scanf("%d",&item);
                                    if(item == 0)
                                                continue;
                                    a[k].row = i;
                                    a[k].col = j;
                                    a[k].val = item;
                                    k++;
                        }
            }
            a[0].val = k-1;

            printf("\nThe entered sparse matrix is:\n");
            printf("\nRow\tColumn\tValue\n");
            for(p=0;  p<=a[0].val;  p++)
            {
                        printf("%d\t", a[p].row);
                        printf("%d\t", a[p].col);
                        printf("%d\n", a[p].val);
            }
}

void fast_transpose(sparse a[],  sparse b[])
{
            int row_terms[100],  start_pos[100];
            int i,  j,  p;       

            int numTerms = a[0].val;
            int numCols = a[0].col;
             
            b[0].row = numCols;
            b[0].col = a[0].row;
            b[0].val = numTerms;
            if(numTerms>0)
            {
                        for(i =0; i<numCols;  i++)
                                                row_terms[i] = 0;

                        for(i=1; i<=numTerms; i++)
                                                row_terms[a[i].col]++;

                        start_pos[0]=1;

                        for(i=1; i<numCols; i++)
                                                start_pos[i] = start_pos[i-1] + row_terms[i-1];

                        for(i=1; i<=numTerms; i++)
                        {
                                                j = start_pos[a[i].col]++;
                                    b[j].row = a[i].col;
                                                b[j].col = a[i].row;
                                                b[j].val = a[i].val;
                        }
             }
            printf("\nThe Fast Transpose sparse matrix is:\n");
            printf("\nRow\tColumn\tValue\n");
            for(p=0; p<=a[0].val; p++)
            {
                        printf("%d\t", b[p].row);
                        printf("%d\t", b[p].col);
                        printf("%d\n", b[p].val);
            }
}
void main()
{
            int m, n, key;
            sparse a[MAX], b[MAX];
            printf("\nEnter  the no of rows and columns:\t");
            scanf("%d%d",&m, &n);

            readsparse(a, m, n);
             fast_transpose(a, b);
}

Output:
Enter  the no of rows and columns:      6 6


Enter the elements:
15   0   0   22   0   -15
0   11   3   0    0   0
0   0   0   -6   0   0
0   0    0    0   0   0
91  0   0   0   0   0
0   0   28  0   0   0

The entered sparse matrix is:

Row     Column  Value
6       6       8
0       0       15
0       3       22
0       5       -15
1       1       11
1       2       3
2       4       -6
4       1       91
5       3       28

The Fast Transpose sparse matrix is:

Row    Column           Value
6          6                      8
0          0                      15
1          1                      11
1          4                      91
2          1                      3
3          0                      22
3          5                      28
4          2                      -6
5          0                      -15

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


Copy one string to another without using library function strcpy

Copy one string to another without using library function strcpy


#include<stdio.h>
main()
{
                char s1[10], s2[10];
                int n, i = 0;
                printf("\nEnter string 2: ");
                gets(s2);

                while(s2[i] != 0)
                {
                                s1[i] = s2[i];
                                i++;
                }
                s1[i] = '\0';

                printf("\n String 1 after copying: ");
                puts(s1);
}

Output:
Enter string 2: Hello Hii
String 1 after copying: Hello Hii

Reversing the string without using Library function

Reversing the string without using Library function


#include<stdio.h>
#include<string.h>
void main()
{
                char str[20], rev[20], temp;
                int i=0, j;
                printf("\nEnter the string: ");
                gets(str);

                j = strlen(str) - 1;
                while(i<j)
                {
                                temp = str[j];
                                str[j] = str[i];
                                str[i] = temp;
                                i++;
                                j--;
                }

                printf("\nReversed string: ");
                puts(str);
}

Output:
Enter the string:   newspaper
Reversed string:   repapswen

Also check,
https://tejaswinihbhat.blogspot.in/2016/08/strrev-reversing-string-using-library.html

strrev: Reversing the string using library function

String: strrev(string)

Reversing the string using library function


#include<stdio.h>          
#include<string.h>
main()
{
                char s[60];
                printf("\nEnter string:\t");
                gets(s);
                printf("\nReversed string is:  %s", strrev(s));
}
Output:
Enter string:   Hello How Are You
Reversed string is:  uoY erA woH olleH

strncpy: Copy specified number of characters from one string to another string using library function

String: strncpy(dest, source, n)


Copy specified number of characters from one string to another string using library function


#include<string.h>
main()
{
                char s[60], d[60]="\0";
                printf("\nSource string:\t");
                gets(s);
                strncpy(d, s, 5);
                printf("\nDestination string: %s", d);
}
Output:
Source string:  Hii How Are you

Destination string: Hii H

strcpy: Copy one string to another string using library function

String: strcpy(dest, source)

Copy one string to another string using library function


#include<stdio.h>
#include<string.h>
main()
{
                char s[60], d[60]="\0";
                printf("\nSource string:\t");
                gets(s);
                strcpy(d, s);
                printf("\nDestination string: %s", d);
}
Output:
Source string:  Hii How Are you
Destination string: Hii How Are you

strupr: Convert from Lower case to Upper case using Library function

String: strupr(string)

Convert from Lower case to Upper case using Library function


#include<stdio.h>
#include<string.h>
main()
{
                char s[60];
                printf("\nEnter string:\t");
                gets(s);
                printf("\nString in uppercase: %s", strupr(s));
}

Output:
Enter string:   Hello How Are You
String in uppercase: HELLO HOW ARE YOU