remove characters from a string


I am a newer, and now i encounter a simple question.

i want to erase all specified characters ,such as ',' , from a string.
which function should i call
for example:

string ls( "i ,am, a ,dog,");

thanks in advace

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Posted On: Monday 5th of November 2012 02:31:24 AM Total Views:  214
View Complete with Replies

RELATED TOPICS OF C Language PROGRAMMING LANGUAGE




How to remove Segmentation Fault

The following code can detect a (single) macro name (stored in str1) from a file and store its definition (stored in str2) and then reads the file again to write into a new file, replacing all occurrences of the macro name with its definition. The code when executed gives segmentation fault, please help me remove it. the mac() is working properly, there's a problem with the main(). Please help. Code: #include #include #include FILE *fp1, *fp2; int i; char str[10]; char str1[10],str2[10]; void mac (void) { char ch; fp1=fopen("comm.txt","r"); do{ ch=fgetc(fp1); if(ch!='\n') { while(ch!='\n') { ch=fgetc(fp1); } } ch=fgetc(fp1); if (ch=='#') { ch=fgetc(fp1); if(ch==' ') { i=0; ch=fgetc(fp1); while (ch!=' ') { str1[i++]=ch; ch=fgetc(fp1); } str1[i]='\0'; } //puts(str1); if(!(strcmp(str1,"define"))) { i=0; ch=fgetc(fp1); while(ch!=' ' && ch!='\n') { str2[i++]=ch; ch=fgetc(fp1); } str2[i]='\0'; } } } while(ch!= EOF); fclose(fp1); } int main(void) { char ch; int flag=1; fp1=fopen("comm.txt","r"); fp2=fopen("new.txt","w"); mac(); do{ if(flag==0) { ch=fgetc(fp1); } if (ch!='#') { while(ch!='\n' && ch!=';') { i=0; while(ch!=' ' && ch!='\n' && ch!=';') { str[i++]=ch; ch=fgetc(fp1); } str[i]='\0'; //ch=fgetc(fp1); if (!(strcmp(str,str1))) fputs(str2,fp2); else fputs(str,fp2); fputc(ch,fp1); ch=fgetc(fp1); } fputc(ch,fp1); if (ch=='\n') flag=1; else flag =0; } else { while(ch!='\n') { fputc(ch,fp2); ch=fgetc(fp1); } fputc(ch,fp2); flag=1; } }while(ch!=EOF); fclose(fp1); fclose(fp2); return EXIT_SUCCESS; }
VIEWS ON THIS POST

174

Posted on:

Wednesday 19th September 2012
View Replies!

Compare two string and remove common letters

Helo Cprogramming members, I am new to programming. I started lessons at c in my university and i am having trouble with an exercise. Ok here is what exercise asks for: "Write a program that aids with appropriate messages on the screen to read two character strings str1 and str2 even which will be given from the keyboard and then delete all the letters in the variable str1, which appear in the variable str2. The display shows the final result for checking the correct operation of the program." For example: string1: eleos string2: lo final result: e e s Here is what i have done, but its not working properly.. Note that i can use only that libraries. Code: #include #include #include int main() { char str1[80], str2[80]; int megethos1, megethos2,max,i,j; printf ("Give first string: "); scanf ("%s", &str1); printf ("Give second string: "); scanf ("%s", &str2); size1= strlen(str1); size2= strlen(str2); for (j=0; j
VIEWS ON THIS POST

208

Posted on:

Saturday 6th October 2012
View Replies!

How to remove Segmentation Fault

