public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
HashSet set = new HashSet();
int j = 0;
for (int i = 0; i < str.length(); i++) {
// System.out.println(str.charAt(i));
if (str.charAt(i) == ' ') {
set.add(str.substring(j, i+1));
j = i;
}
if (i == str.length() – 1) {
set.add(str.substring(j, i));
}
}
for (String s : set) {
System.out.print(s + " ");
}
}
}
//Write a function that removes all duplicate spaces from a sentence. Your program should ensure that only one space exists between words
#include
#include
int main(){
char s[200];
gets(s);
int len,i,k;
for(len=0;s[len]!=’\0′;len++);
for(i=1;i<len;){
if(s[i-1]==' ' && s[i]==' '){
for(k=i-1;k<len;k++){
s[k] = s[k+1];
}
}
else i++;
}
puts(s);
return 0;
Note : \\s in print statement is fixed thing for this code. To avoid ambiguity you can also take reference variable for String as str instead of s
String str = sc.nextLine();
System.out.println(s.replaceAll(“\\s+”,” “).trim());
#python3
def remspace(s):
lis=[]
wor = ”
for i in s+’ ‘:
if i!=’ ‘:
wor+=i
else:
if len(wor)>=1:
lis.append(wor)
wor=”
else:
continue
return ‘ ‘.join(lis)
st=str(input(‘enter the string:\t’))
print(remspace(st))
public static void main(String ard[]){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String n[] = s.split(” “);
System.out.print(String.join(” “,n));
}
import java.util.HashSet;
import java.util.Scanner;
/**
* prep45
*/
public class prep45 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
HashSet set = new HashSet();
int j = 0;
for (int i = 0; i < str.length(); i++) {
// System.out.println(str.charAt(i));
if (str.charAt(i) == ' ') {
set.add(str.substring(j, i+1));
j = i;
}
if (i == str.length() – 1) {
set.add(str.substring(j, i));
}
}
for (String s : set) {
System.out.print(s + " ");
}
}
}
python code
s=input().split()
for i in s:
print(i,end=” “)
#include
#include
int main()
{
char arr[100];
int k;
scanf(“%[^\n]%*c”,arr);
k=strlen(arr);
// printf(“%s”,arr);
for(int i=0;i<k;i++)
{
if(arr[i]!=' ' || arr[i+1]!=' ')
printf("%c",arr[i]);
}
return 0;
}
#Python3
st=[]
st1=input()
for i in st1.split():
st.append(i)
for i in st:
print(i,end=” “)
//Write a function that removes all duplicate spaces from a sentence. Your program should ensure that only one space exists between words
#include
#include
int main(){
char s[200];
gets(s);
int len,i,k;
for(len=0;s[len]!=’\0′;len++);
for(i=1;i<len;){
if(s[i-1]==' ' && s[i]==' '){
for(k=i-1;k<len;k++){
s[k] = s[k+1];
}
}
else i++;
}
puts(s);
return 0;
}
a=input()
b=a.split()
for i in b:
print(i,end=’ ‘)
By…Gunjan soni
Java Code:
import java.util.*;
public class Solution{
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String s = sc.nextLine();
System.out.println(s.replaceAll(“\\s+”,” “).trim());
}
}
Note : \\s in print statement is fixed thing for this code. To avoid ambiguity you can also take reference variable for String as str instead of s
String str = sc.nextLine();
System.out.println(s.replaceAll(“\\s+”,” “).trim());
def read_s(a,n):
for i in range(n):
if a[i]==” ” and a[i+1]==” “:
for j in range(i,n-1):
a[j]=a[j+1]
a[n-1]=””
print(“”.join(a))
a=list(input())
n=len(a)
read_s(a,n)
#python3
def remspace(s):
lis=[]
wor = ”
for i in s+’ ‘:
if i!=’ ‘:
wor+=i
else:
if len(wor)>=1:
lis.append(wor)
wor=”
else:
continue
return ‘ ‘.join(lis)
st=str(input(‘enter the string:\t’))
print(remspace(st))
python code :
string = input()
s = str()
for i in string.split():
s+=i+’ ‘
print(s)
int main ()
{
char sent[100];
gets(sent) ;
char *p;
p = strtok(sent, ” “) ;
while(p! = NULL)
{
printf(“%s “,p) ; //leave a space after %s
p = strtok(NULL, ” “) ;
}
return 0;