473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

split words from a string in C

254 100+
i getting a line from text file, and then i have to split the line into words.

i have this in my text file , "23 Nick male"

i'm not sure how to split those words from a line in C language.

could anyone helps, please.
from
NickyEng
Mar 4 '07 #1
3 5996
Ganon11
3,652 Expert 2GB
Does C have the strtok method?

If you're using pure character arrays (no std::string class), then you can write a find function that takes a cstring, its size, and a character and returns the index of the first occurance of that character in the cstring. Using this, you can find the first portion of the string before a space, use that portion as your age, then repeat the process for the name and gender.
Mar 4 '07 #2
Since you are writing in C, you will declare the line you are reading in
as follow:

Expand|Select|Wrap|Line Numbers
  1. char * line;
  2.  
I would suggest, that you go through every single character:
See if it is a space:
Expand|Select|Wrap|Line Numbers
  1. for(int n = 0; n < length(line); n++)
  2. {   
  3.       if( line[n] == ' ' )   // See if it is a space
  4.           { 
  5.                 // do whatever you need doing
  6.  
You can store an array of pointers which will point to each new word in you line
and use it accordingly

Hope I helped a little. In C everything is done manually.
Mar 5 '07 #3
nickyeng
254 100+
thanks Extremist.

i will try it out.

thanks both of you.

from
Nicky Eng
Mar 5 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dchan1 | last post by:
String line = " I am new to c# "; How to use Regex to split the string so that It would return a String array with: token = "I" token = "am" token = "new" token =...
4
by: Crirus | last post by:
There is a function somewhere to split a string with multiple tokens at a time? Say I have this: aaaa#bbbbb*ccccc$dddd I whould like to split it so the result whould be aaaa bbb
8
by: Dave | last post by:
Greetings, Is there a way I can split a string into an array on a space OR a carriage return? What would the code look like for this? Thanks, -Dave
4
by: michael.bollhoefer | last post by:
I am trying to split a string into two different variables. The string is pulled from an XML file from a WEBDAV program. The string will be in this form "John Doe" <john.doe@webserver.com> ...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
6
by: Saurabh | last post by:
Hi everyone, I am looking for some expert advise to get me out of trouble. I am looking for a solution in C# which will allow me to split the below string in the format provided. The...
10
by: pantagruel | last post by:
Hi, I'm looking for an optimal javascript function to split a camelcase string and return an array. I suppose one could loop through the string, check if character is uppercase and start...
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
5
by: Robert Dodier | last post by:
Hello, I'd like to split a string by commas, but only at the "top level" so to speak. An element can be a comma-less substring, or a quoted string, or a substring which looks like a function...
3
by: Matt Borba | last post by:
What's the best method to split a string variable that holds someones full name (ex John Smith) and put the first name and last name into separate string variables? I know that searching for the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.