Solved

Finding an InString Method for C

Posted on 2011-03-08
7
289 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 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Statment to match two tables in c# 6 80
Line meaning 9 88
Super Scope, DHCP 5 91
Extract specific Urls by tag from a url list using regex & vba in excel 2013 4 20
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

828 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