Solved

Finding an InString Method for C

Posted on 2011-03-08
7
283 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to communicate between two pc's through vb.net code ? 4 146
Is string constant address ? 10 192
mixing C++ and C code elegantly 10 153
How to learn Linux? 10 42
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

914 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

16 Experts available now in Live!

Get 1:1 Help Now