Print All Unique Words Of A String

Description

To print unique word from a string
    Step 1: Split the string from space and store it into a array
    of string to get all words.
    Step 2: Take two loops one loop is for traversing the array of string.
    Step 3: Second loop is for checking the duplicate elements, if duplicate
    element found break the second loop.
    Step 4: If loop reached to end, print the word.

Example: str : this is java and java is good
         output : this 
                  is
                  java
                  and
                  good

C/C++

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* C program to print unique word of a string*/
//Save it as PrintUniqueWord.c
#include<stdio.h>
int main(){
int i, j=0, k=0;
char str[100];
char newStr[10][20];
printf("Enter a string : ");
scanf("%[^\n]s", str);
//Traversing the string
for(i=0;str[i]!='\0';i++){
//checking for space
if(str[i]==' '){
newStr[k][j]='\0';
k++;
j=0;
}else{
newStr[k][j]=str[i];
j++;
}
}
newStr[k][j]='\0';
printf("The unique word of this string : \n");
for(i=0;i<=k;i++){
for(j=0;j<=k;j++){
//Storing the string value into temporary variable
char *temp1 = newStr[i];
char *temp2 = newStr[j];
/*if second loop encounter same value again but j<i break the loop to
avoid printing of duplicate value*/
if(j<i && (strcmp(temp1,temp2) == 0)) {
break;
}
/*While traversing the array, if loop reached to last print the
string with its number of count*/
if(j==k) {
printf("%s\n",newStr[i]);
}
}
}
return 0;
}
/* C program to print unique word of a string*/ //Save it as PrintUniqueWord.c #include<stdio.h> int main(){ int i, j=0, k=0; char str[100]; char newStr[10][20]; printf("Enter a string : "); scanf("%[^\n]s", str); //Traversing the string for(i=0;str[i]!='\0';i++){ //checking for space if(str[i]==' '){ newStr[k][j]='\0'; k++; j=0; }else{ newStr[k][j]=str[i]; j++; } } newStr[k][j]='\0'; printf("The unique word of this string : \n"); for(i=0;i<=k;i++){ for(j=0;j<=k;j++){ //Storing the string value into temporary variable char *temp1 = newStr[i]; char *temp2 = newStr[j]; /*if second loop encounter same value again but j<i break the loop to avoid printing of duplicate value*/ if(j<i && (strcmp(temp1,temp2) == 0)) { break; } /*While traversing the array, if loop reached to last print the string with its number of count*/ if(j==k) { printf("%s\n",newStr[i]); } } } return 0; }
/* C program to print unique word of a string*/
//Save it as PrintUniqueWord.c

#include<stdio.h>
int main(){

    int i, j=0, k=0;

    char str[100];
    char newStr[10][20];

    printf("Enter a string : ");
    scanf("%[^\n]s", str);
    
    //Traversing the string
    for(i=0;str[i]!='\0';i++){
        
        //checking for space
        if(str[i]==' '){
            newStr[k][j]='\0';
            k++;
            j=0;
        }else{
            newStr[k][j]=str[i];
            j++;
        }
    }
    newStr[k][j]='\0';

    printf("The unique word of this string : \n");
    for(i=0;i<=k;i++){
        for(j=0;j<=k;j++){
            
            //Storing the string value into temporary variable
            char *temp1 = newStr[i];
            char *temp2 = newStr[j];
            
            /*if second loop encounter same value again but j<i break the loop to
            avoid printing of duplicate value*/
            if(j<i && (strcmp(temp1,temp2) == 0)) {
                break;
            }
            
            /*While traversing the array, if loop reached to last print the
            string with its number of count*/
            if(j==k) {
                printf("%s\n",newStr[i]);
            }
        }
    }

    return 0;
}
Input:
Enter a string : 
this is c and c is good

Output:
The unique word of this string is :
this
is
c
and
good

Java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* Java program to print unique word of a string*/
//Save it as PrintUniqueWord.java
import java.io.*;
import java.util.Scanner;
public class PrintUniqueWord {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string : ");
String str = scanner.nextLine();
//Splitting the string by space and storing into string array
String newStr[] = str.split(" ");
System.out.println("The unique word of this string is :");
for(int i=0;i<newStr.length;i++) {
for(int j=0;j<newStr.length;j++) {
//Storing the string value into temporary variable
String temp1 = newStr[i];
String temp2 = newStr[j];
/*if second loop encounter same value again but j<i break the loop to
avoid printing of duplicate value*/
if(j<i && temp1.equals(temp2)) {
break;
}
/*While traversing the array, if loop reached to last print the
string with its number of count*/
if(j==newStr.length-1) {
System.out.println(newStr[i]);
}
}
}
}
}
/* Java program to print unique word of a string*/ //Save it as PrintUniqueWord.java import java.io.*; import java.util.Scanner; public class PrintUniqueWord { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a string : "); String str = scanner.nextLine(); //Splitting the string by space and storing into string array String newStr[] = str.split(" "); System.out.println("The unique word of this string is :"); for(int i=0;i<newStr.length;i++) { for(int j=0;j<newStr.length;j++) { //Storing the string value into temporary variable String temp1 = newStr[i]; String temp2 = newStr[j]; /*if second loop encounter same value again but j<i break the loop to avoid printing of duplicate value*/ if(j<i && temp1.equals(temp2)) { break; } /*While traversing the array, if loop reached to last print the string with its number of count*/ if(j==newStr.length-1) { System.out.println(newStr[i]); } } } } }
/* Java program to print unique word of a string*/
//Save it as PrintUniqueWord.java

import java.io.*;
import java.util.Scanner;

public class PrintUniqueWord {

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter a string : ");
        String str = scanner.nextLine();
        
        //Splitting the string by space and storing into string array
        String newStr[] = str.split(" ");
        
        System.out.println("The unique word of this string is :");
        for(int i=0;i<newStr.length;i++) {
            for(int j=0;j<newStr.length;j++) {
                
                //Storing the string value into temporary variable
                String temp1 = newStr[i];
                String temp2 = newStr[j];
                
                /*if second loop encounter same value again but j<i break the loop to 
                avoid printing of duplicate value*/
                if(j<i && temp1.equals(temp2)) {
                    break;
                }
                
                /*While traversing the array, if loop reached to last print the
                string with its number of count*/
                if(j==newStr.length-1) {
                    System.out.println(newStr[i]);
                }
            }
        }
    }
}
Input:
Enter a string : 
this is java and java is good

Output:
The unique word of this string is :
this
is
java
and
good

Related Programs

1) Program To Print Unique Word Of A String Using Set
2) Count Number of Words in a String
3) Find Occurrence Of Each Word In a Sentence
4) Program to Replace Spaces With Dots
5) Program to remove vowels from a String
6) Program to copy string
7) Find Substring of a Given String
8) Program to Reverse a String
9) Program to Compare Two Strings
10) How to find Length or Size of a String
Share Me

Leave a Reply