Check if a string is suffix of another
Here given code implementation process.
// C Program
// Check if a string is suffix of another
#include <stdio.h>
void is_suffix(char str1[],char str2[],int s1,int s2)
{
int status=1;
int i = 0,j=0;
if(s1<s2)
{
status=0;
}
for (i = s1-s2,j=0; i < s1 && status==1; ++i,j++)
{
if(str1[i] != str2[j])
{
status=0;
}
}
if(status == 0 )
{
printf("suffix [%s] are not exist \n",str2);
}
else
{
printf("suffix [%s] are exist \n",str2);
}
}
int main()
{
char str1[]="writeasimplecode";
char str2[]="code";
char str3[]="xcode";
char str4[]="mecode";
char str5[]="codex";
char str6[]="ecode";
char str7[]="writeasimplecode";
printf("%s\n",str1 );
int s1=sizeof(str1)/sizeof(str1[0])-1;
int s2=sizeof(str2)/sizeof(str2[0])-1;
is_suffix(str1,str2,s1,s2);
s2 = sizeof(str3)/sizeof(str3[0])-1;
is_suffix(str1,str3,s1,s2);
s2 = sizeof(str4)/sizeof(str4[0])-1;
is_suffix(str1,str4,s1,s2);
s2 = sizeof(str5)/sizeof(str5[0])-1;
is_suffix(str1,str5,s1,s2);
s2 = sizeof(str6)/sizeof(str6[0])-1;
is_suffix(str1,str6,s1,s2);
s2 = sizeof(str7)/sizeof(str7[0])-1;
is_suffix(str1,str7,s1,s2);
return 0;
}
Output
writeasimplecode
suffix [code] are exist
suffix [xcode] are not exist
suffix [mecode] are not exist
suffix [codex] are not exist
suffix [ecode] are exist
suffix [writeasimplecode] are exist
// Java program
// Check if a string is suffix of another
public class MyString {
public void is_suffix(String str1, String str2) {
//Get the length of given string
int s1 = str1.length();
int s2 = str2.length();
boolean status = true;
int j = 0;
int i = 0;
if (s2 > s1) {
status = false;
}
for (i = s1 - s2, j = 0; i < s1 && status == true; ++i, j++) {
if (str1[i] != str2[j]) {
status = false;
}
}
if (status == true) {
System.out.print("suffix [" + str2 + "] are exist \n");
} else {
System.out.print("suffix [" + str2 + "] are not exist \n");
}
}
public static void main(String[] args) {
MyString obj = new MyString();
//Given string
String text = "writeasimplecode";
System.out.print(text + "\n");
//Test Case
obj.is_suffix(text, "code");
obj.is_suffix(text, "xcode");
obj.is_suffix(text, "mecode");
obj.is_suffix(text, "codex");
obj.is_suffix(text, "ecode");
obj.is_suffix(text, "writeasimplecode");
}
}
Output
writeasimplecode
suffix [code] are exist
suffix [xcode] are not exist
suffix [mecode] are not exist
suffix [codex] are not exist
suffix [ecode] are exist
suffix [writeasimplecode] are exist
// C++ program
// Check if a string is suffix of another
#include<iostream>
using namespace std;
class MyString {
public:
void is_suffix(string str1, string str2) {
//Get the length of given string
int s1 = str1.size();
int s2 = str2.size();
bool status = true;
int j = 0;
int i = 0;
if (s2 > s1) {
status = false;
}
for (i = s1 - s2, j = 0; i < s1 &&
status == true; ++i, j++) {
if (str1[i] != str2[j]) {
status = false;
}
}
if (status == true) {
cout << "suffix [" << str2 << "] are exist \n";
} else {
cout << "suffix [" << str2 << "] are not exist \n";
}
}
};
int main() {
MyString obj = MyString();
//Given string
string text = "writeasimplecode";
cout << text << "\n";
//Test Case
obj.is_suffix(text, "code");
obj.is_suffix(text, "xcode");
obj.is_suffix(text, "mecode");
obj.is_suffix(text, "codex");
obj.is_suffix(text, "ecode");
obj.is_suffix(text, "writeasimplecode");
return 0;
}
Output
writeasimplecode
suffix [code] are exist
suffix [xcode] are not exist
suffix [mecode] are not exist
suffix [codex] are not exist
suffix [ecode] are exist
suffix [writeasimplecode] are exist
// C# program
// Check if a string is suffix of another
using System;
public class MyString {
public void is_suffix(String str1, String str2) {
//Get the length of given string
int s1 = str1.Length;
int s2 = str2.Length;
Boolean status = true;
int j = 0;
int i = 0;
if (s2 > s1) {
status = false;
}
for (i = s1 - s2, j = 0; i < s1 &&
status == true; ++i, j++) {
if (str1[i] != str2[j]) {
status = false;
}
}
if (status == true) {
Console.Write("suffix [" + str2 + "] are exist \n");
} else {
Console.Write("suffix [" + str2 + "] are not exist \n");
}
}
public static void Main(String[] args) {
MyString obj = new MyString();
//Given string
String text = "writeasimplecode";
Console.Write(text + "\n");
obj.is_suffix(text, "code");
obj.is_suffix(text, "xcode");
obj.is_suffix(text, "mecode");
obj.is_suffix(text, "codex");
obj.is_suffix(text, "ecode");
obj.is_suffix(text, "writeasimplecode");
}
}
Output
writeasimplecode
suffix [code] are exist
suffix [xcode] are not exist
suffix [mecode] are not exist
suffix [codex] are not exist
suffix [ecode] are exist
suffix [writeasimplecode] are exist
// Node Js program
// Check if a string is suffix of another
class MyString {
is_suffix(str1, str2) {
//Get the length of given string
var s1 = str1.length;
var s2 = str2.length;
var status = true;
var j = 0;
var i = 0;
if (s2 > s1) {
status = false;
}
for (i = s1 - s2, j = 0; i < s1 &&
status == true; ++i, j++) {
if (str1[i] != str2[j]) {
status = false;
}
}
if (status == true) {
process.stdout.write("suffix [" + str2 + "] are exist \n");
} else {
process.stdout.write("suffix [" + str2 + "] are not exist \n");
}
}
}
function main(args) {
var obj = new MyString();
//Given string
var text = "writeasimplecode";
process.stdout.write(text + "\n");
//Test Case
obj.is_suffix(text, "code");
obj.is_suffix(text, "xcode");
obj.is_suffix(text, "mecode");
obj.is_suffix(text, "codex");
obj.is_suffix(text, "ecode");
obj.is_suffix(text, "writeasimplecode");
}
main();
Output
writeasimplecode
suffix [code] are exist
suffix [xcode] are not exist
suffix [mecode] are not exist
suffix [codex] are not exist
suffix [ecode] are exist
suffix [writeasimplecode] are exist
# Python 3 program
# Check if a string is suffix of another
class MyString :
def is_suffix(self, str1, str2) :
# Get the length of given string
s1 = len(str1)
s2 = len(str2)
status = True
j = 0
i = 0
if (s2 > s1) :
status = False
i = s1 - s2
j = 0
while (i < s1 and status == True) :
if (str1[i] != str2[j]) :
status = False
i += 1
j += 1
if (status == True) :
print("suffix [", str2 ,"] are exist ")
else :
print("suffix [", str2 ,"] are not exist ")
def main() :
obj = MyString()
text = "writeasimplecode"
print(text)
obj.is_suffix(text, "code")
obj.is_suffix(text, "xcode")
obj.is_suffix(text, "mecode")
obj.is_suffix(text, "codex")
obj.is_suffix(text, "ecode")
obj.is_suffix(text, "writeasimplecode")
if __name__ == "__main__":
main()
Output
writeasimplecode
suffix [ code ] are exist
suffix [ xcode ] are not exist
suffix [ mecode ] are not exist
suffix [ codex ] are not exist
suffix [ ecode ] are exist
suffix [ writeasimplecode ] are exist
# Ruby program
# Check if a string is suffix of another
class MyString
def is_suffix(str1, str2)
# Get the length of given string
s1 = str1.length()
s2 = str2.length()
status = true
j = 0
i = 0
if (s2 > s1)
status = false
end
i = s1 - s2
j = 0
while (i < s1 &&
status == true)
if (str1[i] != str2[j])
status = false
end
i += 1
j += 1
end
if (status == true)
print("suffix [", str2 ,"] are exist \n")
else
print("suffix [", str2 ,"] are not exist \n")
end
end
end
def main()
obj = MyString.new()
text = "writeasimplecode"
print(text ,"\n")
obj.is_suffix(text, "code")
obj.is_suffix(text, "xcode")
obj.is_suffix(text, "mecode")
obj.is_suffix(text, "codex")
obj.is_suffix(text, "ecode")
obj.is_suffix(text, "writeasimplecode")
end
main()
Output
writeasimplecode
suffix [code] are exist
suffix [xcode] are not exist
suffix [mecode] are not exist
suffix [codex] are not exist
suffix [ecode] are exist
suffix [writeasimplecode] are exist
// Scala program
// Check if a string is suffix of another
class MyString {
def is_suffix(str1: String, str2: String): Unit = {
//Get the length of given string
var s1: Int = str1.length();
var s2: Int = str2.length();
var status: Boolean = true;
var j: Int = 0;
var i: Int = 0;
if (s2 > s1) {
status = false;
}
i = s1 - s2;
j = 0;
while (i < s1 &&
status == true) {
if (str1(i) != str2(j)) {
status = false;
}
i += 1;
j += 1;
}
if (status == true) {
print("suffix [" + str2 + "] are exist \n");
} else {
print("suffix [" + str2 + "] are not exist \n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
var text: String = "writeasimplecode";
print(text + "\n");
obj.is_suffix(text, "code");
obj.is_suffix(text, "xcode");
obj.is_suffix(text, "mecode");
obj.is_suffix(text, "codex");
obj.is_suffix(text, "ecode");
obj.is_suffix(text, "writeasimplecode");
}
}
Output
writeasimplecode
suffix [code] are exist
suffix [xcode] are not exist
suffix [mecode] are not exist
suffix [codex] are not exist
suffix [ecode] are exist
suffix [writeasimplecode] are exist
// Swift program
// Check if a string is suffix of another
class MyString {
func is_suffix(_ text1: String, _ text2: String) {
var str1 = Array(text1);
var str2 = Array(text2);
//Get the length of given string
let s1: Int = str1.count;
let s2: Int = str2.count;
var status: Bool = true;
var j: Int = 0;
var i: Int = 0;
if (s2 > s1) {
status = false;
}
i = s1 - s2;
j = 0;
while (i < s1 &&
status == true) {
if (str1[i] != str2[j]) {
status = false;
}
i += 1;
j += 1;
}
if (status == true) {
print("suffix [\(text2)] are exist ");
} else {
print("suffix [\(text2)] are not exist ");
}
}
}
func main() {
let obj: MyString = MyString();
let text: String = "writeasimplecode";
print(text);
obj.is_suffix(text, "code");
obj.is_suffix(text, "xcode");
obj.is_suffix(text, "mecode");
obj.is_suffix(text, "codex");
obj.is_suffix(text, "ecode");
obj.is_suffix(text, "writeasimplecode");
}
main();
Output
writeasimplecode
suffix [code] are exist
suffix [xcode] are not exist
suffix [mecode] are not exist
suffix [codex] are not exist
suffix [ecode] are exist
suffix [writeasimplecode] are exist
<?php
// Php program
// Check if a string is suffix of another
class MyString {
public function is_suffix($str1, $str2) {
//Get the length of given string
$s1 = strlen($str1);
$s2 = strlen($str2);
$status = true;
$j = 0;
$i = 0;
if ($s2 > $s1) {
$status = false;
}
$i = $s1 - $s2;
$j = 0;
while ($i < $s1 &&
$status == true) {
if ($str1[$i] != $str2[$j]) {
$status = false;
}++$i;
$j++;
}
if ($status == true) {
echo("suffix [". $str2 ."] are exist \n");
} else {
echo("suffix [". $str2 ."] are not exist \n");
}
}
}
function main() {
$obj = new MyString();
$text = "writeasimplecode";
echo($text ."\n");
$obj->is_suffix($text, "code");
$obj->is_suffix($text, "xcode");
$obj->is_suffix($text, "mecode");
$obj->is_suffix($text, "codex");
$obj->is_suffix($text, "ecode");
$obj->is_suffix($text, "writeasimplecode");
}
main();
Output
writeasimplecode
suffix [code] are exist
suffix [xcode] are not exist
suffix [mecode] are not exist
suffix [codex] are not exist
suffix [ecode] are exist
suffix [writeasimplecode] 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