Solved

Finding an InString Method for C

Posted on 2011-03-08
7
281 Views
Last Modified: 2012-05-11
Hi,

I need an inString function that returns a 0 or 1 to use in my IF statements. I do not think the attached code is correct, and I would love it if I could tap someone's brain to get a working solution.

I searched on here already and could not find anything.

I would like to make this so that I can search for any sized string in the first string.

Thanks
static int inString(const char* testvalue, char token[1])
{
	// variables
	int i;
	// resolve token in string
	for (i = 0; i < strlen(testvalue); i++)
		if (testvalue[i] == token)
			return 1;
	// return
	return 0;
}

Open in new window

0
Comment
Question by:dilithiumtoys_dot_com
  • 3
  • 2
  • 2
7 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 35075153
I think you probably are looking for one of these two library functions:
    http://www.cplusplus.com/reference/clibrary/cstring/strchr/

    http://www.cplusplus.com/reference/clibrary/cstring/strstr/
0
 

Author Comment

by:dilithiumtoys_dot_com
ID: 35078188
Neither function seems to be working for me. Specifically I need to find if a character exists in the string and get a Boolean/testable value from it. How do I test either to determine if the character is actually in the string?



 
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 35080298
strchr() can do your job. I don't know how it's not working for you.

Have a look on the below code

#include "stdio.h"
#include "string.h"

int inString(const char* testvalue, char token) {
  const char* retval = strchr(testvalue, token);
  if(retval == NULL)
    return 0;
  else
    return 1;
}

int main() {
  int res = inString("Subrat", 's');
  printf(res?"Charecter present\n":"Char not present\n");
  res = inString("Subrat",'S');
  printf(res?"Charecter present\n":"Charecter not present\n");
  return 0;
}
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 32

Expert Comment

by:phoffric
ID: 35082179
>> Neither function seems to be working for me. Specifically I need to find if a character exists in the string and get a Boolean/testable value from it.

If you post the code that does not work for you, then we can help fix it. As this problem is a typical academic assignment, I was unable to provide you with code. But, if you want to learn how to find the problem yourself, just post your code for our advice.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35082196
Did you copy the code in the strchr link and try to make it fit for your program? Here is the sample code:
/* strchr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
    printf ("found at %d\n",pch-str+1);
    pch=strchr(pch+1,'s');
  }
  return 0;
}

Open in new window

0
 

Author Comment

by:dilithiumtoys_dot_com
ID: 35084440
Hi,

I am using SubRat2009's example in the attached code, but with strstr(). I am passing in

str1 = "eula.txt"
str2 = "."

I am getting the integer 0 as a result, when I should be getting 1 back. Whats wrong?

Thanks
static int inString(const char* str1, const char* str2)
{
	// variables
	const char* retval; 
	// check string length
	if (strlen(str1) == 0 || strlen(str2) == 0)	
		return 0;
	// resolve string
	retval = strstr(str1, str2);
	// return the result
	if(retval == NULL)
		return 0;
	else
		return 1;
}

Open in new window

0
 
LVL 8

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 500 total points
ID: 35088606
Nothing wrong here. Can you post the main() function where u r calling this function.
Below is the tested code and working fine with returing 1.

int _tmain(int argc, _TCHAR* argv[])
{
      char* str1 = "eula.txt";
    char* str2 = ".";
      int x = inString(str1, str2);
      printf("%d\n",x);

      return 0;
}
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

708 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

13 Experts available now in Live!

Get 1:1 Help Now