LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 02-01-2005, 07:30 PM   #1
its_godzilla
LQ Newbie
 
Registered: Jan 2005
Posts: 10

Rep: Reputation: 0
how to print the first character in a string using strtok


I need help with a program to pull the first character out of a string I read in. I'm using strtok to get the first name and last name separated by a pipe " | " here is my while loop.


while (fgets(buffer,100,stin) != NULL)
{
token[0] = strtok(string, " | "); // gets the first name
token[1] = strtok(NULL, " | "); // gets the last name


Basically I need to print out the first initial and last name .....
Any ideas????
 
Old 02-01-2005, 07:57 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
If you're sure the pipe will be there I'd just do this:
Code:
printf("%c %s\n", string[0], strchr(string, '|')+1);
 
Old 02-01-2005, 08:30 PM   #3
its_godzilla
LQ Newbie
 
Registered: Jan 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Thats not going to work the name comes in like this -----> Bob|Smith|
I need to be able to print BSmith. I need to find out if the strok will let me somehow pull the first char out of token[0] which is the first name = Bob.
 
Old 02-01-2005, 09:14 PM   #4
its_godzilla
LQ Newbie
 
Registered: Jan 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Can anyone else give some input????
 
Old 02-02-2005, 02:17 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Here's a couple of different ways to do it:
Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
  char str[50] = "Bob|Smith|";
  char *s = str;
  char *p;

  p = strtok(s, "|");
  printf("%c", *p);
  p = strtok(NULL, "|");
  printf("%s\n", p);

  return 0;
}
Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
  char str[50] = "Bob|Smith|";
  char *p;

  printf("%c", *str);
  p = strchr(str, '|')+1;
  *strchr(p, '|') = '\0';
  printf("%s\n", p);

  return 0;
}

Last edited by itsme86; 02-02-2005 at 02:19 AM.
 
Old 02-02-2005, 10:22 AM   #6
its_godzilla
LQ Newbie
 
Registered: Jan 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks for your help I know have it up and running
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
python: converting a 3 character octal string into and ASCII char llama_meme Programming 1 07-06-2010 02:00 PM
Remove last character from file/string linuxchump Programming 34 06-08-2009 04:01 AM
Picking a character from a string randomly swatward Programming 2 08-14-2005 01:21 AM
how to solve Character and String problems on linux?(screenshot) tradingbr Linux - General 0 04-29-2004 02:51 PM
Using sed to convert a string to a character? whansard Linux - General 2 01-10-2003 05:13 AM


All times are GMT -5. The time now is 12:17 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration