Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C Programming: String manipulation

Posted on 2013-10-22
6
Medium Priority
?
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.
Suggested Courses

722 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