Description
To replace all spaces with dots, loop through string, when spaces encountered replace with dots.
C/C++
/* C program to Replace Spaces With Dots */ //Save it as ReplaceSpaceWithDot.c #include<stdio.h> #include<string.h> int main(){ char str[100]; int i; printf("Enter the string : "); gets(str); for(i=0;i<strlen(str);i++) { if(str[i] == ' ') { str[i] = '-'; } } printf(str); return 0; }
Input: Enter the string : I love You and your family Output: String after replacing spaces with dots : I-love-You-and-your-family
Java
/* Java program to Replace Spaces With Dots */ //Save it as ReplaceSpaceWithDot.java import java.io.*; import java.util.Scanner; public class ReplaceSpaceWithDot { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the string : "); String str = scanner.nextLine(); char myStr[] = str.toCharArray(); for(int i=0;i<myStr.length;i++) { if(myStr[i] == ' ') { myStr[i] = '-'; } } /*Predefined method in java to replace all occurances*/ //String myStr = str.replaceAll(" ", "-"); System.out.println("String after replacing spaces with dots : "); System.out.println(myStr); } }
Input: Enter the string : I love You and your family Output: String after replacing spaces with dots : I-love-You-and-your-family
Related Programs
1) Program to remove vowels from a String2) Program to Multiply Numbers Present in a String
3) Program to Add numbers present in a String
4) Program to remove given number from a string
5) Program to count Vowels, Consonants, Digits and Whitespaces
6) Count Number of Words in a String
7) Find Occurrence Of Each Word In a Sentence
8) Print All Unique Words Of A String
9) Program To Print Unique Word Of A String Using Set
10) Program to find maximum number of 1s in a substring of a String
11) check strings are equal or not after concatenation of array of strings with other array of strings