Solved

C Programming: String manipulation

Posted on 2013-10-22
6
280 Views
Last Modified: 2013-10-29
If I have a string such as the one below (which contains 3 fields - URL, status, host-list):
http://www.csun.edu/               redirected              130.166.238.195, redwing.csun.edu

Open in new window


Where the whitespace between each field is tab delimited, how can I store the 3rd field (host-list) in a variable?
0
Comment
Question by:pzozulka
  • 2
  • 2
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39591920
This sounds like a job for the strtok function.
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 39591941
Hi pzozulka,

There are quite a few ways to do this.  Based on your previous thread, I'd try something like this:

  char *String;   //  The line you're looking at
  char *S;
  char *URL;
  char *text;
  char *IP;

// Set some value into String for this example.

  String = "http://www.csun.edu/               redirected              130.166.238.195, redwing.csun.edu                                  ";
  S = strdup (String);  // make a copy of String so we don't mess with the original.
  URL = strtok (S, " \t");  // Get the URL
  text = strtok (NULL, " \t");
  IP = strtok (NULL, "\t");

// Note that you should call free (S) when you're done here

Open in new window


Kent
0
 
LVL 8

Author Comment

by:pzozulka
ID: 39591975
#include <string.h>
#include <stdio.h>

int main()
{
   const char str[80] = "This is - www.tutorialspoint.com - website";
   const char s[2] = "-";
   char *token;
   
   /* get the first token */
   token = strtok(str, s);
   
   /* walk through other tokens */
   while( token != NULL ) 
   {
      printf( " %s\n", token );
    
      token = strtok(NULL, s);
   }
   
   return(0);
}

Open in new window


Using the above example, the only idea that comes to mind is to use an array of char *, and during each while loop iteration, increment the array by 1. In each iteration save "token"?

Is there a better way since I don't need to save all the fields (tokens), just the last one?
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 8

Author Comment

by:pzozulka
ID: 39591986
Sorry Kent, didn't see your comment until I posted mine. that clears things up a bit.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 39592005
Your code looks pretty good.  :)  That's a lot learned in under a week.  :)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39592091
Could you not use a for loop for this instead? Something like (untested):

int i;

...

/* get the first token */
token = strtok(str, s);

for (i = 1; i < 3 && token != NULL; i++)
{
   token = strtok(str, s);    
}

if (token != NULL)
{
    printf( " %s\n", token );
}

Open in new window


It's more-or-less equivalent to what you mention with the counter variable.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now