Find unique character in a string
Here given code implementation process.
/*
C program for
Find unique character in a string
*/
#include <stdio.h>
#include <string.h>
void findUniqueChar(char *text)
{
int n = strlen(text);
if (n == 0)
{
return;
}
printf("\n Given Text : %s \n", text);
int result = 0;
int record[256];
// Set initial count of character record
for (int i = 0; i < 256; ++i)
{
record[i] = 0;
}
// Count frequency of character element
for (int i = 0; i < n; ++i)
{
record[text[i]] = record[text[i]] + 1;
}
for (int i = 0; i < n; ++i)
{
if (record[text[i]] == 1)
{
// When [i] location character appears once
printf(" %c", text[i]);
result = 1;
}
}
if (result == 0)
{
printf(" No result");
}
}
int main(int argc, char const *argv[])
{
// Given Sting text
char *text1 = "office";
char *text2 = "error";
char *text3 = "xxxx";
// Test
findUniqueChar(text1);
findUniqueChar(text2);
findUniqueChar(text3);
return 0;
}
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
/*
Java program for
Find unique character in a string
*/
public class NonRepetitive
{
public void findUniqueChar(String text)
{
int n = text.length();
if (n == 0)
{
return;
}
System.out.println("\n Given Text : " + text);
boolean result = false;
int[] record = new int[256];
// Set initial count of character record
for (int i = 0; i < 256; ++i)
{
record[i] = 0;
}
// Count frequency of character element
for (int i = 0; i < n; ++i)
{
record[text.charAt(i)] = record[text.charAt(i)] + 1;
}
for (int i = 0; i < n; ++i)
{
if (record[text.charAt(i)] == 1)
{
// When [i] location character appears once
System.out.print(" " + text.charAt(i));
result = true;
}
}
if (result == false)
{
System.out.print(" No result");
}
}
public static void main(String[] args)
{
NonRepetitive task = new NonRepetitive();
// Given Sting text
String text1 = "office";
String text2 = "error";
String text3 = "xxxx";
// Test
task.findUniqueChar(text1);
task.findUniqueChar(text2);
task.findUniqueChar(text3);
}
}
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ program for
Find unique character in a string
*/
class NonRepetitive
{
public: void findUniqueChar(string text)
{
int n = text.length();
if (n == 0)
{
return;
}
cout << "\n Given Text : " << text << endl;
bool result = false;
int record[256];
// Set initial count of character record
for (int i = 0; i < 256; ++i)
{
record[i] = 0;
}
// Count frequency of character element
for (int i = 0; i < n; ++i)
{
record[text[i]] = record[text[i]] + 1;
}
for (int i = 0; i < n; ++i)
{
if (record[text[i]] == 1)
{
// When [i] location character appears once
cout << " " << text[i];
result = true;
}
}
if (result == false)
{
cout << " No result";
}
}
};
int main()
{
NonRepetitive *task = new NonRepetitive();
// Given Sting text
string text1 = "office";
string text2 = "error";
string text3 = "xxxx";
// Test
task->findUniqueChar(text1);
task->findUniqueChar(text2);
task->findUniqueChar(text3);
return 0;
}
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
// Include namespace system
using System;
/*
Csharp program for
Find unique character in a string
*/
public class NonRepetitive
{
public void findUniqueChar(String text)
{
int n = text.Length;
if (n == 0)
{
return;
}
Console.WriteLine("\n Given Text : " + text);
Boolean result = false;
int[] record = new int[256];
// Set initial count of character record
for (int i = 0; i < 256; ++i)
{
record[i] = 0;
}
// Count frequency of character element
for (int i = 0; i < n; ++i)
{
record[text[i]] = record[text[i]] + 1;
}
for (int i = 0; i < n; ++i)
{
if (record[text[i]] == 1)
{
// When [i] location character appears once
Console.Write(" " + text[i]);
result = true;
}
}
if (result == false)
{
Console.Write(" No result");
}
}
public static void Main(String[] args)
{
NonRepetitive task = new NonRepetitive();
// Given Sting text
String text1 = "office";
String text2 = "error";
String text3 = "xxxx";
// Test
task.findUniqueChar(text1);
task.findUniqueChar(text2);
task.findUniqueChar(text3);
}
}
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
package main
import "fmt"
/*
Go program for
Find unique character in a string
*/
func findUniqueChar(text string) {
var n int = len(text)
if n == 0 {
return
}
fmt.Println("\n Given Text : ", text)
var result bool = false
var record = make([] int,256)
// Count frequency of character element
for i := 0 ; i < n ; i++ {
record[text[i]] = record[text[i]] + 1
}
for i := 0 ; i < n ; i++ {
if record[text[i]] == 1 {
// When [i] location character appears once
fmt.Printf(" %c", text[i])
result = true
}
}
if result == false {
fmt.Print(" No result")
}
}
func main() {
// Given Sting text
var text1 string = "office"
var text2 string = "error"
var text3 string = "xxxx"
// Test
findUniqueChar(text1)
findUniqueChar(text2)
findUniqueChar(text3)
}
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
<?php
/*
Php program for
Find unique character in a string
*/
class NonRepetitive
{
public function findUniqueChar($text)
{
$n = strlen($text);
if ($n == 0)
{
return;
}
echo("\n Given Text : ".$text."\n");
$result = false;
$record = array_fill(0, 256, 0);
// Count frequency of character element
for ($i = 0; $i < $n; ++$i)
{
$record[ord($text[$i])] = $record[ord($text[$i])] + 1;
}
for ($i = 0; $i < $n; ++$i)
{
if ($record[ord($text[$i])] == 1)
{
// When [i] location character appears once
echo(" ".$text[$i]);
$result = true;
}
}
if ($result == false)
{
echo(" No result");
}
}
}
function main()
{
$task = new NonRepetitive();
// Given Sting text
$text1 = "office";
$text2 = "error";
$text3 = "xxxx";
// Test
$task->findUniqueChar($text1);
$task->findUniqueChar($text2);
$task->findUniqueChar($text3);
}
main();
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
/*
Node JS program for
Find unique character in a string
*/
class NonRepetitive
{
findUniqueChar(text)
{
var n = text.length;
if (n == 0)
{
return;
}
console.log("\n Given Text : " + text);
var result = false;
var record = Array(256).fill(0);
// Count frequency of character element
for (var i = 0; i < n; ++i)
{
record[text.charCodeAt(i)] = record[text.charCodeAt(i)] + 1;
}
for (var i = 0; i < n; ++i)
{
if (record[text.charCodeAt(i)] == 1)
{
// When [i] location character appears once
process.stdout.write(" " + text.charAt(i));
result = true;
}
}
if (result == false)
{
process.stdout.write(" No result");
}
}
}
function main()
{
var task = new NonRepetitive();
// Given Sting text
var text1 = "office";
var text2 = "error";
var text3 = "xxxx";
// Test
task.findUniqueChar(text1);
task.findUniqueChar(text2);
task.findUniqueChar(text3);
}
main();
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
# Python 3 program for
# Find unique character in a string
class NonRepetitive :
def findUniqueChar(self, text) :
n = len(text)
if (n == 0) :
return
print("\n Given Text : ", text)
result = False
record = [0] * (256)
i = 0
# Count frequency of character element
while (i < n) :
record[ord(text[i])] = record[ord(text[i])] + 1
i += 1
i = 0
while (i < n) :
if (record[ord(text[i])] == 1) :
# When [i] location character appears once
print(" ", text[i], end = "")
result = True
i += 1
if (result == False) :
print(" No result", end = "")
def main() :
task = NonRepetitive()
# Given Sting text
text1 = "office"
text2 = "error"
text3 = "xxxx"
# Test
task.findUniqueChar(text1)
task.findUniqueChar(text2)
task.findUniqueChar(text3)
if __name__ == "__main__": main()
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
# Ruby program for
# Find unique character in a string
class NonRepetitive
def findUniqueChar(text)
n = text.length
if (n == 0)
return
end
print("\n Given Text : ", text, "\n")
result = false
record = Array.new(256) {0}
i = 0
# Count frequency of character element
while (i < n)
record[text[i].ord] = record[text[i].ord] + 1
i += 1
end
i = 0
while (i < n)
if (record[text[i].ord] == 1)
# When [i] location character appears once
print(" ", text[i])
result = true
end
i += 1
end
if (result == false)
print(" No result")
end
end
end
def main()
task = NonRepetitive.new()
# Given Sting text
text1 = "office"
text2 = "error"
text3 = "xxxx"
# Test
task.findUniqueChar(text1)
task.findUniqueChar(text2)
task.findUniqueChar(text3)
end
main()
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
import scala.collection.mutable._;
/*
Scala program for
Find unique character in a string
*/
class NonRepetitive()
{
def findUniqueChar(text: String): Unit = {
var n: Int = text.length();
if (n == 0)
{
return;
}
println("\n Given Text : " + text);
var result: Boolean = false;
var record: Array[Int] = Array.fill[Int](256)(0);
var i: Int = 0;
// Count frequency of character element
while (i < n)
{
record(text.charAt(i)) = record(text.charAt(i)) + 1;
i += 1;
}
i = 0;
while (i < n)
{
if (record(text.charAt(i)) == 1)
{
// When [i] location character appears once
print(" " + text.charAt(i));
result = true;
}
i += 1;
}
if (result == false)
{
print(" No result");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: NonRepetitive = new NonRepetitive();
// Given Sting text
var text1: String = "office";
var text2: String = "error";
var text3: String = "xxxx";
// Test
task.findUniqueChar(text1);
task.findUniqueChar(text2);
task.findUniqueChar(text3);
}
}
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
import Foundation;
/*
Swift 4 program for
Find unique character in a string
*/
class NonRepetitive
{
func findUniqueChar(_ data: String)
{
let n: Int = data.count;
if (n == 0)
{
return;
}
print("\n Given Text : ", data);
let text = Array(data);
var result: Bool = false;
var record: [Int] = Array(repeating: 0, count: 256);
var i: Int = 0;
var index = 0;
// Count frequency of character element
while (i < n)
{
index = Int(UnicodeScalar(String(text[i]))!.value);
record[index] = record[index] + 1;
i += 1;
}
i = 0;
while (i < n)
{
index = Int(UnicodeScalar(String(text[i]))!.value);
if (record[index] == 1)
{
// When [i] location character appears once
print(" ", text[i], terminator: "");
result = true;
}
i += 1;
}
if (result == false)
{
print(" No result", terminator: "");
}
}
}
func main()
{
let task: NonRepetitive = NonRepetitive();
// Given Sting text
let text1: String = "office";
let text2: String = "error";
let text3: String = "xxxx";
// Test
task.findUniqueChar(text1);
task.findUniqueChar(text2);
task.findUniqueChar(text3);
}
main();
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
/*
Kotlin program for
Find unique character in a string
*/
class NonRepetitive
{
fun findUniqueChar(text: String): Unit
{
val n: Int = text.length;
if (n == 0)
{
return;
}
println("\n Given Text : " + text);
var result: Boolean = false;
val record: Array < Int > = Array(256)
{
0
};
var i: Int = 0;
// Count frequency of character element
while (i < n)
{
record[text.get(i).toInt()] = record[text.get(i).toInt()] + 1;
i += 1;
}
i = 0;
while (i < n)
{
if (record[text.get(i).toInt()] == 1)
{
// When [i] location character appears once
print(" " + text.get(i));
result = true;
}
i += 1;
}
if (result == false)
{
print(" No result");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: NonRepetitive = NonRepetitive();
// Given Sting text
val text1: String = "office";
val text2: String = "error";
val text3: String = "xxxx";
// Test
task.findUniqueChar(text1);
task.findUniqueChar(text2);
task.findUniqueChar(text3);
}
Output
Given Text : office
o i c e
Given Text : error
e o
Given Text : xxxx
No result
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