Space trouble

This is a discussion on Space trouble within the C Programming forums, part of the General Programming Boards category; 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) ...

  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,048
    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-26-2008, 12:35 AM
  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, 04: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, 02: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

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