The following code can detect a (single) macro name (stored in str1) from a file and store its definition (stored in str2) and then reads the file again to write into a new file, replacing all occurrences of the macro name with its definition. The code when executed gives segmentation fault, please help me remove it. the mac() is working properly, there's a problem with the main(). Please help. Code: #include #include #include FILE *fp1, *fp2; int i; char str[10]; char str1[10],str2[10]; void mac (void) { char ch; fp1=fopen("comm.txt","r"); do{ ch=fgetc(fp1); if(ch!='\n') { while(ch!='\n') { ch=fgetc(fp1); } } ch=fgetc(fp1); if (ch=='#') { ch=fgetc(fp1); if(ch==' ') { i=0; ch=fgetc(fp1); while (ch!=' ') { str1[i++]=ch; ch=fgetc(fp1); } str1[i]='\0'; } //puts(str1); if(!(strcmp(str1,"define"))) { i=0; ch=fgetc(fp1); while(ch!=' ' && ch!='\n') { str2[i++]=ch; ch=fgetc(fp1); } str2[i]='\0'; } } } while(ch!= EOF); fclose(fp1); } int main(void) { char ch; int flag=1; fp1=fopen("comm.txt","r"); fp2=fopen("new.txt","w"); mac(); do{ if(flag==0) { ch=fgetc(fp1); } if (ch!='#') { while(ch!='\n' && ch!=';') { i=0; while(ch!=' ' && ch!='\n' && ch!=';') { str[i++]=ch; ch=fgetc(fp1); } str[i]='\0'; //ch=fgetc(fp1); if (!(strcmp(str,str1))) fputs(str2,fp2); else fputs(str,fp2); fputc(ch,fp1); ch=fgetc(fp1); } fputc(ch,fp1); if (ch=='\n') flag=1; else flag =0; } else { while(ch!='\n') { fputc(ch,fp2); ch=fgetc(fp1); } fputc(ch,fp2); flag=1; } }while(ch!=EOF); fclose(fp1); fclose(fp2); return EXIT_SUCCESS; }
VIEWS ON THIS POST

131

Posted on:

Tuesday 16th October 2012
View Replies!

remove redundant white spaces in string

is a strange request. I don't need a code. could somebody please explain me how it has to be done step by step. I just need the logic in for task. , lets say i have a string Code: "this is a string with redundant spaices in it" I don't have any algo in my mind yet, O, I see. My bad. I meant if more than one white space among two words = redundancy. Answer A in your assumptions , damn what I have done to curse me like that Anyway thank you for hint and help., But I did exlained so you can see. Look at your post and examples. Which particular example you typed first , We are all getting stressed from time to time, like me now. I just don't follow in this part of pseudo code. Code: r = 0//got it w = 0/got it while string[r] is not a null character // that is ok string[w] = string[r]//yep all the steps to copy one increment r//string if string[w] is a white space character // right, lets say it meets ' ' while string[r] is a white space character//here I'm stack increment r //what for increment w// to another string[w] = null character // well yes, it has to end somewhere ,
VIEWS ON THIS POST

134

Posted on:

Saturday 20th October 2012
View Replies!

remove non-existent element in the std::set

if I provide a key of element that doesn't exist in the set, and I call set.erase(nonInSetKey), does standard guarantee that nothing "BAD" will happen, in fact, nothing at all will happen. Does it make sense to check first whether key is in the set before removing it
VIEWS ON THIS POST

132

Posted on:

Saturday 3rd November 2012
View Replies!

how to remove warning clock skew detected

, when i use the make file given below i get the warning message.. clock skew detected your build may be incomplete.how can i remove this warning pls help me.. the make file which i use is ... obj-M:=hello.o all: make -C/lib/modules/$(shellUname-r)/build M=$(PWD)modules clean: make -C/lib/modules/$(shellUname-r)/build M=$(PWD)clean
VIEWS ON THIS POST

199

Posted on:

Saturday 3rd November 2012
View Replies!

Accidentally removed a file from project

I accidentally removed a source file from the project i was working on. Is there anyway to recover this file I am using Dev-C.
VIEWS ON THIS POST

148

Posted on:

Saturday 3rd November 2012
View Replies!

std::list remove element mid iteration

The situation is that a std::list is being iterated through. Upon certain criteria some sets become empty. I need to remove the empty sets from the list. Is it safe to iterate through a list and call list::erase( iterator ) in mid iteration
VIEWS ON THIS POST

180

Posted on:

Sunday 4th November 2012
View Replies!

Re: Why won't this list remove_if code compile

