Thread: Complicating things?

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Complicating things?

    So basically what im trying to do is ...

    Open file, read that file, than try to find ..

    We or we and replace them with I, but not replace the cases where words contain We or we, such as Went, went, etc

    a and replace them with the, but not replace the cases where words contain a, such as ability, apparent, etc

    A and replace them with The, same for this ...

    If it were a simple replace, id just getline, and find the We, and just replace it with I but i cant just do that, can i?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    No, you can't. What you need to do is check the elements in the array immediately following and immediately preceding the search string and make sure that they're " ". Also, how are you planning on "replacing" these tokens? There's no easy "replace" function in standard C strings - you need to read the data into a new string element by element, and do the replacement at that time.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by sean_mackrory
    No, you can't. What you need to do is check the elements in the array immediately following and immediately preceding the search string and make sure that they're " ". Also, how are you planning on "replacing" these tokens? There's no easy "replace" function in standard C strings - you need to read the data into a new string element by element, and do the replacement at that time.
    Can't he use strstr?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Can't he use strstr?
    strstr() won't help much in this situation. You can use strstr() to find instances of "we" in a string, but you still need to check to make sure the characters around the "we" are whitespace (spaces, tabs, newline characters). Also you run into the problem of efficiency. You don't want your code to look like:

    Code:
    char *p;
    while(p = strstr(text,"we"))
    {
       /* handle situation */
       text = p + 1;
    }
    text = original_string;
    while(p = strstr(text,"a"))
    {
       etc...
    If you check the text character by character, your program will move much more smoothly.
    Code:
    char *p;
    for(p = text; *p != 0; ++p)
    {
       switch(*p)
       {
       case 'W':
       case 'w':
          /* Check if we are at a valid "we" or "We" */
          break;
       case 'a':
       case 'A':
          /* check if we are at a valid "a" or "A" */
          break;
    }

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    As mentioned, copy the changed text into a new buffer.

    strstr is good way to find the word you're looking for, then you need to test the characters before and after for whitespace, not SPACE. Do this with isspace().

    If you find a candidate for a change, append the text from the last changed position to the current position of "we" into the new buffer. Then append the conversion text, and remember the position after "we". Repeat until done.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    This isnt what im trying to do is it?

    Code:
    void replace(char *a)
    {
    	int     in_fd, out_fd, n_chars;
    	char	ch[BUFFERSIZE];
    	while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 )
    	(
    		do 
    		{
    			ch = getchar();
    		
    			if (ch = 'w')
    				(
    				if ((ispace(off_t lseek(int ch, off_t -1, int SEEK_CUR)) == 1 ) &&
    				off_t lseek(int ch, off_t 1, int SEEK_CUR)) == 'e' &&
    				(ispace(off_t lseek(int ch, off_t 2, int SEEK_CUR)) == 1 ))
    					{
    					
    				
    					}
    				)
    		
    			if (ch = 'W')
    				(
    				if ((ispace(off_t lseek(int ch, off_t -1, int SEEK_CUR)) == 1 ) &&
    				off_t lseek(int ch, off_t 1, int SEEK_CUR)) == 'e' &&
    				(ispace(off_t lseek(int ch, off_t 2, int SEEK_CUR)) == 1 ))
    					{
    				
    				
    					}
    				)
    		
    			if (ch = 'a')
    				(
    				if ((ispace(off_t lseek(int ch, off_t -1, int SEEK_CUR)) == 1 ) &&
    				(ispace(off_t lseek(int ch, off_t 1, int SEEK_CUR)) == 1 ))
    					{
    				
    				
    					}
    				)
    			
    			if (ch = 'A')
    				(
    				if ((ispace(off_t lseek(int ch, off_t -1, int SEEK_CUR)) == 1 ) &&
    				(ispace(off_t lseek(int ch, off_t 1, int SEEK_CUR)) == 1 ))	
    					{
    				
    				
    					}
    				)
    		}
    		while(a!=EOF)	
    	)
    		
    	 if ( write( out_fd, buf, n_chars ) != n_chars )
                 oops("Write error to ",av[1]);
                 
           if ( close(in_fd) == -1 || close(out_fd) == -1 )
              	oops("Error closing files","");
    
    }

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    No. It's not even close. It won't even compile.


    Code:
    if (ch = 'w')
    1) ch is defined as an array, not a sincle character
    2) this simply sets ch to 'w'

    lseek() is used to move around in files.
    getchar() is used to read from the keyboard.
    You can't lseek() the keyboard.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Quote Originally Posted by WaltP
    No. It's not even close. It won't even compile.


    Code:
    if (ch = 'w')
    1) ch is defined as an array, not a sincle character
    2) this simply sets ch to 'w'

    lseek() is used to move around in files.
    getchar() is used to read from the keyboard.
    You can't lseek() the keyboard.
    Something more like...

    Code:
    char *ch;
    
    ch = getchar();
    
    if (ch == 'w')
    (
    if ((ispace(*ch - 1) == 1 ) && (*ch + 1 == 'e') && (ispace(*ch + 2) == 1))
    					{
    					
    				
    					}
    				)

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bconnor
    Something more like...

    Code:
    char *ch;
    
    ch = getchar();
    
    if (ch == 'w')
    No, not at all. 'ch' is a pointer. It doesn't actually point to anything, and even if it did, the comparison to 'w' is wrong, as is the assignment from getchar. Try paying attention to your compiler some time.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You shouldn't be testing if your word is surrounded by whitespace.

    Code:
    We, a?
    would not be replaced. You'd want to test if the surrounding characters are not letters, numbering etc.

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    What you are trying to code is not going to work! Go back to our previous suggestions and use them.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Oki I will give you the solution cause I'm into a great mood take the C Programming Language Second Edition (must have or must read if you really need to know C) section 4.1 basic of functions there is a program that can do this trick for you. Well it need small modifications but read that section of the book and you will do the trick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for things to study
    By Mastadex in forum Windows Programming
    Replies: 5
    Last Post: 08-18-2008, 09:23 AM
  2. Funny things happening outside of function
    By drb2k2 in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 02:26 PM
  3. things to think about... [from www.nuclearwastesite.com]
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-11-2001, 12:47 AM
  4. What is your favorite things about programming?
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 10-21-2001, 12:13 PM
  5. Help with these three things...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2001, 07:05 AM