whisper("strings")

This is a discussion on whisper("strings") within the C Programming forums, part of the General Programming Boards category; I am trying to rebuild a string that has already had its initial spaces removed. The problem is I'm not ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    whisper("strings")

    I am trying to rebuild a string that has already had its initial spaces removed. The problem is I'm not sure the end of the last word gets a '\0' stapled to it or not....ahh The string is user defined and I'm now trying to eliminate internal spaces..and especially any after the last character in the string..here's the function;
    too much time has been spent....please throw me a line!!!!

    im tryin'
    Code:
    void leading_spaces(char string[])
    {
    	int	i=0,c=0;
    
    	while(string[i]==' ')   /*gets rid of leading zeros*/
    	{
    		i++;
    	}
    	while(string[i]!='\0')   /*rebuilds string w/o leading spaces*/
    	{
    		string[c]=string[i];
    		c++;
    		i++;
    
    	}
    	string[c]='\0';        /*places terminator after last character*/
    
    }
    
    void all_spaces(char string[])
    {
    	int i=0,c=0;
    
    		while(string[i]!='\0'&&string[i]!=' ')
    		{
    			string[c]=string[i];
    			i++;
    			c++;
    
    			while(string[i]==' '&&string[i]!='\0')
    			{
    				i++;
    			}
    			while(string[i]!='\0')
    			{
    				i--;
    				string[c]=string[i];
    				c++;
    				i++;
    				puts(string);
    			}
    		}
    	                string[c]='\0';
    }
    
    void display_line(char string[])
    {
    	int i=0;
    	while(string[i]!='\0')
    	{
    		while(string[i]!=' '&&string[i]!='\0')
    		{
    			printf("%c",string[i]);
    			i++;
    
    		}
    		if(string[i]!='\0')
    		{
    			printf("\n");
    			i++;
    			if(string[i]==' '||string[i]=='\0')
    				string[i]='\0';
    		}
    	}
    }

    [code][/code]tagged by Salem

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The string is user defined and I'm now trying to eliminate internal spaces
    Well...it seems you're doing it the hard way:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    char *remspace ( char *s )
    {
      char *p, *q;
      
      for ( p = q = s; ( *p = *q ) != '\0'; q++ ) {
        if ( !isspace ( *q ) )
          p++;
      }
    
      return s;
    }
    
    int main ( void )
    {
      char a[] = "   This    is a   test    ";
      
      printf ( "%s\n", remspace ( a ) );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm confused, what's your question again? Which part are you having problems with, and what exactly are you trying to do with it?

    string is a reserved name, so you shouldn't use it.
    You can use isspace() to test a character to see if it's a space (meaning white space, \n \t space etc etc)

    Here's a function that will remove all spaces from the input string. Maybe this will help you understand one way of doing things.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void remove_all_spaces(char *s)
    {
        char *p = s;
        if (p != NULL)
        {
            while (*p != '\0')
            {
                if (isspace(*p) == 0)
                    *s++ = *p;
                p++;
            }
            *s = '\0';
        }
    }
    
    int main(void)
    {
        char data[] = "  This is a test  ";
        
        printf("Before: >%s<\n", data);
        remove_all_spaces(data);
        printf("After : >%s<\n", data);
        
        return 0;
    }
    
    /*
    Output
    Before: >  This is a test  <
    After : >Thisisatest<
    */
    [edit]Darn it beat just before submitting!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    Im trying to learn c from the book and I was given a set of problems from a friend(without solutions) and the one I am presently working on is the following; allow a user to enter a string of characters and eliminate any leading spaces then eliminate any unwanted spaces within. I knew how to eliminate the leading spaces(see pathetic function) however once I started to eliminate the internal spaces I wasn't quite sure how to retain the single space between words, once I started it seemed I just got deeper and deeper in the &*^%(so I figured if somebody had some better solutions, I could figure it out a bit easier......anyways I'm sure there are tons of different ways to tackle this one....thanks for your valuable time (I know I wasted enough of mine trying to figure it out properly)


    cheers
    Last edited by mickle; 01-05-2003 at 06:03 PM.

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Yet another... and without pointers

    Code:
    #include <stdio.h>
    
    #define MAX 80
    
    void spaces(char aString[]);
    
    int main(void)
    {
      spaces("    a string with   spaces    .");
    
      printf("\n\nWell....alrighty then\n");
      
      return 0;
    }
    
    void spaces(char aString[])
    {
    	
      char buff[MAX];
      int store[MAX];
    	
      int i, j, k;
    	
      j=0;
      k=0;
    	
      for(i=0; aString[i] != '\0'; i++)
      {
        if(aString[i] != ' ')
        { 
          store[j] = 1;
          buff[k++] = aString[i];
        }
        else
          store[j] = 0;
        j++;
      }
    	
      buff[k] = '\0';
    	
      printf("%s\n", aString);
      printf("%s\n", buff);
    	
      j=0; 
    	
      for(i=0; buff[i] != '\0'; i++)
      {
        while(store[j++] != 1)
          putchar(' ');
    		
        putchar(buff[i]);
      }
    }

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    thanks again for the input however i want to keep a space between the words in the string.....the initial user defined string is garbage, thats why i used the user string in the function...I want to edit it for future use....

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    This problem was discussed just 3 or 4 posts ago here.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21