Solved

Finding an InString Method for C

Posted on 2011-03-08
7
293 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Console based application in Linux 1 105
Passing a array as parameter - C 2 110
Using popen() and gunzip() to open file in HTTPServer 6 85
Bitwise and to sum elements 2 52
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 how to use strings and some functions related to them 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.

734 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