Solved

C Programming: String manipulation

Posted on 2013-10-22
6
278 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 74

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 74

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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.

760 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

21 Experts available now in Live!

Get 1:1 Help Now