Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C++
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv){
  char *str1, *str2;
  int str1len, str2len; 

  str1 = argv[1]; str2 = argv[2];
  str1len=strlen(str1);
  str2len=strlen(str2);
  if (str2len>str1len){
    printf("false\n");
    return 0;
  }
  char sub1[100];
  for (int i=0;i<str1len-str2len;i++){
    strncpy(sub1,str1+i,str2len);
    if (strcmp(sub1,str2)==0){
      printf("true\n");
      return 0;
    }
  }
  printf("false\n");
  return 0;
}

What I have tried:

my code works correctly in all cases except when I have something like this: 'beautiful' 'bea' the result should be false but it gives me true. so generally if the second argument is contained within a word of the first argument, it returns true, although it should return false.
Posted
Updated 27-May-18 0:07am
v3

The first thing to notice is that technically, it can be argued that "bea" is fully contained in "beautiful" in the sense that all the letters are there in sequence and next to each other: I would expect to find "true" for that.
And when I run your code with "beautiful" and "bea" I get false anyway ...
As I do for "beautiful" and "eau", which is wrong, and for "beautiful" and "aut", but not for "beautiful" and "auti" which returns true.

That code is a bad way to do it: your are copying a lot of information just so you can use standard functions, and that is part of your problem. Read the definition of strncpy[^] and then think about what it puts in your sub1 buffer, and what strcmp[^] is going to do with it.

This is your homework: while I wouldn't do it like this (it's very inefficient) it's also t=your task to get it working. So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

Oh, and do yourself a favour: indent your code! It becomes a lot more readable, and easier to work with if you do that...
C++
#include <stdio.h>
#include <string.h>
int
main (int argc, char **argv)
{
  char *str1, *str2;
  int str1len, str2len;
  str1 = argv[1];
  str2 = argv[2];
  str1len = strlen (str1);
  str2len = strlen (str2);
  if (str2len > str1len)
    {
      printf ("false\n");
      return 0;
    }
  char sub1[100];
  for (int i = 0; i < str1len - str2len; i++)
    {
      strncpy (sub1, str1 + i, str2len);
      if (strcmp (sub1, str2) == 0)
	{
	  printf ("true\n");
	  return 0;
	}
    }
  printf ("false\n");
  return 0;
}
See what I mean?
 
Share this answer
 
v2
Here's How:
C++
#include "stdafx.h"

#include <stdio.h>
#include <conio.h>
#include <string.h>

void print_true_if_exists(const char* str1, const char* str2)
{
	bool found = false;
	for (int pos = 0; str1[pos] != '\0' && !found; pos++)
	{
		int i = pos, t = 0; double rel = .0;
		while (str1[i] != '\0' && str2[t] != '\0')
			rel += (str1[i++] == str2[t++]) ? 1 / (double)strlen(str2) : 0;

		if (rel == 1) { printf("result: \"true\" string 2 is found at pos %d", pos); found = true; }
	}

	if (found == false)
		printf("result: \"false\" string 2 is not found...");
}

int main(int argc, char* argv[])
{
	char str1[256] = "red green blue pink white grey", str2[256] = "pink\0";

	print_true_if_exists(str1, str2);

	_getch();

    return 0;
}
 
Share this answer
 
I would use strtok to split the long string into parts and than make a comparision.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900