Ive tried changing it to pointer format but when i do, the new string without spaces doesnt appear. i appreciate the help!
[/RIGHT][/CENTER][/LEFT]
int main()
{
char ar[50];
cout << "enter a sentence: " <<endl;
cin.getline(ar, 49);
cout<<" your sentence has " <<stripspaces(a r)<<" spaces "<<endl;
return 0;
}
int stripspaces (char str[])
{
int spacecounter = 0;
char * p =str;
while (*p)
{
if (*p == ' ')
spacecounter++;
p++;
}
int i =0;
char temp[50];
int count= 0;
while (str[i])
{
if(str[i]!= ' ')
{
temp[count] = str[i];
count++;
}
i++;
}
temp[count] = '\0';
cout <<" The new string without spaces is:" <<temp<<endl;
return spacecounter;
}[/CODE]
Code:
#include <iostream> int stripspaces(char str[]); using namespace std; [LEFT][CENTER][RIGHT][CODE]
int main()
{
char ar[50];
cout << "enter a sentence: " <<endl;
cin.getline(ar, 49);
cout<<" your sentence has " <<stripspaces(a r)<<" spaces "<<endl;
return 0;
}
int stripspaces (char str[])
{
int spacecounter = 0;
char * p =str;
while (*p)
{
if (*p == ' ')
spacecounter++;
p++;
}
int i =0;
char temp[50];
int count= 0;
while (str[i])
{
if(str[i]!= ' ')
{
temp[count] = str[i];
count++;
}
i++;
}
temp[count] = '\0';
cout <<" The new string without spaces is:" <<temp<<endl;
return spacecounter;
}[/CODE]
Comment