Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-04-2005, 01:43 PM   #1 (permalink)
Lordieth
Registered User
 
Join Date: Sep 2005
Posts: 12
Lordieth is on a distinguished road
String disassembling

Right, i've got to designing a program which will read a text file, and then convert each word one by one, then print it to the screen and a file - but i've ran into a problem.

Code:
while(getline(fin, temp))
cout << temp << endl;
(fin being the filesteam input)

Now, while this will read every line easily from a text file and print it, i'm not sure what to do with that line in the string - ideally, I want to break it apart word by word, convert each one, and put it back into the string, which sounds like the best way of going about things, but i'm not entirely sure how I would: break the string up into it's seperate contained words - once i've done that I can load them into a string array, convert the words, and combine them back together - presto! it's just the matter of seperating words in a string that confuses me.

I'd be very grateful for any help on this!
Lordieth is offline   Reply With Quote
Old 10-04-2005, 02:12 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
You might wanna look at get() for streams, especialy the stream& get (char* s, streamsize n, char delim ); where you can tell it to use <space> as teh delimiter.
Else you might wanna checkout strtok() where this thread might give you a hint.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-05-2005, 06:05 AM   #3 (permalink)
Lordieth
Registered User
 
Join Date: Sep 2005
Posts: 12
Lordieth is on a distinguished road
Quote:
Originally Posted by redhead
You sir, are brilliant - works a charm!

Just one more problem....

Code:
/* strtok example */
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

void Database();

string test[100];
int i;
int j;

int main ()
{
  string input = "testing";
  
  cout << "Please enter a sentence to be converted\n>>";
  cin >> input;
  
  char str[] ="This is a sample string,just testing.";
  char * pch;
  printf ("Splitting string \"%s\" in tokens:\n",str);
  pch = strtok (str," ");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    test[i++] = ("%s\n",pch);
    pch = strtok (NULL, " ,.");
  }
  system("pause");
  for(j = 0; j < 7; j++)
  {
          Database();
          cout << test[j] << " ";
  }
  cout << "\n";
  system("pause");
  return 0;
}

void Database()
{
     if(test[j] == "sample")
     {
                test[j] = "Smaple";
     }
}
I have no way it seems, of entering my own text to use - it won't accept strings, or chars with an allotted size, or an open char like str - i'm really stuck here.
Lordieth is offline   Reply With Quote
Old 10-05-2005, 06:47 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
What are you talking about ??
Code:
/* strtok example */
#include <string>
#include <iostream>
#include <vector>

int main ()
{
  std::string input;
  std::vector <std::string> input_words;
  char* ptr;
  std::cout << "Please enter a sentence to be converted" << std::endl;
  std::cout << ">> ";
  std::getline(std::cin, input);
  std::cout << "Splitting string \"" << input 
	    << "\" in tokens:" << std::endl;
  ptr = strtok ((char*) input.c_str()," ,.");
  while (ptr != NULL)
  {
    input_words.push_back(ptr);
    ptr = strtok (NULL, " ,.");
  }
  for(int i=0; i < input_words.size(); ++i)
    std::cout << input_words[i] << std::endl;
  return 0;
}
Completely C++ without any restrictions what so ever...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 10-05-2005 at 07:13 AM.
redhead is offline   Reply With Quote
Old 10-05-2005, 07:14 AM   #5 (permalink)
Lordieth
Registered User
 
Join Date: Sep 2005
Posts: 12
Lordieth is on a distinguished road
Hm, I think I got a little confused somewhere

I was trying to say char str[] = string, or char, or whatever, and it was throwing a fit about it.

I'll try your method above now, cheers.
Lordieth is offline   Reply With Quote
Old 10-05-2005, 07:21 AM   #6 (permalink)
Lordieth
Registered User
 
Join Date: Sep 2005
Posts: 12
Lordieth is on a distinguished road
It works great - thankyou! I think I know where I went wrong (and I was going to add getline after, but thanks for pointing that out aswell)

I was expecting
Code:
char* ptr;
to be string instead, and your code here:
Code:
ptr = strtok ((char*) input.c_str()," ,.");
Looks a little different, so I assume that had something to do with it?

Sorry for being such a noob, this function is a little new to me

Anyway, I really appreciate your help, and I know, I shouldn't be using namespace
Lordieth is offline   Reply With Quote
Old 10-05-2005, 08:09 AM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Code:
ptr = strtok ((char*) input.c_str()," ,.");
means, convert the string input to a standard char* (in ANSI/ISO C reference) with the .c_str()
this how ever returns a const char* but strtok() expects a regular char* which it can dynamicaly alter, const aren't supposed to be altered.
So we tell it with the cast ie: (char*)
In this case strtok() will alter the first argument to hold the remainder of the orriginal string after encountering any chars from our delimiter string, since input.c_str() is assigned to a runtime temporary variable we couldn't care less what happens to it, thus we just cast it to char* and feed it to strtok(), which in return assigns first word to our ptr.
Uppon second call to strtok() we want to keep on cyckling through our string, which at this point is assigned to an internaly known variable, hence strtok() needs to be called with NULL.
Since our orriginal conversion of the input string was a const char, we're out of the woods from our (char*) cast at this point, since now the string is assigned to the internaly used variable which allows it to be altered on each call to strtok().
Quote:
Sorry for being such a noob, this function is a little new to me
Dont be sorry, your first shot at this lead me to the assumption, that you knew about the differences and conversions between std::string and char*, so I assumed you were able to implement it into the expected use of strtok()
Quote:
Anyway, I really appreciate your help, and I know, I shouldn't be using namespace
I'm only trying to make my code examples useable in any environment they might be introduced to. Specificaly telling what namespace every instance is fetched from, helps to ensure the expected results in any environment.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help for another program Androto Standard C, C++ 54 10-15-2004 08:21 AM
sorting objects arrays by object properties sde Java 28 08-05-2004 06:51 AM
From C to Java HighterDK Java 11 07-13-2004 08:15 PM


All times are GMT -8. The time now is 05:08 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting