Thread: Squeezing a set of string

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    Squeezing a set of string

    Hi,

    I'm having some trouble trying to create a code for squeezing characters.
    Here's what I am trying to do.

    Here's an example:
    sample string:
    squeeze guess pinning yeeeess squeeze -ens

    Output:
    squeze gues pining yes

    -this program squeezes repeating letters to one set of letters

    This is a code that I found on the net, but unfortuantely it doesn't help with my program.
    Code:
    #include <stdio.h>
    
    
    void squeeze(char s[], char s2[]);
    
    int main(){
    
    char string[] = "this is a test";
    char remove[] = "tesa";
    
    squeeze(string, remove);
    printf("%s\n", string);
    
    return 0;
    }
    
    
    void squeeze(char s[], char s2[])
    {
      int i = 0, j = 0, k = 0;
    
      while (s2[k] != '\0')
      {
          for (i = j = 0; s[i] != '\0'; i++)
          {
            if (s[i] != s2[k])
             {
                s[j++] = s[i];
             }
          }
          k++;
          s[j] = '\0';
       }
     }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess this code does something different.

    You'll probably need to loop over the original string. If the next character is different than the last character copied to the output string, copy it, otherwise don't copy.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Yeah, the fonction squeeze(..) you posted is, and you might have discover it, removing all the characters in the string s that are in the string s2.

    This fonction has really non-explicit variable name by the way.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    Here's my new code:

    but I have some problems in replacing a specific text. It only replaces the first character of a string.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <limits.h>
    
    void word();
    void character(FILE *fp);
    void line(FILE *fp);
    
    int main(int argc, char*argv[])
    {
    	FILE *fp;
    
    	if(argc!=2)	/*b/c there is only one file*/
    	{
    		fprintf(stderr, "&#37;s", argv[0]);
    		return 1;
    	}
    	if((fp = fopen(argv[1], "r+"))==0)
    	{
    		perror("fopen");
    		return 1;
    	}
    	word(fp);
    	fclose(fp);
    	return 0;
    }
    
    void word(FILE *fp)
    {
    	int c;
    	int space = 1;
    	int count = 0;
    	while((c=fgetc(fp))!=EOF)
    	{
    		/*checks the code if there is a space*/
    		if(space && !isspace(c)) 
    		{
    			count++;
    			putchar(toupper(c));
    		}
    		/*If the conditions are not met, then it will add a character*/
    		else
    			putchar(c);
    		/*If there is no space, it will return true*/
    		if(isspace(c))
    			space = 1;
    		/*If there is space, it will return false*/
    		else
    			space = 0;
    	}
    	printf("\nTotal Number: %d\t", count);
    }
    Last edited by vopo; 07-16-2007 at 11:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM