Solved

C and getting a subset of characters

Posted on 2008-10-17
13
218 Views
Last Modified: 2012-05-05
Hi group,

In other high level languages usually there is a function as substr or ... which allows to extract or get a copy a portion of string. What is the best way to extract a subset of a string in C?

assume I have:

           "some words here"

and want to convert it to:
           
            some words here

thanks.


0
Comment
Question by:akohan
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22744523
char string[] = "some words here";
 char *word;

 for(word = strtok(string," "); word; word=strtok(0," ") ){
   printf("%s\n",word);
 }
0
 

Author Comment

by:akohan
ID: 22744818

Thanks! ok I'm still having the issue. I know there should be something wrong on my side so I will try to explain it:

Currently I have an arry as arr where arr[0]  has a value i.e. www.example.com

This is what I've done:

char* pDomain;
pDomain = arr[0];  //arr[0] contains  www.example.com

server_ip = get_ip(pDomain);  // will not work but when I hard code it as  "www.example.com" it works!

get_ip prototype is

   char*  get_ip(char*)

Any suggestions?

Regads.









0
 
LVL 84

Expert Comment

by:ozo
ID: 22745027
how do you set arr[0]?
how did you code get_ip?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 6

Expert Comment

by:RishadanPort
ID: 22745086
I am confused... Are you wanting to remove ""  <-- quotations from that line?
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22745120
arr[0] could have lost a null character at the back of it, and is printing an extra space following with a null character.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22745144
Is this a compile error, or a run time error...

If this is a compile error, it could be that the signature is really:
char*  get_ip(const char*), and thus it would work for "www.example.com", but not if you passed in a char *...
0
 

Author Comment

by:akohan
ID: 22745842

Yes, I want to get rid of the "s. No it is not compiler error or runtime error.
Ok, I debugged it and found that since I'm reading those domains from a csv fiile so they are in "data" format, so when I pass pDomain to the function it gets there like
"www.example.com" rather www.example.com.

All I need to do it removing double quotes.

Regards.
0
 
LVL 84

Expert Comment

by:ozo
ID: 22746043
char *pDomain="\"www.example.com\"";
int start=strspn(pDomain,"\"");
int end=strlen(pDomain);
while( end && pDomain[--end] == '"' ){  }
char *server_ip = malloc(end-start+2);
strlcpy(server_ip, pDomain+start,end-start+2);
 printf("%s\n",server_ip);
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22746097
//instead of this
int start=strspn(pDomain,"\"");
int end=strlen(pDomain);
while( end && pDomain[--end] == '"' ){  }

//I suggest something more like this maybe?
char *pDomain="\"www.example.com\"";
int start=strspn(pDomain,"\"");
char *temp = pDomain + start;
int end=strspn(temp, "\"");
0
 
LVL 84

Expert Comment

by:ozo
ID: 22746145
do you mean
int end=strcspn(temp, "\"");
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22746163
oops. I was thinking that the func strspn that you provided did that.

char *pDomain="\"www.example.com\"";
int start=strcspn(pDomain,"\"");
char *temp = pDomain + start;
int end=strcspn(temp, "\"");
0
 
LVL 84

Expert Comment

by:ozo
ID: 22746196
char *pDomain="\"\"www.example.com\"";
int start=strspn(pDomain,"\"");
int end=strcspn(pDomain+start, "\"");
char *server_ip = malloc(end+1);
strlcpy(server_ip, pDomain+start,end+1);
printf("%s\n",server_ip);
0
 
LVL 23

Accepted Solution

by:
Mysidia earned 200 total points
ID: 22746754
If you don't mind  clobbering 'arr' in the process, you can truncate the
last byte and use memmove, and avoid allocating new  memory.

Accomplishes something similar to 'substr'

int main(){
char pDomain[512] ="\"www.example.com\"";

pDomain[strlen(pDomain) - 1] = '\0';
memmove(pDomain, pDomain+1,  strlen(pDomain));
printf("%s\n", pDomain);

}
------

You can also use 'static' temporary space to emulate other languages' substr,
but  either  (A)  the function won't be re-entrant  (cannot use the function
in a multi-threaded application or call the function multiple times and
keep old return values),
or (B) you choose to return malloc'ed memory and have the caller free()
it instead

#include <stdio.h>


char* substr(char * string , int start , int length )
{
          static char* temp = NULL;
            /* <--  change from 'static'  to non-static, if you want to be
                  clean, re-entrant or keep return values  beyond future calls;
                  BUT caller  must free()   old values to avoid memory leak. */

          if (temp!= NULL)  {
               free(temp);
           }
          temp = malloc(strlen(string) + 2 - start);
          if (!temp) { abort(); }

          memmove(temp, string + start, length);
          temp[start+length] = '\0';
          return temp;
}


int main()
{
char pDomain[512] ="\"www.example.com\"";

printf("%s\n", substr(pDomain,1,3));
}
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

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 and use structures in the C programming language.
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.

838 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