Dadas dos strings s1 y s2, compruebe si s1 es un sufijo de s2. O en palabras simples, necesitamos encontrar si la string s2 termina con la string s1.
Ejemplos:
C++
// CPP program to find if a string is suffix of another #include <bits/stdc++.h> using namespace std; bool isSuffix(string s1, string s2) { int n1 = s1.length(), n2 = s2.length(); if (n1 > n2) return false; for (int i = 0; i < n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true; } int main() { string s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation of endsWith // function bool result = isSuffix(s1, s2); if (result) cout << "Yes"; else cout << "No"; return 0; } // This code is contributed by Aditya Kumar (adityakumar129)
C
// C program to find if a string is suffix of another #include <stdbool.h> #include <stdio.h> #include <string.h> bool isSuffix(char s1[], char s2[]) { int n1 = strlen(s1), n2 = strlen(s2); if (n1 > n2) return false; for (int i = 0; i < n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true; } int main() { char s1[] = "geeks", s2[] = "geeksforgeeks"; // Test case-sensitive implementation of endsWith // function bool result = isSuffix(s1, s2); if (result) printf("Yes"); else printf("No"); return 0; } // This code is contributed by Aditya Kumar (adityakumar129)
Java
// Java program to find if a string is // suffix of another class GFG { static boolean isSuffix(String s1, String s2) { int n1 = s1.length(), n2 = s2.length(); if (n1 > n2) return false; for (int i=0; i<n1; i++) if (s1.charAt(n1 - i - 1) != s2.charAt(n2 - i - 1)) return false; return true; } public static void main(String []args) { String s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function boolean result = isSuffix(s1, s2); if (result) System.out.println( "Yes"); else System.out.println("No"); } } // This code is contributed by iAyushRaj
Python3
# Python 3 program to find if a # string is suffix of another def isSuffix(s1, s2): n1 = len(s1) n2 = len(s2) if (n1 > n2): return False for i in range(n1): if(s1[n1 - i - 1] != s2[n2 - i - 1]): return False return True # Driver Code if __name__ == "__main__": s1 = "geeks" s2 = "geeksforgeeks" # Test case-sensitive implementation # of endsWith function result = isSuffix(s1, s2) if (result): print("Yes") else: print( "No") # This code is contributed # by ChitraNayal
C#
// C# program to find if a string is // suffix of another using System; class GFG { static bool isSuffix(string s1, string s2) { int n1 = s1.Length, n2 = s2.Length; if (n1 > n2) return false; for (int i=0; i<n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true; } public static void Main() { string s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function bool result = isSuffix(s1, s2); if (result) Console.WriteLine( "Yes"); else Console.WriteLine("No"); } } // This code is contributed by iAyushRaj
PHP
<?php // PHP program to find if a // string is suffix of another function isSuffix($s1, $s2) { $n1 = ($s1); $n2 = strlen($s2); if ($n1 > $n2) return false; for ($i = 0; $i < $n1; $i++) if ($s1[$n1 - $i - 1] != $s2[$n2 - $i - 1]) return false; return true; } // Driver Code $s1 = "geeks"; $s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function $result = isSuffix($s1, $s2); if ($result) echo "Yes"; else echo "No"; // This code is contributed by m_kit ?>
Javascript
<script> // Javascript program to find if // a string is suffix of another function isSuffix(s1, s2) { let n1 = s1.length, n2 = s2.length; if (n1 > n2) return false; for(let i = 0; i < n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true; } // Driver code let s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function let result = isSuffix(s1, s2); if (result) document.write( "Yes"); else document.write("No"); // This code is contributed by decode2207 </script>
C++
// CPP program to find if a string is // suffix of another #include <boost/algorithm/string.hpp> #include <iostream> #include <string> using namespace std; int main() { string s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function bool result = boost::algorithm::ends_with(s2, s1); if (result) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java program to find if a string is // suffix of another class GFG { public static void main(String[] args) { String s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function boolean result = s2.endsWith(s1); if (result) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by 29AjayKumar
Python3
# Python3 program to find if a string is # suffix of another if __name__ == '__main__': s1 = "geeks"; s2 = "geeksforgeeks"; # Test case-sensitive implementation # of endsWith function result = s2.endswith(s1); if (result): print("Yes"); else: print("No"); # This code is contributed by Rajput-Ji
C#
// C# program to find if a string is // suffix of another using System; class GFG { // Driver code public static void Main(String[] args) { String s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function bool result = s2.EndsWith(s1); if (result) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code contributed by Rajput-Ji
Javascript
<script> // Javascript program to find if a string is // suffix of another let s1 = "geeks", s2 = "geeksforgeeks"; // Test case-sensitive implementation // of endsWith function let result = s2.endsWith(s1); if (result) document.write("Yes"); else document.write("No"); // This code is contributed by avanitrachhadiya2155 </script>
Publicación traducida automáticamente
Artículo escrito por Gaurang Bansal 2 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA