Write a program to remove all the blank space from the string and print it, also count the number of characters
Example:
Input: str[]="Hello World" Output: String after remove space is HelloWorld Number of characters are 10
Approach
C
#include <stdio.h>int main(){char str[] = "Hello World";int n = sizeof(str) / sizeof(str[0]);int l = 0;for (int i = 0; i < n; i++){if (str[i] == ' '){continue;}else{str[l] = str[i];l++;}}printf("String after remove space is %s\n", str);printf("Number of characters are %d\n", l-1);return 0;}
Java
public class RemoveBlankSpace {public static void main(String[] args) {String st = "Hello World";char str[] = st.toCharArray();int n = str.length;StringBuffer sb = new StringBuffer();for (int i = 0; i < n; i++) {if (str[i] == ' ') {continue;} else {sb.append(str[i]);}}System.out.println("string after removal of space " + sb.toString());System.out.println("number of character " + sb.length());}}
No comments:
Post a Comment