Solved

C and getting a subset of characters

Posted on 2008-10-17
13
215 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

708 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

13 Experts available now in Live!

Get 1:1 Help Now