Reversing words...noob

This is a discussion on Reversing words...noob within the C Programming forums, part of the General Programming Boards category; I have this code. All i need to do else is to reverse the order of words. I think, i ...

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

    Reversing words...noob

    I have this code. All i need to do else is to reverse the order of words. I think, i have to get
    lenght of the row. I tried to use strlen but I cant get it to work. Something is really messed up in my program and I dont know what. Please show me the problem, dont solve it, all I need is the right direction!

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    
    {
    	int sranje;
    	char niz[80],*ptrn;
    	int i;
    	ptrn = niz;
    	puts("Unesite recenicu: ");
    for(i=0;i<80;i++)
    {
    	scanf("%c", &niz[i]);
    	printf("%c", *ptrn);
    	if(*ptrn==' ')
    	{
    		br++;
    		printf("\n");
    	}
    	ptrn++;	
    }
    	scanf("%d", &sranje);
    
    }

  2. #2
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    and 'br' is there by accident, dont mind it...mistake

  3. #3
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    I got it, just need to reverse the words, so that goes from last to first. Exp. 'Nice day today' ----> 'today day Nice' help!

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    
    {
    	int sranje;
    	char niz[80],*ptrn;
    	int i,len=0,br=0;
    
    	ptrn = niz;
    
    	puts("Unesite recenicu: ");
    	gets(ptrn);
    
    	len=strlen(niz);
    
    for(i=len;i !=0;i--)
    {
    	printf("%c", *ptrn);
    
    	if(*ptrn==' ')
    	{
    		br++;
    		printf("\n");
    	}
    	ptrn++;	
    }
    printf("\n%d \n%d", len,br);
    	scanf("%d", &sranje);
    
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Code:
    gets(ptrn);
    Don't use gets as it is unsafe in not protecting you from input greater than the capacity of your buffer to store. Use fgets instead:
    Code:
    fgets(niz,sizeof(niz),stdin);  /* Gets only a maximum of sizeof(niz)-1 chars from stdin */
    If all you need to do is print the words in reverse order, you can go backwards from the end of the string towards the beginning and find the spaces or start of the string. When you've found one, you print the characters after that point and up to (but not including) the next space or end of the string. You can then go back to searching for the next space (or start of the string) starting from where you last left off. You can do this using a single for loop and an if test.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Code:
    #include <stdio.h>
    #include<string.h>
    
    void main(void)
    {
    	int i;
    	char temp [80];
    	printf("Upisite niz znakova <Za kraj stisnite ENTER>:\n");
    	gets (temp);
    	printf("\nObrnuti redosljed rijeci:\n\n");
    	for (i=strlen(temp)-1;i>=0;i--)
    	{
    		if(temp[i]==' ')
    		{
    			puts(&temp[i+1]);
    			temp[i]='\0';
    		}
    	}
    	printf("%s\n\n",&temp);
    }
    This one is working, but im trying to do it using pointers! And, can someone explain to me
    Code:
    puts(&temp[i+1]);
    			temp[i]='\0';
    this? Becouse i really dont get it.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by shardin View Post
    This one is working, but im trying to do it using pointers! And, can someone explain to me
    Code:
    puts(&temp[i+1]);
    temp[i]='\0';
    this? Becouse i really dont get it.
    temp[i] is the current character, the space that you've just found. temp[i+1] is the character after that one, the one you want to start printing from. Since the puts function expects an address and not a character for its argument, you must then use the address-of operator & to get the address of that character (&temp[i+1]), the starting point from which you'll be printing.

    After you've printed the word, you set the current character to a NULL (temp[i] = '\0') so that the next time you call puts to output the word that comes before this current one, it stops before it reaches the word you've already printed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing Letters in Words of a Sentence
    By CRSpeedy in forum C Programming
    Replies: 10
    Last Post: 03-31-2009, 06:12 PM
  2. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  3. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 09:01 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM

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