string function


 
Thread Tools Search this Thread
Top Forums Programming string function
# 1  
Old 02-11-2003
string function

I have a question concerning string functions. I have not been able to locate a function that does what I want, so I fugured I'd ask before I wrote on myself.
Is there a function to which I can pass 2 strings (character string a and character string b) and have it tell me if string b appears anywhere in string a?

ie, can it determine if "test" exists in the string "this is a test"?
# 2  
Old 02-11-2003
grep will do what you want. If you just want a "yes it exists" or "no it doesn't" answer, just check $? after you have done the grep. This example might help :

stringa=test
stringb="this is a test"
echo $stringb | grep $stringa >> /dev/null
if [ $? = 0 ]
then
echo "Match Found"
else
echo "No Match Found"
fi

TioTony
# 3  
Old 02-12-2003
I assume the original poster choose to post on the C programming forum, he/she may want an answer with the C programming language, not shell script.

Try the strstr() function.

From the man page of string.h functions:



#include <string.h>

char *strstr(const char *s1, const char *s2);

strstr()
The strstr() function locates the first occurrence of the
string s2 (excluding the terminating null character) in
string s1 and returns a pointer to the located string, or a
null pointer if the string is not found. If s2 points to a
string with zero length (that is, the string ""), the func-
tion returns s1.
# 4  
Old 02-12-2003
int CheckSubStr( char * MainStr , char * SearchStr )
{
int StartIndex,Index, Counter ;
for (StartIndex=strlen(MainStr)-strlen( SearchStr ) ; StartIndex >= 0 ; StartIndex --)
for ( Index = 0 , Counter = StartIndex ; Index < strlen ( SearchStr ) && MainStr[Counter++]==SearchStr[Index++]; )
if ( SearchStr [Index] == '\0' )
return 0 ; /* Matched */
return 1 ; /* Not Matched */
}
# 5  
Old 02-12-2003
Yes, I was looking for a C function. Thanks, cbkihong. It looks like strstr() was exactly what I was looking for. Smilie
# 6  
Old 02-12-2003
Sorry, I had Shell scripting on the brain and didn't notice you posted in the C forum.
# 7  
Old 02-26-2006
Actually I am after a C shell scripting solution myself.

Can you use the C functions of Unix, especially the string functions, within a C shell? How?

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String Function in UNIX

Hi - Have file1 which has the below date 08/31/2018 And also have file2 which has the below texts ASOF:<CMODate> FUND I need to read the second file if it has colon (:) then move the date from first file to second file like this ASOF:08/31/2018 have used cut -d":" -f1 and moved the... (2 Replies)
Discussion started by: Mohan0509
2 Replies

2. Shell Programming and Scripting

How do I get the first string value from function?

Hello All, I am trying to get the value "node01_mymachine" and disregard the rest of the returned string (command ran*) from myscript.sh $ myscript.sh GetNodeName node01_mymachine Command ran successfully. If I called from another script like this: anyprocess=`myscript.sh... (2 Replies)
Discussion started by: msetjadi
2 Replies

3. Shell Programming and Scripting

awk string-function

Sorry for setting my foot as just a technical user on holy ground here again asking and learning. After tries with strings and arrays I decided to go for an if-else-if-ladder for a database, just because it looks a little easier to me, but as it happens, my result is not the desired one. So here... (8 Replies)
Discussion started by: 1in10
8 Replies

4. Programming

Returning local string value from a function in C

Hi, If I have a code like this, what are the potential problems do you see? const char* const retString() { return "hello"; /* string literal */ } My questions are: a) Since the string literal which is already a constant read only data (cannot be... (4 Replies)
Discussion started by: royalibrahim
4 Replies

5. Programming

Is this string splitting function OK?

Hello, I am recently working on an application that sends large strings accross a network very often. These then need to be broken up first with '!' and then with ','. My current function (below) works fine for this when not too much data is being sent across the network but segfaults when a... (4 Replies)
Discussion started by: kpedersen
4 Replies

6. Shell Programming and Scripting

function returns string

Can I create a function to return non-interger value in shell script? for example, function getcommand () { echo "read command" read command echo $command } command=$(getcommand) I tried to do something as above. The statement echo "read command" does not show up. ... (5 Replies)
Discussion started by: lalelle
5 Replies

7. Shell Programming and Scripting

Passing string from function with '*'

Hi I have a shell function which returns string(ksh). The string is an sql statement. This statement can have '*' in its content (i.e. select 100 / 2 *100 from dual). When this happens ret_str will have contents of current directry I run the script from build in sql. Is there any way to fix it... (2 Replies)
Discussion started by: zam
2 Replies

8. Shell Programming and Scripting

Passing a string parameter to a function

I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in. How can I make the function receive the whole string (and not terminate at the first space it encounters)?. part of... (1 Reply)
Discussion started by: fastgoon
1 Replies

9. Programming

string returning function

I have two string returning function in ESQL/C char *segment_name(lbuffer) char *lbuffer; {..... and char *get_bpdvalue(f_name) char *f_name; {...... both declared above main() char *get_bpdvalue(); char *segment_name(); my problem is segment_name works on sprintf and strcpy... (5 Replies)
Discussion started by: jisc
5 Replies

10. Programming

C function to test string or integer

Hi everyone , Is there any predefined C function that tests whether an input is string or an integer? Thank's in advance :) (3 Replies)
Discussion started by: qqq
3 Replies
Login or Register to Ask a Question