Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to extract a string starting with 'abc' & ending with 'xyz' ?

Reply
Thread Tools

How to extract a string starting with 'abc' & ending with 'xyz' ?

 
 
Umesh
Guest
Posts: n/a
 
      05-26-2007
I want to extract a string abc*xyz from a text file.
* indicates arbitrary no. of characters.

I'm only able to do it when the string has definite no. of characters
or the string length is constant: i.e. five or the string is abc?????
xyz
How can i generalize it for any length of the string?

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-26-2007
Umesh wrote:
> I want to extract a string abc*xyz from a text file.
> * indicates arbitrary no. of characters.
>
> I'm only able to do it when the string has definite no. of characters
> or the string length is constant: i.e. five or the string is abc?????
> xyz
> How can i generalize it for any length of the string?
>

Have you looked up regular expressions yet?

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      05-26-2007
"Umesh" <> schrieb im Newsbeitrag
news: ups.com...
>I want to extract a string abc*xyz from a text file.
> * indicates arbitrary no. of characters.
>
> I'm only able to do it when the string has definite no. of characters
> or the string length is constant: i.e. five or the string is abc?????
> xyz
> How can i generalize it for any length of the string?

You may want to look for regex, a librarx for regular expressions.
http://directory.fsf.org/regex.html

Bye, Jojo


 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      05-26-2007
Umesh wrote:

> I want to extract a string abc*xyz from a text file.
> * indicates arbitrary no. of characters.
>
> I'm only able to do it when the string has definite no. of characters
> or the string length is constant: i.e. five or the string is abc?????
> xyz
> How can i generalize it for any length of the string?


I don't know how you can generalise it, because I don't
know what your specifically doing.

But, assuming you don't just want to fall back to using
regular expressions, I don't see what your problem is.

Find `abc` (easy-peasy strstr-squeezy). Then find `xyz`
starting from after the `b`. Viola.

Perhaps you haven't described your full problem?

--
Shakespeare Hedgehog
"Based on their behaviour so far -- I have no idea" /Sahara/

 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      05-26-2007

"Umesh" <> wrote in message
news: ups.com...
>I want to extract a string abc*xyz from a text file.
> * indicates arbitrary no. of characters.
>
> I'm only able to do it when the string has definite no. of characters
> or the string length is constant: i.e. five or the string is abc?????
> xyz
> How can i generalize it for any length of the string?
>

As others have pointed out, if you want to handle the geneneral problem of
"how can I match a string with charcteristics such and such" you want
regular expressions.
However for a one off simple pattern regular expressions are overkill.

