Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C Programming: String manipulation

Posted on 2013-10-22
6
Medium Priority
?
289 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 46

Accepted Solution

by:
Kent Olsen earned 2000 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
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!

 
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 46

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

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!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

971 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