0% found this document useful (0 votes)
59 views

Strings Toc

The document discusses a C program to find prefixes, suffixes, and substrings of a given string by removing leading and trailing characters, where prefixes are obtained by removing trailing characters, suffixes by removing leading characters, and substrings by removing both leading and trailing characters. It provides code to get the prefixes, suffixes, and substrings of a string entered by the user by calling functions for each that use loops and character indexing to print the results.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Strings Toc

The document discusses a C program to find prefixes, suffixes, and substrings of a given string by removing leading and trailing characters, where prefixes are obtained by removing trailing characters, suffixes by removing leading characters, and substrings by removing both leading and trailing characters. It provides code to get the prefixes, suffixes, and substrings of a string entered by the user by calling functions for each that use loops and character indexing to print the results.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab-1:

Write a program to find prefixes, suffixes and substring from given


string.

Strings:
Strings are denoted by lower case letters. String is a finite sequence of symbols taken
from some alphabet. e.g., 0110 is a string from binary alphabet, ―automata is a string
over alphabet {a, b, c ... z}.
Empty String: It is a string with zero occurrences of symbols. It is denoted by ‘ε’
(epsilon).

Suffix of a string:
A string s is called a suffix of a string w if it is obtained by removing 0 or more leading
symbols in w. For example;
w = abcd
s = bcd is suffix of w. Here s is proper suffix if s ≠ w.

Prefix of a string:
A string s is called a prefix of a string w if it is obtained by removing 0 or more trailing
symbols of w. For example;
w = abcd
s = abc is prefix of w,
Here, s is proper suffix i.e. s is proper suffix if s ≠ w.

Substring:
A string s is called substring of a string w if it is obtained by removing 0 or more leading
or trailing symbols in w. It is proper substring of w if s ≠ w.
If s is a string, then Substr (s, i, j) is substring of s beginning at i th & ending at jth position
both inclusive.
Source code:

#include <stdio.h>
#include <string.h>
int l, i, j, k;
char str[100];
void prefix(char s[], int n)
{
for (i = 0; i < n; i++)
{
for (j = 0; j <= i; j++)
{

printf("%c", s[j]);
}
printf("\n");
}
}
void suffix(char *s, int n)
{
for (i = 0; i < n; i++)
{
printf("%s\n", &s[i]);
}
}
void substring(char *s, int n)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n - i; j++)
{
for (k = j; k < j + i + 1; k++)
{
printf("%c", s[k]);
}
printf("\n");
}
}
}

int main()
{
int choice, flag = 1;
printf("Enter your string:\n");
scanf("%s", &str);
do
{
printf("\nEnter your choice");
printf("\n1: Prefix");
printf("\n2: Suffix");
printf("\n3: Substrings");
printf("\n4: Exit");
printf("\nEnter your choice number: \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nPrefix:\n");
prefix(str, strlen(str));
break;
case 2:
printf("\nSuffix:\n");
suffix(str, strlen(str));
break;
case 3:
printf("\nSubstring:\n");
substring(str, strlen(str));
break;
case 4:
flag = 0;
break;

default:
printf("\nInvalid Choice!");
}
} while (flag);
return 0;
}

Output:

You might also like