Solved

C Programming: String manipulation

Posted on 2013-10-22
6
283 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:
Kent Olsen 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:Kent Olsen
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

740 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