Thread: Char array in a function

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Char array in a function

    I need to enter a sentence and the program has to write the words backwards
    using a function. Mean if i enter i'm bored i should get bored i'm
    I've only managed to do this
    Code:
     #include<stdio.h>
    #include<string.h>
    void Func21Mas(char *s, int n)
    {
      char *sen=s;
      for (int i=n-1; i>=0; i--){
        while (*sen)
          printf("%c",*sen++);
          sen++;
        printf(" ");
      }
    }
    void main()
    {
      int k=0;
      char sent[100], *ptrtok, *words[20];
      gets(sent);
      ptrtok=strtok(sent," ");
      while(ptrtok){
        words[k]=ptrtok;
        k++;
        ptrtok=strtok(NULL," ");
      }
      Func21Mas(*words,k);
      /*for (int i=k-1; i>=0; i--){
        while (*words[i])
          printf("%c",*words[i]++);
        printf(" ");
      }*/
    }
    Last edited by parken; 01-10-2011 at 12:22 PM. Reason: in c++

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is this supposed to be C or C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    It has to be in c++.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In that case, make use of the C++ standard library. For example, use the overloaded operator>> for std::istream and std::string to read into a std::vector<std::string>, then print starting from the last element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM