Check if one string is a subsequence of another string
Here given code implementation process.
//C Program
//Check if one string is a subsequence of another string
#include <stdio.h>
void is_subsequence(char first[],char second[],int s1,int s2)
{
int j=0;
for (int i = 0; i < s1 && j < s2 && s1 >= s2; ++i)
{
//Check that First string character are exist in second string
if(first[i] == second[j])
{
j++;
}
}
if(j == s2 )
{
printf("subsequence [%s] are exist \n",second);
}
else
{
printf("subsequence [%s] are not exist \n",second);
}
}
int main()
{
char text[]="infinitycodeset";
char str1[]="fitcost";
char str2[]="noxset";
char str3[]="finecode";
char str4[]="inset";
char str5[]="findset";
printf("%s\n",text );
//Get the size
int s1=sizeof(text)/sizeof(text[0])-1;
int s2=sizeof(str1)/sizeof(str1[0])-1;
//Test Case
is_subsequence(text,str1,s1,s2);
s2=sizeof(str2)/sizeof(str2[0])-1;
is_subsequence(text,str2,s1,s2);
s2=sizeof(str3)/sizeof(str3[0])-1;
is_subsequence(text,str3,s1,s2);
s2=sizeof(str4)/sizeof(str4[0])-1;
is_subsequence(text,str4,s1,s2);
s2=sizeof(str5)/sizeof(str5[0])-1;
is_subsequence(text,str5,s1,s2);
return 0;
}
Output
infinitycodeset
subsequence [fitcost] are exist
subsequence [noxset] are not exist
subsequence [finecode] are not exist
subsequence [inset] are exist
subsequence [findset] are exist
// Java program
// Check if one string is a subsequence of another string
public class MyString {
public void is_subsequence(String first, String second) {
int j = 0;
//Get the length of given string
int s1 = first.length();
int s2 = second.length();
for (int i = 0; i < s1 && j < s2 && s1 >= s2; ++i)
{
//Check that First string character are exist in second string
if (first.charAt(i) == second.charAt(j))
{
j++;
}
}
if (j == s2)
{
System.out.print("subsequence ["+second+"] are exist \n");
}
else
{
System.out.print("subsequence ["+second+"] are not exist \n");
}
}
public static void main(String[] args)
{
MyString obj = new MyString();
//Given string
String text = "infinitycodeset";
System.out.print(text+"\n");
//Test Case
obj.is_subsequence(text,"fitcost");
obj.is_subsequence(text,"noxset");
obj.is_subsequence(text,"finecode");
obj.is_subsequence(text,"inset");
obj.is_subsequence(text,"findset");
}
}
Output
infinitycodeset
subsequence [fitcost] are exist
subsequence [noxset] are not exist
subsequence [finecode] are not exist
subsequence [inset] are exist
subsequence [findset] are exist
// C++ program
// Check if one string is a subsequence of another string
#include<iostream>
using namespace std;
class MyString {
public:
void is_subsequence(string first, string second) {
int j = 0;
//Get the length of given string
int s1 = first.size();
int s2 = second.size();
for (int i = 0; i < s1 &&
j < s2 &&
s1 >= s2; ++i) {
//Check that First string character are exist in second string
if (first[i] == second[j]) {
j++;
}
}
if (j == s2) {
cout << "subsequence [" << second << "] are exist \n";
} else {
cout << "subsequence [" << second << "] are not exist \n";
}
}
};
int main() {
MyString obj = MyString();
//Given string
string text = "infinitycodeset";
cout << text << "\n";
//Test Case
obj.is_subsequence(text, "fitcost");
obj.is_subsequence(text, "noxset");
obj.is_subsequence(text, "finecode");
obj.is_subsequence(text, "inset");
obj.is_subsequence(text, "findset");
return 0;
}
Output
infinitycodeset
subsequence [fitcost] are exist
subsequence [noxset] are not exist
subsequence [finecode] are not exist
subsequence [inset] are exist
subsequence [findset] are exist
// C# program
// Check if one string is a subsequence of another string
using System;
public class MyString {
public void is_subsequence(String first, String second) {
int j = 0;
//Get the length of given string
int s1 = first.Length;
int s2 = second.Length;
for (int i = 0; i < s1 &&
j < s2 &&
s1 >= s2; ++i) {
//Check that First string character are exist in second string
if (first[i] == second[j]) {
j++;
}
}
if (j == s2) {
Console.Write("subsequence [" + second + "] are exist \n");
} else {
Console.Write("subsequence [" + second + "] are not exist \n");
}
}
public static void Main(String[] args) {
MyString obj = new MyString();
//Given string
String text = "infinitycodeset";
Console.Write(text + "\n");
obj.is_subsequence(text, "fitcost");
obj.is_subsequence(text, "noxset");
obj.is_subsequence(text, "finecode");
obj.is_subsequence(text, "inset");
obj.is_subsequence(text, "findset");
}
}
Output
infinitycodeset
subsequence [fitcost] are exist
subsequence [noxset] are not exist
subsequence [finecode] are not exist
subsequence [inset] are exist
subsequence [findset] are exist
<?php
// Php program
// Check if one string is a subsequence of another string
class MyString {
public function is_subsequence($first, $second) {
$j = 0;
//Get the length of given string
$s1 = strlen($first);
$s2 = strlen($second);
for ($i = 0; $i < $s1 &&
$j < $s2 &&
$s1 >= $s2; ++$i) {
//Check that First string character are exist in second string
if ($first[$i] == $second[$j]) {
$j++;
}
}
if ($j == $s2) {
echo("subsequence [". $second ."] are exist \n");
} else {
echo("subsequence [". $second ."] are not exist \n");
}
}
}
function main() {
$obj = new MyString();
//Given string
$text = "infinitycodeset";
echo($text ."\n");
//Test Case
$obj->is_subsequence($text, "fitcost");
$obj->is_subsequence($text, "noxset");
$obj->is_subsequence($text, "finecode");
$obj->is_subsequence($text, "inset");
$obj->is_subsequence($text, "findset");
}
main();
Output
infinitycodeset
subsequence [fitcost] are exist
subsequence [noxset] are not exist
subsequence [finecode] are not exist
subsequence [inset] are exist
subsequence [findset] are exist
// Node Js program
// Check if one string is a subsequence of another string
class MyString {
is_subsequence(first, second) {
var j = 0;
//Get the length of given string
var s1 = first.length;
var s2 = second.length;
for (var i = 0; i < s1 &&
j < s2 &&
s1 >= s2; ++i) {
//Check that First string character are exist in second string
if (first[i] == second[j]) {
j++;
}
}
if (j == s2) {
process.stdout.write("subsequence [" + second + "] are exist \n");
} else {
process.stdout.write("subsequence [" + second + "] are not exist \n");
}
}
}
function main(args) {
var obj = new MyString();
//Given string
var text = "infinitycodeset";
process.stdout.write(text + "\n");
//Test Case
obj.is_subsequence(text, "fitcost");
obj.is_subsequence(text, "noxset");
obj.is_subsequence(text, "finecode");
obj.is_subsequence(text, "inset");
obj.is_subsequence(text, "findset");
}
main();
Output
infinitycodeset
subsequence [fitcost] are exist
subsequence [noxset] are not exist
subsequence [finecode] are not exist
subsequence [inset] are exist
subsequence [findset] are exist
# Python 3 program
# Check if one string is a subsequence of another string
class MyString :
def is_subsequence(self, first, second) :
j = 0
s1 = len(first)
s2 = len(second)
i = 0
while (i < s1 and j < s2 and s1 >= s2) :
# Check that First string character are exist in second string
if (first[i] == second[j]) :
j += 1
i += 1
if (j == s2) :
print("subsequence [", second ,"] are exist ")
else :
print("subsequence [", second ,"] are not exist ")
def main() :
obj = MyString()
text = "infinitycodeset"
print(text)
obj.is_subsequence(text, "fitcost")
obj.is_subsequence(text, "noxset")
obj.is_subsequence(text, "finecode")
obj.is_subsequence(text, "inset")
obj.is_subsequence(text, "findset")
if __name__ == "__main__":
main()
Output
infinitycodeset
subsequence [ fitcost ] are exist
subsequence [ noxset ] are not exist
subsequence [ finecode ] are not exist
subsequence [ inset ] are exist
subsequence [ findset ] are exist
# Ruby program
# Check if one string is a subsequence of another string
class MyString
def is_subsequence(first, second)
j = 0
s1 = first.length()
s2 = second.length()
i = 0
while (i < s1 &&
j < s2 &&
s1 >= s2)
# Check that First string character are exist in second string
if (first[i] == second[j])
j += 1
end
i += 1
end
if (j == s2)
print("subsequence [", second ,"] are exist \n")
else
print("subsequence [", second ,"] are not exist \n")
end
end
end
def main()
obj = MyString.new()
text = "infinitycodeset"
print(text ,"\n")
obj.is_subsequence(text, "fitcost")
obj.is_subsequence(text, "noxset")
obj.is_subsequence(text, "finecode")
obj.is_subsequence(text, "inset")
obj.is_subsequence(text, "findset")
end
main()
Output
infinitycodeset
subsequence [fitcost] are exist
subsequence [noxset] are not exist
subsequence [finecode] are not exist
subsequence [inset] are exist
subsequence [findset] are exist
// Scala program
// Check if one string is a subsequence of another string
class MyString {
def is_subsequence(first: String, second: String): Unit = {
var j: Int = 0;
var s1: Int = first.length();
var s2: Int = second.length();
var i: Int = 0;
while (i < s1 &&
j < s2 &&
s1 >= s2) {
//Check that First string character are exist in second string
if (first(i) == second(j)) {
j += 1;
}
i += 1;
}
if (j == s2) {
print("subsequence [" + second + "] are exist \n");
} else {
print("subsequence [" + second + "] are not exist \n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
var text: String = "infinitycodeset";
print(text + "\n");
obj.is_subsequence(text, "fitcost");
obj.is_subsequence(text, "noxset");
obj.is_subsequence(text, "finecode");
obj.is_subsequence(text, "inset");
obj.is_subsequence(text, "findset");
}
}
Output
infinitycodeset
subsequence [fitcost] are exist
subsequence [noxset] are not exist
subsequence [finecode] are not exist
subsequence [inset] are exist
subsequence [findset] are exist
// Swift program
// Check if one string is a subsequence of another string
class MyString {
func is_subsequence(_ text1: String, _ text2: String) {
var first = Array(text1);
var second = Array(text2);
var j: Int = 0;
let s1: Int = first.count;
let s2: Int = second.count;
var i: Int = 0;
while (i < s1 &&
j < s2 &&
s1 >= s2) {
//Check that First string character are exist in second string
if (first[i] == second[j]) {
j += 1;
}
i += 1;
}
if (j == s2) {
print("subsequence [", text2 ,"] are exist \n", terminator: "");
} else {
print("subsequence [", text2 ,"] are not exist \n", terminator: "");
}
}
}
func main() {
let obj: MyString = MyString();
let text: String = "infinitycodeset";
print(text ,"\n", terminator: "");
obj.is_subsequence(text, "fitcost");
obj.is_subsequence(text, "noxset");
obj.is_subsequence(text, "finecode");
obj.is_subsequence(text, "inset");
obj.is_subsequence(text, "findset");
}
main();
Output
infinitycodeset
subsequence [ fitcost ] are exist
subsequence [ noxset ] are not exist
subsequence [ finecode ] are not exist
subsequence [ inset ] are exist
subsequence [ findset ] are exist
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment