0
how to ignore space in string program in c++
6 ответов
+ 1
For both space and punctuation
#include <iostream>
using namespace std;
int main() {
string text = "this. is my string. it's here.";
for (int i = 0, len = text.size(); i < len; i++){
if (!isalnum(text[i])){
text.erase(i--, 1);
len = text.size();
}
}
cout << text;
return 0;
}
+ 1
For only spaces use
#include <iostream>
using namespace std;
int main() {
string input = "this. is my string. it's here.";
for (int i = input.length()-1; i >= 0;--i){
if(input[i] == ' ')
input.erase(i, 1);
}
cout << input;
return 0;
}
0
You can use setfill() method to set character to empty spaces like use
setfill('');
it will remove all spaces. You have to include a header file called
#include<iomanip>
Moreover can you give me a sample program. Then I can help you better.
0
i use it in palindrome program i want to ignore spaces abg functuation in my program
0
you want to remove punctuation and spaces right?
0
how to ignore spaxe and punctuation in thus program
include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
char pal ( char word[80] )
{
int l,i,testpalindrome;
l=strlen(word);
if(l>0)
{
for (i=0;i<=(l);i++)
if (word[1]!=word[l-1-1])
testpalindrome=1; }
return testpalindrome;}
void main ()
{
clrscr ();
char word[80];
int hold;
cout<<"Enter a word : ";
cin>>word;
hold=pal(word) ;
if (hold!=1)
{
cout<<"the word is palindrome"; }
else
{
cout<<"The word is NOT palindrome ";}
getch();
}