Char array in a function

This is a discussion on Char array in a function within the C++ Programming forums, part of the General Programming Boards category; I need to enter a sentence and the program has to write the words backwards using a function. Mean if ...

  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 11:22 AM. Reason: in c++

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Is this supposed to be C or C++?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,451
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04: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

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