Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

C and getting a subset of characters

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
akohan
Asked:
akohan
  • 5
  • 5
  • 2
  • +1
1 Solution
 
ozoCommented:
char string[] = "some words here";
 char *word;

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

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
 
ozoCommented:
how do you set arr[0]?
how did you code get_ip?
0
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.

 
RishadanPortCommented:
I am confused... Are you wanting to remove ""  <-- quotations from that line?
0
 
RishadanPortCommented:
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
 
RishadanPortCommented:
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
 
akohanAuthor Commented:

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
 
ozoCommented:
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
 
RishadanPortCommented:
//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
 
ozoCommented:
do you mean
int end=strcspn(temp, "\"");
0
 
RishadanPortCommented:
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
 
ozoCommented:
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
 
MysidiaCommented:
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

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!

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now