Solved

C and getting a subset of characters

Posted on 2008-10-17
13
216 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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…
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 structures 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.

867 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

20 Experts available now in Live!

Get 1:1 Help Now