LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2004, 07:28 PM   #1
saiz66
Member
 
Registered: Apr 2003
Posts: 225

Rep: Reputation: 30
[c] string help


Hi. I wanted to know if it was possible to cut the first part of a string from a substring that I wanted to search. for ex.

string1 = "Here is a sample file."
string2 = "a"

if i wanted to find an occurence of a in string1 i would type:

printf("%s\n", strstr (string1, string2));

this would output "a sample file."

But how would I output the first part? "Here is"

Thanks.
 
Old 10-05-2004, 11:01 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
#include <stdio.h>

main()
{
char a[] = "Here is a sample file";
char b[] = "a";
char * c = NULL;

printf("%s\n", strstr( a, b ));

c = strstr(a, b );
*c = '\0';
printf( "%s\n", a );

}
 
Old 10-05-2004, 11:04 PM   #3
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Code:
    char *string1 = "Here is a sample file.";
    char *string2 = "a";
    char *sub;

    sub = strstr( string1, string2 );
    if (sub) {
        printf( "%s\n", strndup( string1, sub - string1 ) );  // !!!
    }
Note that strndup() allocates memory for you, so you would be better to save the return pointer and free() it later. If your program is short and doesn't use much memory you can get by without it, but I think you would run into problems if you tried to scale up (memory leaks), so it's better to do it correctly from the start...
 
Old 10-05-2004, 11:45 PM   #4
saiz66
Member
 
Registered: Apr 2003
Posts: 225

Original Poster
Rep: Reputation: 30
thanks a lot!

Also, does anybody know if strcpy() would copy over a char * c? or do both arguments have to be char c[100]? And if that is the case how can I get around this? I dont want to have a fixed size. thanks.
 
Old 10-06-2004, 01:00 AM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Yes, but the buffer pointed to by c has to be large enough to contain the data - if you have a 50 character string and you try to strcpy it into a 40 character buffer, you're either going to cause a crash or start writing over memory that you shouldn't, with god-knows-what consequences. Also, unless you check the sizes of your buffers beforehand, you really shouldn't use strcpy over strncpy.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
string huno Programming 2 05-22-2005 03:17 PM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
C String Help Scrag Programming 6 04-14-2004 12:47 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration