Check if a character is present in a string
Here given code implementation process.
// C Program for
// Check if a character is present in a string
#include <stdio.h>
#include <string.h>
// Check whether a particular character is present in a string
void isCharacterPresent(char *text, char ch)
{
int n = strlen(text);
if (n == 0)
{
// Empty String
return;
}
printf("\n Given text : %s ", text);
printf("\n Given character : %c ", ch);
int i = 0;
while (i < n)
{
if (text[i] == ch)
{
// When character exist
printf("\n Result : Yes ");
return;
}
i++;
}
printf("\n Result : No ");
}
int main(int argc, char const *argv[])
{
// Test
// text = "SDIP02BX"
// character = 'O'
isCharacterPresent("SDIP02BX", 'O');
// text = "axynpqr"
// character = 'n'
isCharacterPresent("axynpqr", 'n');
return 0;
}
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
// Java program for
// Check if a character is present in a string
public class Alphabets
{
// Check whether a particular character is present in a string
public void isCharacterPresent(String text, char ch)
{
int n = text.length();
System.out.print("\n Given text : " + text);
System.out.print("\n Given character : " + ch);
int i = 0;
while (i < n)
{
if (text.charAt(i) == ch)
{
// When character exist
System.out.print("\n Result : Yes ");
return;
}
i++;
}
System.out.print("\n Result : No ");
}
public static void main(String[] args)
{
Alphabets task = new Alphabets();
// Test
// text = "SDIP02BX"
// character = 'O'
task.isCharacterPresent("SDIP02BX", 'O');
// text = "axynpqr"
// character = 'n'
task.isCharacterPresent("axynpqr", 'n');
}
}
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
// Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ program for
// Check if a character is present in a string
class Alphabets
{
public:
// Check whether a particular character is present in a string
void isCharacterPresent(string text, char ch)
{
int n = text.length();
cout << "\n Given text : " << text;
cout << "\n Given character : " << ch;
int i = 0;
while (i < n)
{
if (text[i] == ch)
{
// When character exist
cout << "\n Result : Yes ";
return;
}
i++;
}
cout << "\n Result : No ";
}
};
int main()
{
Alphabets *task = new Alphabets();
// Test
// text = "SDIP02BX"
// character = 'O'
task->isCharacterPresent("SDIP02BX", 'O');
// text = "axynpqr"
// character = 'n'
task->isCharacterPresent("axynpqr", 'n');
return 0;
}
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
package main
import "fmt"
// Go program for
// Check if a character is present in a string
type Alphabets struct {}
func getAlphabets() * Alphabets {
var me *Alphabets = &Alphabets {}
return me
}
// Check whether a particular character is present in a string
func(this Alphabets) isCharacterPresent(text string, ch byte) {
var n int = len(text)
fmt.Print("\n Given text : ", text)
fmt.Printf("\n Given character : %c", ch)
var i int = 0
for (i < n) {
if text[i] == ch {
// When character exist
fmt.Print("\n Result : Yes ")
return
}
i++
}
fmt.Print("\n Result : No ")
}
func main() {
var task * Alphabets = getAlphabets()
// Test
// text = "SDIP02BX"
// character = 'O'
task.isCharacterPresent("SDIP02BX", 'O')
// text = "axynpqr"
// character = 'n'
task.isCharacterPresent("axynpqr", 'n')
}
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
// Include namespace system
using System;
// Csharp program for
// Check if a character is present in a string
public class Alphabets
{
// Check whether a particular character is present in a string
public void isCharacterPresent(String text, char ch)
{
int n = text.Length;
Console.Write("\n Given text : " + text);
Console.Write("\n Given character : " + ch);
int i = 0;
while (i < n)
{
if (text[i] == ch)
{
// When character exist
Console.Write("\n Result : Yes ");
return;
}
i++;
}
Console.Write("\n Result : No ");
}
public static void Main(String[] args)
{
Alphabets task = new Alphabets();
// Test
// text = "SDIP02BX"
// character = 'O'
task.isCharacterPresent("SDIP02BX", 'O');
// text = "axynpqr"
// character = 'n'
task.isCharacterPresent("axynpqr", 'n');
}
}
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
<?php
// Php program for
// Check if a character is present in a string
class Alphabets
{
// Check whether a particular character is present in a string
public function isCharacterPresent($text, $ch)
{
$n = strlen($text);
echo("\n Given text : ".$text);
echo("\n Given character : ".$ch);
$i = 0;
while ($i < $n)
{
if ($text[$i] == $ch)
{
// When character exist
echo("\n Result : Yes ");
return;
}
$i++;
}
echo("\n Result : No ");
}
}
function main()
{
$task = new Alphabets();
// Test
// text = "SDIP02BX"
// character = 'O'
$task->isCharacterPresent("SDIP02BX", 'O');
// text = "axynpqr"
// character = 'n'
$task->isCharacterPresent("axynpqr", 'n');
}
main();
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
// Node JS program for
// Check if a character is present in a string
class Alphabets
{
// Check whether a particular character is present in a string
isCharacterPresent(text, ch)
{
var n = text.length;
process.stdout.write("\n Given text : " + text);
process.stdout.write("\n Given character : " + ch);
var i = 0;
while (i < n)
{
if (text.charAt(i) == ch)
{
// When character exist
process.stdout.write("\n Result : Yes ");
return;
}
i++;
}
process.stdout.write("\n Result : No ");
}
}
function main()
{
var task = new Alphabets();
// Test
// text = "SDIP02BX"
// character = 'O'
task.isCharacterPresent("SDIP02BX", 'O');
// text = "axynpqr"
// character = 'n'
task.isCharacterPresent("axynpqr", 'n');
}
main();
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
# Python 3 program for
# Check if a character is present in a string
class Alphabets :
# Check whether a particular character is present in a string
def isCharacterPresent(self, text, ch) :
n = len(text)
print("\n Given text : ", text, end = "")
print("\n Given character : ", ch, end = "")
i = 0
while (i < n) :
if (text[i] == ch) :
# When character exist
print("\n Result : Yes ", end = "")
return
i += 1
print("\n Result : No ", end = "")
def main() :
task = Alphabets()
# Test
# text = "SDIP02BX"
# character = 'O'
task.isCharacterPresent("SDIP02BX", 'O')
# text = "axynpqr"
# character = 'n'
task.isCharacterPresent("axynpqr", 'n')
if __name__ == "__main__": main()
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
# Ruby program for
# Check if a character is present in a string
class Alphabets
# Check whether a particular character is present in a string
def isCharacterPresent(text, ch)
n = text.length
print("\n Given text : ", text)
print("\n Given character : ", ch)
i = 0
while (i < n)
if (text[i] == ch)
# When character exist
print("\n Result : Yes ")
return
end
i += 1
end
print("\n Result : No ")
end
end
def main()
task = Alphabets.new()
# Test
# text = "SDIP02BX"
# character = 'O'
task.isCharacterPresent("SDIP02BX", 'O')
# text = "axynpqr"
# character = 'n'
task.isCharacterPresent("axynpqr", 'n')
end
main()
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
import scala.collection.mutable._;
// Scala program for
// Check if a character is present in a string
class Alphabets()
{
// Check whether a particular character is present in a string
def isCharacterPresent(text: String, ch: Char): Unit = {
var n: Int = text.length();
print("\n Given text : " + text);
print("\n Given character : " + ch);
var i: Int = 0;
while (i < n)
{
if (text.charAt(i) == ch)
{
// When character exist
print("\n Result : Yes ");
return;
}
i += 1;
}
print("\n Result : No ");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Alphabets = new Alphabets();
// Test
// text = "SDIP02BX"
// character = 'O'
task.isCharacterPresent("SDIP02BX", 'O');
// text = "axynpqr"
// character = 'n'
task.isCharacterPresent("axynpqr", 'n');
}
}
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
import Foundation;
// Swift 4 program for
// Check if a character is present in a string
class Alphabets
{
// Check whether a particular character is present in a string
func isCharacterPresent(_ data: String, _ ch: Character)
{
let text = Array(data);
let n: Int = text.count;
print("\n Given text : ", data, terminator: "");
print("\n Given character : ", ch, terminator: "");
var i: Int = 0;
while (i < n)
{
if (text[i] == ch)
{
// When character exist
print("\n Result : Yes ", terminator: "");
return;
}
i += 1;
}
print("\n Result : No ", terminator: "");
}
}
func main()
{
let task: Alphabets = Alphabets();
// Test
// text = "SDIP02BX"
// character = 'O'
task.isCharacterPresent("SDIP02BX", "O");
// text = "axynpqr"
// character = 'n'
task.isCharacterPresent("axynpqr", "n");
}
main();
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
// Kotlin program for
// Check if a character is present in a string
class Alphabets
{
// Check whether a particular character is present in a string
fun isCharacterPresent(text: String, ch: Char): Unit
{
val n: Int = text.length;
print("\n Given text : " + text);
print("\n Given character : " + ch);
var i: Int = 0;
while (i < n)
{
if (text.get(i) == ch)
{
// When character exist
print("\n Result : Yes ");
return;
}
i += 1;
}
print("\n Result : No ");
}
}
fun main(args: Array < String > ): Unit
{
val task: Alphabets = Alphabets();
// Test
// text = "SDIP02BX"
// character = 'O'
task.isCharacterPresent("SDIP02BX", 'O');
// text = "axynpqr"
// character = 'n'
task.isCharacterPresent("axynpqr", 'n');
}
Output
Given text : SDIP02BX
Given character : O
Result : No
Given text : axynpqr
Given character : n
Result : Yes
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