Dada una string, nuestra tarea es imprimir los caracteres presentes en el índice principal.
Ejemplos:
C++
// C++ Program to print Characters at // Prime index in a given String #include <bits/stdc++.h> using namespace std; bool isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } // Function to print // character at prime index void prime_index(string input) { int n = input.length(); // Loop to check if // index prime or not for (int i = 2; i <= n; i++) if (isPrime(i)) cout << input[i - 1]; } // Driver Code int main() { string input = "GeeksforGeeks"; prime_index(input); return 0; }
Java
// Java Program to print // Characters at Prime index // in a given String class GFG { static boolean isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } // Function to print // character at prime index static void prime_index(String input) { int n = input.length(); // Loop to check if // index prime or not for (int i = 2; i <= n; i++) if (isPrime(i)) System.out.print (input.charAt(i - 1)); } // Driver code public static void main (String[] args) { String input = "GeeksforGeeks"; prime_index(input); } } // This code is contributed by Anant Agarwal.
Python3
# Python3 program to print # Characters at Prime index # in a given String def isPrime(n): # Corner case if n <= 1: return False # Check from 2 to n-1 for i in range(2, n): if n % i == 0: return False; return True # Function to print # character at prime index def prime_index (input): p = list(input) s = "" # Loop to check if # index prime or not for i in range (2, len(p) + 1): if isPrime(i): s = s + input[i-1] print (s) # Driver Code input = "GeeksforGeeks" prime_index(input)
C#
// C# Program to print Characters // at Prime index in a given String using System; class GFG { static bool isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } // Function to print character // at prime index static void prime_index(string input) { int n = input.Length; // Loop to check if // index prime or not for (int i = 2; i <= n; i++) if (isPrime(i)) Console.Write(input[i - 1]); } // Driver code public static void Main () { string input = "GeeksforGeeks"; prime_index(input); } } // This code is contributed by Vt_m.
PHP
<?php // PHP Program to print // Characters at Prime // index in a given String function isPrime($n) { // Corner case if ($n <= 1) return false; // Check from 2 to n-1 for ($i = 2; $i < $n; $i++) if ($n % $i == 0) return false; return true; } // Function to print // character at prime index function prime_index($input) { $n = strlen($input); // Loop to check if // index prime or not for ($i = 2; $i <= $n; $i++) if (isPrime($i)) echo $input[$i - 1]; } // Driver Code $input = "GeeksforGeeks"; prime_index($input); // This code is contributed by ajit. ?>
Javascript
<script> // Javascript program to print characters // at Prime index in a given String function isPrime(n) { // Corner case if (n <= 1) return false; // Check from 2 to n-1 for(let i = 2; i < n; i++) if (n % i == 0) return false; return true; } // Function to print character // at prime index function prime_index(input) { let n = input.length; // Loop to check if // index prime or not for(let i = 2; i <= n; i++) if (isPrime(i)) document.write(input[i - 1]); } // Driver code let input = "GeeksforGeeks"; prime_index(input); // This code is contributed by suresh07 </script>
Publicación traducida automáticamente
Artículo escrito por shreyanshi_arun y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA