Thread: help on loop

  1. #31
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by StormRoBoT View Post
    can i do this in division and remainder operators to separate the number into its individual digits?
    There was a recent post about palindromes. There, the guy (or gal) used the / and % to seperate the digits. In other words he had an array of int to work with. There we proposed again a working solution, but you will see (if you search with palindrome as a key word) that it is much more difficult. Stringwise is much better.
    If strrev() is the problem and doesn't work, write it your own or google it and copy it. Stick with what you have

  2. #32
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    /*
    **  STRREV.C - reverse a string in place
    **
    **  public domain by Bob Stout
    */
    
    #include <string.h>
    
    char *strrev(char *str)
    {
          char *p1, *p2;
    
          if (! str || ! *str)
                return str;
          for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
          {
                *p1 ^= *p2;
                *p2 ^= *p1;
                *p1 ^= *p2;
          }
          return str;
    }

  3. #33
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Using xor to swap the elements is really not so clever these days, as it makes the dependance chain longer in the processor. In the old days, when processors were slow and memory was (relatively speaking) fast, saving a register may have made a difference. In a modern processor, I'm 99.9% sure that a regular "temp = a; a = b; b = temp" type swap is a better choice. [Of course, it probably takes a string that is several hundred K to make any noticable difference, but that is sort of another problem].

    --
    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.

  4. #34
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you calling me unclever? Besides, Bob Stout (whoever that is) wrote this code, not me. I just copied and pasted the first google result. With all the profiling and crap the compiler does these days you are probably right. Next time I will copy and paste the second google result

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Some empirical evidence:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    
    char *astrrev(char *str)
    {
          size_t len;
          size_t i;
    
          if (! str || ! *str)
                return str;
    
          len = strlen(str)-1;
          for (i = 0; i < len / 2; i++)
          {
                char t = str[i];
                str[i] = str[len-i];
                str[len-i] = t;
          }
          return str;
    }
    
    
    char *bstrrev(char *str)
    {
          char *p1, *p2;
    
          if (! str || ! *str)
                return str;
          for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
          {
                *p1 ^= *p2;
                *p2 ^= *p1;
                *p1 ^= *p2;
          }
          return str;
    }
    
    
    void testit(char *(*func)(char *), char *name)
    {
    	char str[3][20] = { "abc", "aabbcc", "abcdefgh" };
    	int i, j;
    	clock_t t;
    
    	for(i = 0; i < 3; i++)
    	{
    		printf("strrev(%s)=", str[i]);
    		printf("%s\n", strrev(str[i]));
    	}
    
    	t = clock();
    	for(j = 0; j < 10000000; j++)
    	{
    		for(i = 0; i < 3; i++)
    		{
    			func(str[i]);
    		}
    	}
    	t = clock() - t;
    	printf("%s: %f\n", name, (double)t / CLOCKS_PER_SEC);
    }
    
    #define TESTIT(x) testit(x, #x)
    
    int main()
    {
    
    	TESTIT(astrrev);
    	TESTIT(bstrrev);
    	TESTIT(strrev);
    
    	return 0;
    }
    The result is:
    Code:
    strrev(abc)=cba
    strrev(aabbcc)=ccbbaa
    strrev(abcdefgh)=hgfedcba
    astrrev: 0.828000
    ....
    bstrrev: 1.078000
    ...
    strrev: 0.781000
    So the winner is MS, with my function about 20% faster than the XOR method. Under some circumstances, a 20% speed up is definitely a good thing!

    --
    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.

  6. #36
    Registered User
    Join Date
    Sep 2008
    Posts
    21

    Unhappy

    wah.. this realy complicated..

    but i still cant fint the way to make it like this..

    Code:
    Enter a five-digit number ( -1 to end ): 18181
    18181 is a palindrome
    
    Enter a five-digit number ( -1 to end ): 12345
    12345 is not a palindrome
    
    Enter a five-digit number ( -1 to end ): -1

  7. #37
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    > So the winner is MS, with my function about 20&#37; faster than the XOR method. Under some circumstances, a 20% speed up is definitely a good thing!

    Well that goes to show you that google results are not always in the order of "most optimal" or "fastest performance." I will google it again and give that Bob Stout guy a stern talking to. Its times like these that I am glad I always give due credit within comments so that any complaints can go to the author of the code... That and its unethical to claim another's work as your own. But in honestly, its mostly to fan the flames in the proper direction.

  8. #38
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hmmm this has some limitations... but what if you did this:

    Example:
    Code:
    static const int primes[] = {02,03,05,07,013,015,021,023,027}; // yep.. I used octals... I am bored
    
    long unsigned long key_fstring(const char *s)
    {
      long unsigned long  r = 0;
    
      if(s)
        for(r = 1;*s;++s)
          r *= primes[*s - '0'];
    
      return r;
    }
    
    long unsigned long key_rstring(const char *s)
    {
      long unsigned long  r = 0;
      const char *begin = s;
    
      if(s)
        for(r = 1, s += strlen(s) - 1;s >= begin;--s)
          r *= primes[*s - '0'];
    
      return r;
    }
    
    int main(void)
    {
      char buffer[256], *p, *end;
      int x;
    
      do
      {
        puts("Please input a number:");
        fgets(buffer, sizeof(buffer), stdin);
        strtok(buffer, "\n");
    
        x = strtol(buffer, &end, 10);
        if(end != buffer)
        {
           x = +x;
           snprintf(buffer, sizeof(buffer), "&#37;d", x);
    
           /* All that has been done up to this point is the gathering of the user input, less user idiocy */
           printf("%d is%sa palindrome.\n", x, (key_fstring(buffer) == key_rstring(buffer))?" "," not ");
        }
      } while(*buffer);
    
      return 0;
    }

  9. #39
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by StormRoBoT View Post
    wah.. this realy complicated..

    but i still cant fint the way to make it like this..

    Code:
    Enter a five-digit number ( -1 to end ): 18181
    18181 is a palindrome
    
    Enter a five-digit number ( -1 to end ): 12345
    12345 is not a palindrome
    
    Enter a five-digit number ( -1 to end ): -1
    I fail to see why. You where more or less provided with every necessary code you needed to finish and correct your code. You had like 90&#37; of the code done yourself in the first place. Post what you don't understand from the code(s) provided
    EDIT: necessary code needed....sigh

  10. #40
    Registered User
    Join Date
    Sep 2008
    Posts
    21

    Question

    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    void main()
    {
    int a , b;
    int n;
    
    printf("Enter a five-digit number ( -1 to end ): \n");
    scanf("%d",&n);
    a = n;
    b=0;
    
    while(a>0)
    {
    b *= 10;
    b += a%10;
    a /= 10;
    }
    
    if(b == n)
    {
    printf("%d is a palindrome\n" , n);
    }
    else
    {
    printf("%d is not a palindrome\n" , n);
    }
    
    }
    this is some other code, but still i need help on looping, where should i put my while?

  11. #41
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    i need to put something like
    Code:
     while (n!=-1)
    but i dont know where to put it

  12. #42
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Have you made any effort to figure this looping out on your own? It's really not hard, if you actually think about it, and don't expect it to be spoon-fed to you. I have twice linked you to the lesson on loops here at cprogramming.com, and I suspect you haven't even looked at it.

  13. #43
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by StormRoBoT View Post
    i need to put something like
    Code:
     while (n!=-1)
    but i dont know where to put it
    Oh, here's an idea...EXPERIMENT!!!!

  14. #44
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Final post.
    Code:
    while(n!= 1) {
    //do something
    }
    This means that the code inside the brackets ({, }) will be executed repeatedly while n is not equal to -1. Thus it will only stop when n is equal to -1.
    So put inside the brackets the code you want to be executed repeatedly.
    Over and out

  15. #45
    Registered User
    Join Date
    Sep 2008
    Posts
    21

    Question

    i got the loop here
    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    void main()
    {
    int a , b;
    int n;
    
    printf("Enter a five-digit number ( -1 to end ): \n");
    scanf("&#37;d",&n);
    a = n;
    b=0;
    
    while(n!=-1)
    {
    b *= 10;
    b += a%10;
    a /= 10;
    if(b == n)
    {
    printf("%d is a palindrome\n" , n);
    printf("Enter a five-digit number ( -1 to end ): \n");
    scanf("%d",&n);
    }
    else
    {
    printf("%d is not a palindrome\n" , n);
    printf("Enter a five-digit number ( -1 to end ): \n");
    scanf("%d",&n);
    }
    }
    }
    but its seems that my formular is wrong, cuz all the number i have enter its says not a palindrome

    can any one help?
    Last edited by StormRoBoT; 09-18-2008 at 08:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

Tags for this Thread