
Vinayak P. answered 04/12/20
Senior Software Engineer
Hi you want skip all spaces.
for
(
int
i = 0; str[i]; i++)
if
(str[i] !=
' '
)
str[count++] = str[i];
Oscar A.
asked 03/30/20Write a program that removes all spaces from the given input.
Ex: If the input is:
the output is:
Your program must define and call the following function. The function should return a string representing the input string without spaces.
string RemoveSpaces(string userString)
#include <iostream>
using namespace std;
/* Define your function here */
int main() {
/* Type your code here. Your code must call the function. */
return 0;
}
I need help please
Vinayak P. answered 04/12/20
Senior Software Engineer
Hi you want skip all spaces.
for
(
int
i = 0; str[i]; i++)
if
(str[i] !=
' '
)
str[count++] = str[i];
Patrick B. answered 03/31/20
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define BLANK_CHAR (' ')
int CountCharacters(char userChar, char * userString)
{
int countReturn=0;
int n = strlen(userString);
for (int iLoop=0; iLoop<n; iLoop++)
{
if (userString[iLoop]==userChar)
{
countReturn++;
}
}
return(countReturn);
}
/******************************************
Removes white spaces from passed string; returns pointer
to the string that is stripped of the whitespace chars;
Returns NULL pointer is empty string is passed;
Side Effects:
CALLER MUST FREE THE OUTPUT BUFFER that is returned
**********************************************************/
char * RemoveSpaces(char * userString)
{
char * outbuff = NULL;
if (userString!=NULL)
{
int n = strlen(userString);
outbuff = (char *) malloc(n);
if (outbuff != NULL)
{
memset(outbuff,0,n);
int iIndex=0;
//copies non-blank chars to outbuff
for (int iLoop=0; iLoop<n; iLoop++)
{
if (userString[iLoop]!=BLANK_CHAR)
{
outbuff[iIndex]=userString[iLoop];
iIndex++;
}
} //for
}
}
return(outbuff);
}
int main()
{
char inbuff[255];
cout << " PLEASE INPUT THE STRING OF WHICH YOU WOULD LIKE TO STRIP WHITESPACE CHARS :>";
gets(inbuff);
char * outbuff = RemoveSpaces(inbuff);
if (outbuff !=NULL)
{
cout << ">" << outbuff << "<" << endl;
free(outbuff);
}
memset(inbuff,0,255);
cout << " PLEASE INPUT THE STRING IN WHICH YOU WOULD LIKE TO SEARCH CHAR :>";
gets(inbuff);
char chChar;
cout << "PLEASE INPUT THE CHARCTER YOU SEEK :>";
cin >> chChar;
int iCount = CountCharacters(chChar,inbuff);
cout << " char " << chChar << " appears " << iCount << " time(s) in >" << inbuff << "<" << endl;
}
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.