Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Finding an InString Method for C

Posted on 2011-03-08
7
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 9

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 9

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

618 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