C++ Learning Community Forum
August 01, 2010, 02:58:58 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Help!  (Read 1049 times)
bsaikrishna
Geekoid
*
Posts: 178


View Profile
« on: November 21, 2007, 11:38:46 AM »

Could anyone please tell me the C version of this program. the main function.

Quote
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void reverse(string word) {

        for(int i=word.length()-1;i>=0;--i)
                cout<<word;
        cout<<" ";
}
int main() {

string s;
string word;

while(getline(cin,s,'\n')&&s!="")
{
        stringstream iss(s);
        while(iss>>word)
                reverse(word);

}

return 0;
}

Logged
Code Goddess
Rabble Rouser
Jr. Nerd
***
Posts: 69



View Profile WWW
« Reply #1 on: November 21, 2007, 01:32:58 PM »

*sigh*

Okay, just to shut you up (because you're obviously not listening or trying), I'll give you not only the C translation, but I'll even correct the errors:
Code:
#include <stdio.h>
#include <string.h>

void reverse ( const char *word )
{
  size_t len = strlen ( word );

  while ( --len < (size_t)-1 )
    putchar ( word[len] );
 
  putchar ( ' ' );
}

int main ( void )
{
  char line[1024];

  while ( fgets ( line, sizeof line, stdin ) != NULL && *line != '\n' ) {
    char word[1024];
    char *p = line;
    int n = 0;

    while ( sscanf ( p, "%1024s%n", word, &n ) == 1 ) {
      reverse ( word );
      p += n;
    }
  }

  return 0;
}
Don't expect me to be as nice the next time you completely ignore my help. Now shoo.
Logged
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #2 on: November 21, 2007, 04:47:06 PM »

Could anyone please tell me the C version of this program. the main function.

Quote
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void reverse(string word) {

        for(int i=word.length()-1;i>=0;--i)
                cout<<word;
        cout<<" ";
}
int main() {

string s;
string word;

while(getline(cin,s,'\n')&&s!="")
{
        stringstream iss(s);
        while(iss>>word)
                reverse(word);

}

return 0;
}


I thought this was C (with streams thrown in).
Its very procedural no OO so it looks like C to me!
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!