"Angus" wrote: > I have a class CRequest with a function: > bool IsFinished() const; > > I have a list of these CRequests - list mylist. > > I create a function object to find if a CRequest is finished: > class is_finished : public std::unary_function > { > public: > bool operator() (CRequest req) { return req.IsFinished(); } > }; > > And I use remove_if like this: > m_RequestList.remove_if(is_finished()); > > But I get a compile error: > error C2664: 'remove_if' : cannot convert parameter 1 from 'class > is_finished' to 'class std::binder2nd >' > No constructor could take the source type, or constructor overload > resolution was ambiguous > > How can I fix this Try this: class CRequest { public: bool isFinished() const; }; void fn( list lst ) { lst.remove_if( mem_fun_ref( &CRequest::isFinished ) ); }
VIEWS ON THIS POST

179

Posted on:

Sunday 4th November 2012
View Replies!

Unicode to characters

all, There could be flavors of this question discussed in the past, but I could not really make a head/tail out of it. I have bunch of unicode values stored in a string array and I want to see the corresponding characters displayed in an excel file. How could I go about doing that vector unicodevalues; // has values 0041, 0042, ... 0410 etc. (hexa decimal values) for 0041 (assumes hex) I should see alphabet 'A' , a 'B' for 0042 ... special character corresponding to 0x410. I could live with a comma separated .csv file instead of a .xls to view it in excel. Please advice.
VIEWS ON THIS POST

314

Posted on:

Sunday 4th November 2012
View Replies!

remove_copy

Dear all, it may be a friday afternoon issue, but I was looking for a remove variant which removed elements from a vector, but not throwing them away in the first place, but keeping a copy so that I still could do things with the removed elements before actually destroy them. So in the STL you have the 'remove(_if)' function which removes elements. However the 'remove_copy' does not remove elements form the original input! It has more the semantic of a 'copy_if_not' function (also implicitly pointed out by Austern, by implementing 'copy_if' in terms of 'remove_copy_if', see 12.6.4 of Generic Programming && STL). Since the STL already exist for more than a decade, it must be my own mind which interprets the function name wrong. Or it is indeed a friday afternoon issue. Of course human language is multiple interpretable and English is not my native language, so the original c+ + committee members may be 'right' after all. Above functionality is solvable btw by stable_partition as someone pointed out in CUJ. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
VIEWS ON THIS POST

143

Posted on:

Sunday 4th November 2012
View Replies!

the usage of remove function in c++

can anyone tell me the usage of remove,include its' return value,prototype,a example may as well.
VIEWS ON THIS POST

144

Posted on:

Sunday 4th November 2012
View Replies!

Remove first two characters from char* or from int?

I have a char *year that contains a 4-digit year (i.e 1929), I use the following syntax c = atoi(year); to convert it to an integer. However I must strip the first two numbers. I've tried to convert it from char* to a string and use the erase function, which works, but then I can't seem to convert the string it to an integer. I am impartial as to when I remove the first two digits, meaning it can be done before or after the conversion to int. In my example above the end result should be an integer variable equalling 29. , yogi_bear_79 wrote: >> c = atoi(year+2); > Perfect!
VIEWS ON THIS POST

273

Posted on:

Sunday 4th November 2012
View Replies!

Newbie Question (take it easy on me fellas) - Creating customASCII-style characters

Please don't bite me to much with the following post, (a little nibbling doesn't hurt though). When I was a child I owned an Amstrad CPC6128 home computer and liked to fiddle around with the BASIC that came with it. One of my favourite commands was (memory permitting) something like this; DATA xyz$ (1, 1, 1, 1, 0, etc) this allowed you to design your own ASCII-style characters in an 8x8 gird with 1's representing a white dot and 0 not. You could then call this as you would any other single character. I'm currently learning C++ (albeit from a book) and was wondering if there was a similar command for use in console style applications. I hope that you are able to help me with this and don't bite me to much.
VIEWS ON THIS POST

221

Posted on:

Sunday 4th November 2012
View Replies!

copy unknown number of characters from cstring