Use strncmp() to compare the first three character to "abc". Call strlen()
to find the end of the string, move back three places (make sure you don't
move into memory you don't own, place minus one), the call strcmp() with
"xyz".

Warp it all up in a little function of its own
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
Umesh
Guest
Posts: n/a
 
      05-26-2007
/* This is what I can do. If the length of the string starting with
abc & ending with xyz is known(11 in this case), I can program it as
follows. But if the length of the string varies between 10 to 50* what
should I do? Thanks. /

//abc?????xyz
#include<stdio.h>
#include<stdlib.h>
int main()
{

FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL)
{
puts("Error opening file");
exit(0);
}
fp=fopen("c:/2.txt","w");

char c[12];
while((c[0]=getc(f))!=EOF)
if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]=='b' && (c[2]=getc(f))!
=EOF && c[2]=='c'&& (c[3]=getc(f))!=EOF && c[3]!=' ' && (c[4]=getc(f))!
=EOF && c[4]!=' ' && (c[5]=getc(f))!=EOF && c[5]!=' ' &&
(c[6]=getc(f))!=EOF && c[6]!=' ' && (c[7]=getc(f))!=EOF && c[7]!=' '
&& (c[8]=getc(f))!=EOF && c[8]=='x'&& (c[9]=getc(f))!=EOF &&
c[9]=='y' && (c[10]=getc(f))!=EOF && c[10]=='z')
{
c[11]='\0';
fprintf(fp,"%s\n",c);

}
fclose(f);
fclose(fp);
return 0;

}

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-26-2007
Umesh wrote:
> /* This is what I can do. If the length of the string starting with
> abc & ending with xyz is known(11 in this case), I can program it as
> follows. But if the length of the string varies between 10 to 50* what
> should I do? Thanks. /
>

1) learn how to post.
2) find a regular expression library.

--
Ian Collins.
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      05-26-2007

"Umesh" <> ha scritto nel messaggio
news: oups.com...
> /* This is what I can do. If the length of the string starting with
> abc & ending with xyz is known(11 in this case), I can program it as
> follows. But if the length of the string varies between 10 to 50* what
> should I do? Thanks. /

No, it doesn't. "abc45678xyz" doesn't work.
You are only comparing the string with "abc xyz".

>
> //abc?????xyz
> #include<stdio.h>
> #include<stdlib.h>
> int main()
> {
>
> FILE *f,*fp;
> f=fopen("c:/1.txt","r");
> if(f==NULL)
> {
> puts("Error opening file");

Write that to stderr, not to stdout...
> exit(0);

Use EXIT_FAILURE, not 0 which means 'success'...
> }
> fp=fopen("c:/2.txt","w");

Check it for NULL, too.
>
> char c[12];
> while((c[0]=getc(f))!=EOF)

EOF doesn't fit in a unsigned char and might compare equal to a
valid signed char.
see www.c-faq.com, question 12.1.

> if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]=='b' && (c[2]=getc(f))!
> =EOF && c[2]=='c'&& (c[3]=getc(f))!=EOF && c[3]!=' ' && (c[4]=getc(f))!
> =EOF && c[4]!=' ' && (c[5]=getc(f))!=EOF && c[5]!=' ' &&
> (c[6]=getc(f))!=EOF && c[6]!=' ' && (c[7]=getc(f))!=EOF && c[7]!=' '
> && (c[8]=getc(f))!=EOF && c[8]=='x'&& (c[9]=getc(f))!=EOF &&
> c[9]=='y' && (c[10]=getc(f))!=EOF && c[10]=='z')

What a mess...
What's wrong with
scanf("%11c", c);
if (!strcmp(c, "abc xyz"))...
> {
> c[11]='\0';
> fprintf(fp,"%s\n",c);
>
> }
> fclose(f);
> fclose(fp);

Check wheter these succeeded.
> return 0;
>
> }
>


Try:
/*not compiled, not tested*/
#define MAXLINE 16383
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int status;
FILE *infp, *outfp;
char buf[MAXLINE+1];
char *str, *endstr = NULL;
infp = fopen("c:/1.txt", "r");
if (infp == NULL) {
perror("Unable to open input file");
exit(EXIT_FAILURE);
}
outfp = fopen("c:/2.txt", "r");
if (outfp == NULL) {
perror("Unable to open or create output file");
fclose(infp);
exit(EXIT_FAILURE);
}
while (fgets(buf, MAXLINE, infp)) {
endstr = NULL;
(str = strstr(buf, "abc")) && (endstr = strstr(str,"xyz"));
if (endstr != NULL)
fprintf(outfp, "%*s", endstr-str+3, str);
}
status = ferror(infp) || ferror(infp);
if (fclose(infp)) {
perror("Unable to close input file");
status++;
}
if (fclose(outfp)) {
perror("Unable to close output file");
status++;
}
return status ? EXIT_FAILURE : 0;
}


 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      05-26-2007

