473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do check for part of a name in C

Guys:
I need to write a function in C which will take a string and that string should not have some predefined strings as a prefix of the input.
For eg: I enter a string , it should not have say.."cat", "rat", "ipv4" as prefixes.
I store these values in a array, but then which function do I use..looked thru some stuff - don't think strtok() or strcmp is the right one...

sorry..I'm a beginner at c- been ages since I last coded and forgot abt it
Any help will be greatly appreciated. Thanks in advance.
Feb 2 '07 #1
3 1329
horace1
1,510 Expert 1GB
Guys:
I need to write a function in C which will take a string and that string should not have some predefined strings as a prefix of the input.
For eg: I enter a string , it should not have say.."cat", "rat", "ipv4" as prefixes.
I store these values in a array, but then which function do I use..looked thru some stuff - don't think strtok() or strcmp is the right one...

sorry..I'm a beginner at c- been ages since I last coded and forgot abt it
Any help will be greatly appreciated. Thanks in advance.
you can use the strstr() function
char * strstr ( char * str1, const char * str2 );
it returns a pointer to the first occurrence of str2 in str1 or NULL if not found. If the pointer returned equals str1 the string is prefixed by str2, e.g.
Expand|Select|Wrap|Line Numbers
  1.     char test[]="cat called joe";
  2.     char * ch=strstr(test,"cat");
  3.     if(ch==test)  ...   test starts with cat
  4.  
see
http://www.cplusplus.com/strstr
Feb 2 '07 #2
you can use the strstr() function
char * strstr ( char * str1, const char * str2 );
it returns a pointer to the first occurrence of str2 in str1 or NULL if not found. If the pointer returned equals str1 the string is prefixed by str2, e.g.
Expand|Select|Wrap|Line Numbers
  1.     char test[]="cat called joe";
  2.     char * ch=strstr(test,"cat");
  3.     if(ch==test)  ...   test starts with cat
  4.  
see
http://www.cplusplus.com/strstr
Thanks !
But here is the catch - the word can be in the string else-where - it should not err out in this scenrio. It should only not be there in the beginning. How do u take care of that? For eg: it's k to have cat in the middle..!
Feb 2 '07 #3
horace1
1,510 Expert 1GB
Thanks !
But here is the catch - the word can be in the string else-where - it should not err out in this scenrio. It should only not be there in the beginning. How do u take care of that? For eg: it's k to have cat in the middle..!
Expand|Select|Wrap|Line Numbers
  1.  char test[]="cat called joe";
  2.     char * ch=strstr(test,"cat");
  3.     if(ch==test)  ...   error!!  test starts with cat
  4.  
in the above code if ch == test the code starts with cat otherwise it is OK
Feb 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: mrbog | last post by:
Tell me if my assertion is wrong here: The only way to prevent session hijacking is to NEVER store authentication information (such as name/password) in the session. Well, to never authenticate...
0
by: richardkreidl | last post by:
What I'd like to do is to check the values in an element if they're greater than a certain time and if they are, then change the font color of the time. I have about 10 elements that I will check...
3
by: KathyB | last post by:
Hi, I'm trying to find a way to validate input text boxes where I don't know the names until the page is rendered. I've got 2 validate functions that fire with the onsubmit button of a "mini" form...
4
by: Dr. StrangeDub | last post by:
I am part of a group that has developed an ASP.Net web application. I am looking for a way to determine whether or not the browser is actually running on the web server. For this case (when...
3
by: Rich | last post by:
I have a form with 2 check boxes. One of the check boxes is used to specify that the user is a "primary contact." When I check the primary contact box I want a second box for "standard contact"...
7
by: Tony Johnson | last post by:
Can you make a check box very big? It seems like when you drag it bigger the little check is still the same size. Thank you, *** Sent via Developersdex http://www.developersdex.com ***...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
14
by: Ørjan Langbakk | last post by:
I have a form where the user has the possibility to enclose his name. email, address and phonenumber. I want to be able to check if some of the fields are filled - at least one. This is so that...
14
by: Martin Wells | last post by:
When I have errors in a program, whether they be compiler errors, linker errors, or if the program crashes when running, I have a list of things I check for initially. If I get an error for...
13
by: PhpCool | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.