Let's say I have a cstring: char str[] = " world"; and I have a needle: char n[] = "wo"; I find that the needle starts at the 5+1 position of the string. How am I going to copy the characters up to this point, using the header -- Using (c++ string) it is possible in two ways: 1) for (int counter = 0; i < str.find(n); i++) tmp += str[i]; 2) str.substr();
VIEWS ON THIS POST

193

Posted on:

Sunday 4th November 2012
View Replies!

Triming characters off the end of a stream

I have a strstream, which has a few characters at the end of the stream ')|' to be exact, I want to remove them form the stream, but SetLength doesn't work on strstream, and I can't find any other way to turnicate the stream. - Dan
VIEWS ON THIS POST

170

Posted on:

Sunday 4th November 2012
View Replies!

How can I fix the warning C4786 'identifier' : identifier wastruncated to 'number' characters in the debug information...?

I have a MyIOManager Object. In this object I have a ""typedef map cmdMap"" I also have a CommandManager & Command Objects. // MyIOManager class .... typedef map commandMap; .... class MyIOManager { .... commandMap cmdMap; CommandManager * cmdManager; Command * cmd .... } MyIOManager::StartIO() { .... cmd = cmdManager->CatchCommand( cmdMap ); .... } // CommandManager class Command * CommandManager::CatchCommand( ... ) { .... // return a Command *... } But I get too many warnings like the following: What is wrong How can I fix it I checked the MSDN Library... there is written that I should shorten the Identifier names..., But I don't understand, I don't have at all long name more than 255 characters...
VIEWS ON THIS POST

246

Posted on:

Monday 5th November 2012
View Replies!

please remove these errors

Write a program which overloads a binary Minus (+) operator, The program will contain a class Matrix, This class will contain a private data member Array[][] which store int values. The class will further contain a Default constructor, get() function which takes values for array from the user and also contain a Display function witch display the array on the screen, In main function create three objects Mat1, Mat2, Mat3 of this class, first call get() and Display() functions with Mat1 and Mat2 objects then implement the statement Mat3 = Mat1 + Mat2; and call Display() function with Mat3. #include #include #include class Matrix { private : int numRows, numCols ; int elements [30] [30] ; public : Matrix( int rows , int cols ) ; void getMatrix ( ) ; void displayMatrix ( ) ; Matrix(); Matrix::Matrix operator + (Matrix); }; Matrix :: Matrix ( int rows = 0 , int cols = 0) { numCols = cols ; numRows = rows ; for ( int i = 0 ; i < numRows ; i ++ ) { for ( int j = 0 ; j < numCols ; j ++ ) { elements [ i ] [ j ] = 0 ; } } } void Matrix :: getMatrix ( ) { for ( int i = 0 ; i < numRows ; i ++ ) { for ( int j = 0 ; j < numCols ; j ++ ) { cin >> elements [ i ] [ j ] ; //cout
VIEWS ON THIS POST

162

Posted on:

Monday 5th November 2012
View Replies!

std::list: remove from front without deleting or looping through whole list

, I have the following situation: Thread A is allocating a dataset, doing some low-level calculations and storing a pointer to the dataset in a std::list via push_back. Thread B should retrieve the pointer to the first dataset in the list, remove it from the list, and do some high level ****ysis. The problem now is, how do I efficiently retrieve the pointer Calling front() and then pop_front() deletes the dataset, i.e. calls its destructor, so this is not good, since I don't want to copy the datasets before calling pop_front(), because they are very large... Calling front() and remove(mylist.front()) does the job, but loops through the whole list and is thus also not very efficient.. Is there any efficient way to retrieve the first object from the list, and remove the object then from the list, without looping through the whole list or deleting the object If anybody has a good answer to the problem, I would really appreciate to hear it.
VIEWS ON THIS POST

181

Posted on:

Monday 5th November 2012
View Replies!

accessing the characters of a word

In the following code, how do I get the program to print out each letter of the word that *delete is pointing to separately I need to access each letter separately. Rather I need to access the last letter of the word. char *delete = "CProgramming"; int i; for(i=0;i
VIEWS ON THIS POST

221

Posted on:

Monday 5th November 2012
View Replies!