Thread: Space trouble

  1. #1
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78

    Space trouble

    Code:
    int main()
    
    {
    	int sranje;
    	char polje[30];
    	int a,br=0,j=0;
    
    	printf("Unesi: ");
    	gets(polje);
    
    	for(a=0;a<strlen(polje)+1;a++)
    	{
    		if(polje[a]==' ')
    		{
    			br++;
    	if(br>10)
    	{
    		polje[j]=polje[a];
    		j++;
    	}
    		}
    		else
    		{
    		br=0;
    		polje[j]=polje[a];
    		j++;
    		}
    	}
    		puts(polje);
    	
    	scanf("%d", &sranje);
    }
    First of all, I wrote some of this and I don't really understand what I wrote. The thing is, I'm trying to remove spaces in the string. With this I managed to remove them by using
    Code:
    f(br>10)
    , which I added, but what I dont get is this part
    Code:
        else
    		{
    		br=0;
    		polje[j]=polje[a];
    		j++;
    		}
    so, if someone can explain this to me, I really need to understand this.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Some "style comments":

    1. you should have a return statement at end of main, returning 0.
    2. Do NOT use gets(something), use fgets(something, sizeof(something), stdin)

    I don't see anything wrong with your "space removal", except that it leaves every tenth (11th?) space in the string with your br counter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not to mention #include <stdio.h>.

    2. Do NOT use gets(something), use fgets(something, sizeof(something), stdin)
    Why gets() is bad. http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    How to use fgets(), including stripping the (possible) newline. http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    puts() is okay, though. It's just gets() that's a problem.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Process memory space
    By barboris in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2008, 11:35 PM
  2. malloc allocates the same space in the memory
    By myle in forum C Programming
    Replies: 23
    Last Post: 11-20-2007, 07:52 PM
  3. Read space from the file
    By Ken JS in forum C++ Programming
    Replies: 9
    Last Post: 08-05-2007, 03:13 AM
  4. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  5. ARGH! (argc and argv too!) HD Space!!!
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 12-05-2001, 04:05 AM