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

Split function in C

Here is my function to split 2 strings.
It has no errors but the function returns an array of lots of random characters.

Expand|Select|Wrap|Line Numbers
  1. char * split(const char * original, const char * search) {
  2.     char split[2][1024];
  3.  
  4.     int i = 0;
  5.     int j = 0;
  6.     int flag = 0;
  7.     int r = 1;
  8.  
  9.     for (i = 0; i<strlen(original); i=i+1) {
  10.         if (!original[i]) break;
  11.  
  12.         if (flag == 0) {
  13.             if (original[i] == search[0]) flag = 1;
  14.             else split[0][i] = original[i];
  15.         } else if (flag == 1){
  16.             if (search[r]==original[i]) r=r+1;
  17.             else if (r >= strlen(search)) flag=2;
  18.             else flag=0;
  19.         } else {
  20.             split[1][j] = original[i];
  21.             j=j+1;
  22.         }
  23.     }
  24.     return split[2];
  25. }
  26.  
Jan 26 '09 #1
4 7928
gpraghuram
1,275 Expert 1GB
After splitting the string you should put \0 at the end of the string.
Then only u get the proper string as string ends with \0.


Raghu
Jan 26 '09 #2
newb16
687 512MB
You write to split[0] and split[1] but return split[2] (invalid location)
ps (style) - change x=x+1 to x++;
and don't call strlen every loop iteration
Jan 26 '09 #3
How do you return both split[0] and split[1]? I assumed split[2] would be the whole array.
Jan 26 '09 #4
newb16
687 512MB
In additional arguments, like
Expand|Select|Wrap|Line Numbers
  1. ... char* split1, char* split2) {
  2. ...
  3. strcpy(split2,split[1]);
  4. return;
  5.  
Assuming that caller preallocated space where split1 and split2 point.
Jan 27 '09 #5

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

Similar topics

5
by: Arjen | last post by:
Hi All, What I want to is using a string as PATTERN in a split function. This makes it possible for me to change the PATTERN on one place in my script... For example: $separator = ";"; $line...
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,...
4
by: Henry Chen | last post by:
Hi, I have a string that needs to be parsed into the string. The separator is not char. It is something like " at ". With current string.Split function, it doesn't work. Is there any exist...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
10
by: mb | last post by:
I was wondering if there is an easy, more useful Split function that will split with a string delimiter like "<>" or "////"?
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
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......
5
by: sck10 | last post by:
Hello, I have a list of email addresses that I need to send email to from the website. I am trying to use the "Split" function to get all the To's and then use the uBound function for the...
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"
1
by: John | last post by:
Hi I have written a Split function which in turn calls the standard string split function. Code is below; Function Split1(ByVal Expression As String, Optional ByVal Delimiter As String = " ",...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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...

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.