110k views
5 votes
Write a program that removes all spaces from the given input. You may assume that the input string will not exceed 50 characters. Ex: If the input is: Hello my name is John. the output is: HellomynameisJohn. Your program must define and call the following function. userString is the user specified string. The function assigns userStringNoSpaces with the user specified string without spaces. void RemoveSpaces(char userString[], char userStringNoSpaces[]) Note: This is a lab based on a previous chapter that now requires the use of a function.

User Lizarisk
by
6.3k points

2 Answers

2 votes

Answer:

Please see attachment

Step-by-step explanation:

Please see attachment

Write a program that removes all spaces from the given input. You may assume that-example-1
User Jasna
by
5.1k points
4 votes

Answer:

Program that removes all spaces from the given input

Step-by-step explanation:

// An efficient Java program to remove all spaces

// from a string

class GFG

{

// Function to remove all spaces

// from a given string

static int removeSpaces(char []str)

{

// To keep track of non-space character count

int count = 0;

// Traverse the given string.

// If current character

// is not space, then place

// it at index 'count++'

for (int i = 0; i<str.length; i++)

if (str[i] != ' ')

str[count++] = str[i]; // here count is

// incremented

return count;

}

// Driver code

public static void main(String[] args)

{

char str[] = "g eeks for ge eeks ".toCharArray();

int i = removeSpaces(str);

System.out.println(String.valueOf(str).subSequence(0, i));

}

}

User Fpe
by
4.9k points