473,395 Members | 1,404 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,395 software developers and data experts.

how to split input string into 4 components

3
Sample input string

james 35 bond 30
berry 40 bond 37

The program must separte each line into 4 parts. first name, age, last name, and age+7, the following code is a sample of with only 2 component, I don't know how to do it with 4 components. Could someone edit the code for me please.



Expand|Select|Wrap|Line Numbers
  1. void main(int argc, char *argv[])
  2. {
  3.     struct person list[];
  4.     int i,n;
  5.     char *ch, *ch1;
  6.     char buf[256];
  7.     FILE *file1;
  8.     i=0;
  9.     fgets(buf,250,file1);    
  10.  
  11.     while(!feof(file1))
  12.     {
  13.     ch=strchr(buf,'\n');
  14.     if (ch!=NULL) *ch='\0';
  15.     else break;
  16.     ch = strchr(buf, ' ');
  17.     if(ch!=NULL) 
  18.                  { 
  19.                  *ch='\0'; 
  20.                  ch1=++ch;  //age
  21.  
  22.                   }
  23.     else break;
  24.     strcpy(list[i].lastname,buf);
  25.                 list[i].age=atoi(ch1);
  26.     i++;        
  27.     fgets(buf,256,file1);
  28.     }
  29.  
  30.     n=i;
  31.     for(i=0;i<n;i++)
  32.               printf("%s is %d years, first name is %s\n", list[i].lastname, list[i].age); 
  33.     }
Mar 15 '09 #1
3 3303
gpraghuram
1,275 Expert 1GB
Hi,
If the lines are tokens are separated by space then u can use this logic.
Open the file
Read the line using fgets.
use sscnaf to read the 4 values
store the values
close the file

Thanks
Raghu
Mar 16 '09 #2
nyc680
3
do sscanf automatically parse into 4 parts? can you show me an example code?
Mar 16 '09 #3
gpraghuram
1,275 Expert 1GB
hi,
Check this.

Expand|Select|Wrap|Line Numbers
  1. int arr[]="james 35 bond 30";
  2. char val1[10],val3[2];
  3. int val2,val4;
  4.  
  5. sscanf(arr,"%s %d %s %d",val1,&val2,val3,&val4);
  6.  
  7.  
I think the above one helps u.

Raghu
Mar 16 '09 #4

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

Similar topics

2
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces,...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
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
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
3
by: edoardo.poeta | last post by:
I'm a dummy. I have a basic knowledge of javascript and I want to split a string, but I receive an error at line 15. Where my error in make the array? Why? Can someone help me to resolve? Thank's....
7
by: Jordi Rico | last post by:
Hi, I know I can split a string into an array doing this: Dim s As String()=Regex.Split("One-Two-Three","-") So I would have: s(0)="One" s(1)="Two"
12
blazedaces
by: blazedaces | last post by:
Hello again. I'm trying to take as an input an ArrayList<String> and utilize String's .spit(delimiter) method to turn that into a String. I'm getting some kind of error though (I'll post the code...
1
by: mad.scientist.jr | last post by:
I am working in C# ASP.NET framework 1.1 and for some reason Regex.Split isn't working as expected. When trying to split a string, Split is returning an array with the entire string in element ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.