Thread: writing backwards?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    writing backwards?

    Hey guys i have homework and i need seriously help, i dont know exactly writing programs need your help

    question is Wtire a C program that will enter a line of text, store it in an array and then write it out backwards. Allow the length of the line not exceed 80 characters.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    We don't do homework. We provide help in fixing your homework, or guiding you along the way. Right now you should provide us with some code you have written for this, or what your ideas are concerning it.

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Wow, I'm slipping. Someone beat me to the response quicker than I could write the code -(. Anyways, maybe something like the following...

    Code:
    #include <stdio.h>
    
    #define MAXLINE 80
    
    /*Rookie way to reverse a string*/
    void reverse(char s[])
    {
      int i, j;
      char temp;
    
      i = 0;
      while (s[i] != '\0')
        ++i;
      --i;
      if (s[i] == '\n')
        --i;
      j = 0;
      while (j < i) {
        temp = s[j];
        s[j] = s[i];
        s[i] = temp;
        --i;
        ++j;
      }
    }
    
    int getline(char s[], int max)
    {
      int c, i;
      for (i = 0 ;i < max -1 && (c = getchar()) != EOF && c != '\n'; i++) 
        s[i] = c;
      if (c == '\n') {
        s[i++] = c;
      }
      s[i] = '\0';
      return i;
    }
    
    int main(void)
    {
      char line[MAXLINE];
    
      while (getline(line, MAXLINE) > 0) {
        reverse(line);
        printf("%s\n", line);
      }
    
      return 0;
    }
    [cd@localhost oakland]$ gcc -Wall -Wextra -Wshadow -ansi -pedantic easy.c -o easy
    [cd@localhost oakland]$ ./easy
    sex
    xes

    drugs
    sgurd

    chicks
    skcihc

    lalalalaliesoedosnomeosnffffffffffffffffffffffffff ffffffffffffffffffffffilolndodsmsoetssssssssssssss sssssssssssssssssssssssssssssssssssssssssiiiiiiii
    odnlolifffffffffffffffffffffffffffffffffffffffffff fffffnsoemonsodeoseilalalalal
    iiiiiiiissssssssssssssssssssssssssssssssssssssssss sssssssssssssteosmsd

    maybe
    ebyam

    [cd@localhost oakland]$
    Last edited by Overworked_PhD; 04-28-2010 at 05:01 PM.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Overworked_PhD View Post
    Wow, I'm slipping. Someone beat me to the response quicker than I could write the code -(. Anyways, maybe something like the following...

    Code:
    #include <stdio.h>
    
    #define MAXLINE 80
    
    /*Rookie way to reverse a string*/
    void reverse(char s[])
    {
      int i, j;
      char temp;
    
      i = 0;
      while (s[i] != '\0')
        ++i;
      --i;
      if (s[i] == '\n')
        --i;
      j = 0;
      while (j < i) {
        temp = s[j];
        s[j] = s[i];
        s[i] = temp;
        --i;
        ++j;
      }
    }
    
    int getline(char s[], int max)
    {
      int c, i;
      for (i = 0 ;i < max -1 && (c = getchar()) != EOF && c != '\n'; i++) 
        s[i] = c;
      if (c == '\n') {
        s[i++] = c;
      }
      s[i] = '\0';
      return i;
    }
    
    int main(void)
    {
      char line[MAXLINE];
    
      while (getline(line, MAXLINE) > 0) {
        reverse(line);
        printf("%s\n", line);
      }
    
      return 0;
    }
    [cd@localhost oakland]$ gcc -Wall -Wextra -Wshadow -ansi -pedantic easy.c -o easy
    [cd@localhost oakland]$ ./easy
    sex
    xes

    drugs
    sgurd

    chicks
    skcihc

    lalalalaliesoedosnomeosnffffffffffffffffffffffffff ffffffffffffffffffffffilolndodsmsoetssssssssssssss sssssssssssssssssssssssssssssssssssssssssiiiiiiii
    odnlolifffffffffffffffffffffffffffffffffffffffffff fffffnsoemonsodeoseilalalalal
    iiiiiiiissssssssssssssssssssssssssssssssssssssssss sssssssssssssteosmsd

    maybe
    ebyam

    [cd@localhost oakland]$
    Back for more violation of forum rules and unnecessary trolling I see.

  5. #5
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Nah, I'm just proving that I can run circles around you when it comes to basic programing -).

  6. #6
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Besides, no one on here should be do *any* kind of programming for a living if this exercise took them more than 5 minutes to do.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Overworked_PhD View Post
    Nah, I'm just proving that I can run circles around you when it comes to basic programing -).
    Haha. Sure keep comforting yourself with that.

  8. #8
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Ya know, anytime you want to meet me face to face for some kind of programming showdown, I'll be more than happy to show up and clown you like the rookie you are. Maybe we could get MK27 or tabstop to issue the programming problems. Then, in real life, without the aid of google or any other resource, we can go at it -).

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Overworked_PhD View Post
    Ya know, anytime you want to meet me face to face for some kind of programming showdown, I'll be more than happy to show up and clown you like the rookie you are. Maybe we could get MK27 or tabstop to issue the programming problems. Then, in real life, without the aid of google or any other resource, we can go at it -).
    Yeah right, like I have the kind of time you have to go around 'competing' in programming with strangers on the Internet. ROFL. I did that when I was in college, you should have tried that too. I am going to stop responding to your useless trolling in respect for the OP whose thread is being clogged with nonsense.

  10. #10
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    What is 'ROFL'? Sorry, I only know proper English. And given your responses, I bet you probably went to either some 2 year local community college or a 4 year university that no one has ever heard about. I'm not saying that this is bad, but the quality of your eduction reflects in your less than brilliant responses on here.
    Last edited by Overworked_PhD; 04-28-2010 at 05:57 PM. Reason: Education is never a bad thing.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop trolling or we'll ban you again. You aren't actually discussing anything C-worthy, so if you want to troll, go to the general forum or some place, or get permabanned.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quzah, maybe you should give some of your "winner" advice on comp.lang.c. I would love to see you go down in flames when someone like Dr. Sosman from SUN Microsystems or Dr. Mclean from Oxford University in England corrects you and then you try to engage in a losing debate with a bunch of technical geniuses.
    Last edited by Overworked_PhD; 04-28-2010 at 06:18 PM.

  13. #13
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Pssst... The late Dr. W. Richard Stevens, authors of the books "Advance Programming in the Unix Environment" and the Unix Network Programming books used to roam the halls of comp.lang.c. Another notable alumni of comp.lang.c is ANSI/ISO C committee member P.J. Plauger.

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Heh. Nice name dropping. I can do that too. Dennis Ritchie. Brian Kernighan. Ken Thompson. Doug McIlroy. Bjarne Stroustrup. Wow, I feel smarter already.

  15. #15
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by kermit View Post
    Heh. Nice name dropping. I can do that too. Dennis Ritchie. Brian Kernighan. Ken Thompson. Doug McIlroy. Bjarne Stroustrup. Wow, I feel smarter already.
    I have copies of e-mail correspondences between me and Dr. Ritchie. Also, I wouldn't mention these names unless I had some kind of active communication between them on comp.lang.c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 03:28 AM
  2. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  3. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 02:16 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM