1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Revrse sting program source code in c

Discussion in 'C' started by avidwan, Jan 12, 2013.

  1. avidwan

    avidwan New Member

    Joined:
    Jan 12, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    how to write reverse a string program in c?
    how to write Reverse string program source code using pointers in c programming language..?
    Armstrong numbers in c?
    palindrome numbers program in c?
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    the armstrong number is interesting. to be different, I'd try something like

    Code:
    char num[4];
    int i, val, sum;
    
    for(i=1; i <= 999; ++i) {
        
        sprintf(num, "%03d", i); // 000 format
        sum = pow(num[0] - '0', 3) +
              pow(num[1] - '0', 3) +
              pow(num[2] - '0', 3);
        sscanf(num, "%d", &val);
    
        if(val == sum)
            printf("armstrong: %d\n", i);
    }
    the other ones aren't too bad. to reverse the string, use a loop to write out each character from the end to index pos 0.

    for the pointer thing, try a function and pass the string as an argument, or create a char *ptr to point to your string; if you get the first assignment, this one is the same basic idea using *(ptr + index) instead of str[index].

    the palindrome thing is the same value forward as backward... eye and 1221 are palindromes.
     
  3. liveproject101

    liveproject101 New Member

    Joined:
    Apr 9, 2013
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello Avidwan

    If you want to get the reverse of any input string using single pointer you can try the following program:

    You would get the output olleh if the input is Hello.
     

Share This Page