Showing posts with label Strings. Show all posts
Showing posts with label Strings. Show all posts

Monday 15 August 2016

Delete the substring of length n from the string at specified position (pos)

Delete the substring of length n from the string at specified position (pos)

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

void main()
{
          char str[20], substr[20];
          int n, pos, i, m;
          printf("\nEnter the string:");
          gets(str);
          printf("\nEnter the position:");
          scanf("%d", &pos);
          printf("\nEnter the number of characters to be deleted:");
          scanf("%d", &n);
          m = strlen(str);

          for(i=pos;i<m-n;i++)
          { 
              str[i] = str[i+n];
          }
          str[i] ='\0';
          printf("\nThe substring is:");
          puts(str);
}

Output:
Enter the string:         abcdefghijkl
Enter the position:     3
Enter the number of characters to be deleted:  5
The substring is:     abcijkl


Friday 12 August 2016

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

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

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

String: strlwr(string)

Convert from Upper case to Lower case using Library function


#include<stdio.h>
#include<string.h>
main()
{
                char s[60];
                printf("\nEnter string:\t");
                gets(s);
                printf("\nString in lowercase: %s", strlwr(s));
}
Output:
Enter string:   Hello How Are You
String in lowercase:  hello  how are you

Thursday 11 August 2016

Length of the string without using library function

Length of the string without using library function

#include<stdio.h>
main()
{
                char s[20];
                int i = 0 ;
                printf("\n Enter a string: ");
                gets(s);

                while(s[i] != '\0')
                {
                                i++;
                }
                printf("\n Length of string: %d", i );
}
Output:
 Enter a string: Hello Hii
 Length of string: 9

strlen: Length of the string using library function

String: strlen(string)

Length of the string using library function


#include<stdio.h>          
#include<string.h>
main()
{
                char s[60];
                printf("\nEnter string:\t");
                gets(s);
                printf("\nLength of string is: %d", strlen(s));
}

Output:
Enter string:   Hello How Are You
Length of string is: 17

Unformatted Input and output functions for string


Unformatted Input and output functions for string


#include<stdio.h>
void main()
{
           char s[10];
           printf("\nEnter a string: ");
           gets(s);                      //unformatted input
           printf("\nEntered string is: ");
           puts(s);                    //unformatted output
}

Output:
Enter a string: How are you
Entered string is: How are you

Unformatted Input and output functions for character

Unformatted Input and output functions for character


#include<stdio.h>
void main()
{
              char c;
              printf("\nEnter a character: ");
              c = getchar();                    //unformatted input
              printf("\nEntered character is: ");
              putchar(c);                     //unformatted output
}

Output:
Enter a character: h
Entered character is: h

Formatted Input and output functions for string

Formatted Input and output functions for string


#include<stdio.h>
void main()
{
         char s[10];
         printf("\nEnter a string: ");
         scanf("%s", s);                  //formatted input

         printf("\nEntered string is: ");
         printf("%s", s);               //formatted output
}

Output:
Enter a string: How are you
Entered string is: How

Formatted Input and output functions for character

Formatted Input and output functions for character

#include<stdio.h>
void main()
{
          char ch;
          printf("\n Enter a character: ");
          scanf("%c", &ch);                          //formatted input
          printf("\n Entered character is: ");
          printf("%c", ch);                          //formatted output
}

Output:

1) Enter a character: h
    Entered character is: h

2) Enter a character: hii
    Entered character is: h

Monday 8 August 2016

Lab Program 2 String 15CSL38 Data Structures in C Lab

Lab Program 2:


Design, Develop and Implement a Program in C for the following operations on Strings
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if PAT exists in STR.
c. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in functions.

#include<stdio.h>
char str[50], pat[20], rep[20], ans[50];
int     c=0, m=0, i=0, j=0, k, flag=0;
void stringmatch()
{
                 while(str[c] !='\0')
                {
                                if(str[m] == pat[i])
                                {
                                                i++;
                                                m++;
                                                if(pat[i] == '\0')
                                                {             
                                                                flag = 1;
                                                                for(k=0; rep[k]!='\0'; k++, j++)
                                                                {
                                                                                ans[j] = rep[k];
                                                                }
                                                                i = 0;
                                                                c = m;
                                                }
                                }
                                else
                                {
                                                ans[j]= str[c];
                                                j++;
                                                c++;
                                                m=c;
                                                i=0;
                                }
                }
                ans[j]='\0';
}
void main()
{
                printf("\nEnter the main string:");
                gets(str);
                printf("\nEnter the pat string:");
                gets(pat);
                printf("\nEnter the replace string:");
                gets(rep);
                stringmatch();
                if(flag == 1)
                                printf("\nResultant string is %s", ans);
                else
                                printf("\nPattern string is not found");
}

Enter the main string:    hello aabhii howaab
Enter the pat string:        aab
Enter the replace string:                               klmno

Resultant string is: hello klmnohii howklmno





Method 2:

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

char str[50], pat[50], rep[50];
int start = 0, patfound = 0;
int lasts, lastp, lastr;

void replacepattern()
{
            int i, j;
            lastr = strlen(rep)-2;
           
            if(lastp != lastr)
            {
                        printf("\nInvalid length of replace string");
                        exit(0);
            }
            else
            {
                        i = start;
                        for(j=0; j<=lastr; j++)
                        {
                                    str[i] = rep[j];
                                    i++;
                        }
            }
            return;
}

void findpattern()
{
            int i, j, inmatch;
            lasts = (strlen(str))-2;
            lastp = (strlen(pat))-2;
            int endmatch;

            for(endmatch = lastp; endmatch<=lasts; endmatch++, start++)
            {
                        if(str[endmatch] == pat[lastp])
                        {
                                    inmatch = start;
                                     j=0;
                                    while(j<lastp)
                                    {
                                                if(str[inmatch] == pat[j])
                                                {
                                                            inmatch++;
                                                            j++;
                                                }
                                                else
                                                {
                                                            break;
                                                }
                                    }
                                    if(j == lastp)
                                    {
                                                patfound = 1;
                                                replacepattern();
                                    }
                        }
            }
            return;
}

void main()
{
            printf("\nEnter the main string(STR): ");
            fgets(str, 50, stdin);

            printf("\nEnter the pattern to be matched(PAT): ");
            fgets(pat, 50, stdin);

            printf("\nEnter the string to be replaced(REP): ");
            fgets(rep, 50, stdin);

            printf("\nThe string before pattern match is:\n %s", str);

            findpattern();

            if(patfound == 0)
                        printf("\nThe pattern is not found in the main string");
            else
                        printf("\nThe string after pattern match and replace is: \n %s ",str);
            return;
}


 Output:


Case 1:

Enter the main string(STR):    Hello hii how are you hii

Enter the pattern to be matched(PAT): hii

Enter the string to be replaced(REP): xyz

The string before pattern match is:
 Hello hii how are you hii

The string after pattern match and replace is:
 Hello xyz how are you xyz




Case 2:

Enter the main string(STR): Hello hii how are you

Enter the pattern to be matched(PAT): abc

Enter the string to be replaced(REP): xyz

The string before pattern match is:
 Hello hii how are you

The pattern is not found in the main string



Also Credits to: 
Manoj Taleka  (manoj89biet@gmail.com)
Yashaswini Jogi (jogi.yash@gmail.com)