"Army1987" <> ha scritto nel messaggio
news:f39663$vqa$...
>
> "Umesh" <> ha scritto nel messaggio
> news: oups.com...
>> /* This is what I can do. If the length of the string starting with
>> abc & ending with xyz is known(11 in this case), I can program it as
>> follows. But if the length of the string varies between 10 to 50* what
>> should I do? Thanks. /

> No, it doesn't. "abc45678xyz" doesn't work.
> You are only comparing the string with "abc xyz".
>
>>
>> //abc?????xyz
>> #include<stdio.h>
>> #include<stdlib.h>
>> int main()
>> {
>>
>> FILE *f,*fp;
>> f=fopen("c:/1.txt","r");
>> if(f==NULL)
>> {
>> puts("Error opening file");

> Write that to stderr, not to stdout...
>> exit(0);

> Use EXIT_FAILURE, not 0 which means 'success'...
>> }
>> fp=fopen("c:/2.txt","w");

> Check it for NULL, too.
>>
>> char c[12];
>> while((c[0]=getc(f))!=EOF)

> EOF doesn't fit in a unsigned char and might compare equal to a
> valid signed char.
> see www.c-faq.com, question 12.1.
>
>> if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]=='b' && (c[2]=getc(f))!
>> =EOF && c[2]=='c'&& (c[3]=getc(f))!=EOF && c[3]!=' ' && (c[4]=getc(f))!
>> =EOF && c[4]!=' ' && (c[5]=getc(f))!=EOF && c[5]!=' ' &&
>> (c[6]=getc(f))!=EOF && c[6]!=' ' && (c[7]=getc(f))!=EOF && c[7]!=' '
>> && (c[8]=getc(f))!=EOF && c[8]=='x'&& (c[9]=getc(f))!=EOF &&
>> c[9]=='y' && (c[10]=getc(f))!=EOF && c[10]=='z')

> What a mess...
> What's wrong with
> scanf("%11c", c);
> if (!strcmp(c, "abc xyz"))...
>> {
>> c[11]='\0';
>> fprintf(fp,"%s\n",c);
>>
>> }
>> fclose(f);
>> fclose(fp);

> Check wheter these succeeded.
>> return 0;
>>
>> }
>>

>
> Try:
> /*not compiled, not tested*/
> #define MAXLINE 16383
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> int main(void)
> {
> int status;
> FILE *infp, *outfp;
> char buf[MAXLINE+1];
> char *str, *endstr = NULL;
> infp = fopen("c:/1.txt", "r");
> if (infp == NULL) {
> perror("Unable to open input file");
> exit(EXIT_FAILURE);
> }
> outfp = fopen("c:/2.txt", "r");
> if (outfp == NULL) {
> perror("Unable to open or create output file");
> fclose(infp);
> exit(EXIT_FAILURE);
> }
> while (fgets(buf, MAXLINE, infp)) {
> endstr = NULL;
> (str = strstr(buf, "abc")) && (endstr = strstr(str,"xyz"));
> if (endstr != NULL)
> fprintf(outfp, "%*s", endstr-str+3, str);

fprintf(outfp, "%.*s\n", endstr-str+3, str);
> }
> status = ferror(infp) || ferror(infp);

status = ferror(infp) || ferror(outfp);
> if (fclose(infp)) {
> perror("Unable to close input file");
> status++;
> }
> if (fclose(outfp)) {
> perror("Unable to close output file");
> status++;
> }
> return status ? EXIT_FAILURE : 0;
> }
>



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-26-2007
Umesh wrote:
> I want to extract a string abc*xyz from a text file.
> * indicates arbitrary no. of characters.
>
> I'm only able to do it when the string has definite no. of characters
> or the string length is constant: i.e. five or the string is abc?????
> xyz
> How can i generalize it for any length of the string?


Using what the Standard library provides, you can do

const char *the_string = ...;
const char *abc, *xyz;

abc = strstr(the_string, "abc");
if (abc != NULL) {
xyz = strstr(abc + 3, "xyz");
if (xyz != NULL) {
printf ("Found \"%.*s\"\n",
(int)(xyz + 3 - abc), abc);

--
Eric Sosman
lid
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM
Delete starting and ending lines of a file Srinivasa T.N. Perl 7 12-01-2003 06:52 AM



Advertisments