how to ignore space in string program in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to ignore space in string program in c++

14th Nov 2016, 6:40 AM
KG Feignh
KG Feignh - avatar
6 Answers
+ 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; }
14th Nov 2016, 7:49 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 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; }
14th Nov 2016, 7:55 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
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.
14th Nov 2016, 7:18 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
0
i use it in palindrome program i want to ignore spaces abg functuation in my program
14th Nov 2016, 7:24 AM
KG Feignh
KG Feignh - avatar
0
you want to remove punctuation and spaces right?
14th Nov 2016, 7:30 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
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(); }
14th Nov 2016, 5:08 PM
KG Feignh
KG Feignh - avatar