LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2008, 02:45 AM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
splitting numbers and characters from an array of characters.


Hello all,

I have a string in c that I am trying to split the numbers and letters into their corrisponding variable, could someone explain how this is possible please i.e:

pass in a char of "123abc"

and return two values of "123" as and integer and another variable of "abc"?

Many thanks in advance.
 
Old 11-13-2008, 03:13 AM   #2
maradnus
Member
 
Registered: Oct 2008
Location: Yellagiri Hills
Distribution: Fedora
Posts: 87

Rep: Reputation: 15
You can go through the string related functions like

strcpy, strncpy, etc

use man strcpy for more information.

YOu can also think of atoi() function.

----------------
OR

you can write your own function to do that.
void parsestring(const char *src, char *no, char *text);
 
Old 11-13-2008, 03:25 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
Hi, cheers for the response. its not the conversion that i cant work out, its the actual splitting of the string( on the basis that i do not know how long the numbers, or chars are in the value ) i.e 123abc or 1234cd etc:


Code:
if( splitstring( charvalue, numvalue, "123abc" )==TRUE )
{}

Code:
int splitstring( char *p_newchar, char *p_newnum, char *p_text )
{
int  l_newnum        = 0;
char l_tmpnum[10]    = "";
char l_newchar[10]   = "";

for( lenofnumber )
{
  l_tmpnum=intvalue + nextint;
}

l_newnum=atoi( l_tmpnum );

for( lenofchar )
{
  l_newchar=charvalue + nextchar;
}

...

}
 
Old 11-13-2008, 03:53 AM   #4
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Well, you obviously have to go through the whole string one character at a time and decide if it's a number or a letter.

You might find some more clues in ctype.h
 
Old 11-14-2008, 09:20 AM   #5
maradnus
Member
 
Registered: Oct 2008
Location: Yellagiri Hills
Distribution: Fedora
Posts: 87

Rep: Reputation: 15
I hope the data format is fixed like this: NNNNNNNNNXXXXXXXX
that is first part is a number and the next part is the text.
 
Old 11-14-2008, 09:33 AM   #6
maradnus
Member
 
Registered: Oct 2008
Location: Yellagiri Hills
Distribution: Fedora
Posts: 87

Rep: Reputation: 15
Use the following statement:

Code:
char text[100];
strcpy(text,"1234asdfk");
int n = atoi(text);
printf("%d",n);
 
Old 11-14-2008, 09:34 AM   #7
maradnus
Member
 
Registered: Oct 2008
Location: Yellagiri Hills
Distribution: Fedora
Posts: 87

Rep: Reputation: 15
Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
void pstring(const char *src, int *);
int main(int argc, char *argv[])
{
        int n;
        char t[100];
        printf("\n Enter Text:");
        scanf("%s",t);
        pstring(t,&n);
        exit(0);
}


void pstring(const char *src, int *no)
{
        int result;
        int i=0;
        int sum = 0;
        while(src[i]!='\0'){
                if(isalpha(src[i]))break;
                sum = sum*10+(src[i]-'0');
                i++;
        }
        *no = sum;

}
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mirror characters in an array flyordie Linux - Newbie 1 10-16-2008 04:07 AM
To store N numbers in an array som_kurian Programming 10 12-16-2007 03:31 AM
Accented Characters and other "foreign language" Characters Mark_in_Hollywood LQ Suggestions & Feedback 2 04-30-2007 06:10 PM
How to modify the names of files and replace characters with other characters or symb peter88 Linux - General 2 12-10-2006 03:05 AM
Accented characters in C Orkie Programming 2 05-17-2005 12:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration