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

int contain( int argc, char* argv[]) 
{
int i;
int j;
int lenst1;
int lenst2;
int pos1;
int pos2;

if (lenst2>lenst1)
{
    printf("flase");
    return 0;
}
for (j=0; j<lenst1;j++)
{
    for (i=0; i<lenst2; i++)
    {
        if (st2[i]==st1[j])
        {
            pos1=j;
            pos2=i;
            while (pos2<lenst2)
            {
                pos2++;
                pos1++;
                if (st2[i]==st1[j])
                {

                }
                else
                {
                    printf("flase\n");
                    return 0;
                }
                printf("true\n");
                return 0;
            }
        }
    }
}
}

My goal is to write a program called "contains" that takes two text strings as arguments and prints "true" followed by a newline.

If the second string is entirely contained within the first, or "false" followed by a newline otherwise. I think my logic is correct. My question is how do I pass these two strings as parameters.

Rui Yu
  • 113
  • 3
  • 11
  • 1
    A good start would be to actually *have* two strings. Which you don't. And use *initialized* variables. Perhaps you [need to read a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)? – Some programmer dude Feb 10 '17 at 05:23

2 Answers2

0

Initialise your variables in the main. If you want it as a parameter then write the logic in some other function and call it by passing the two strings as variables and define them in the main.

Ria Sen
  • 94
  • 13
0

The two "standard" parameters of main are responsible for this.

  • argc ... number of command line arguments
  • argv ... array of strings with command line arguments.

Thereby, argv[0] provides the name of the program.

In your case:

#include <stdio.h> /* for printf, fprintf */
#include <string.h> /* for strlen */

int main(int argc, char **argv)
{
  char *str1, *str2;
  /* check for min. number of required args. */
  if (argc <= 2) {
    fprintf(stderr, "ERROR! At least one arg. missing!\n");
    return -1;
  }
  /* get args. */
  str1 = argv[1]; str2 = argv[2];
  /* (the strings are 0-terminated) */
  printf("argv[1]: '%s' (length: %d)\n", str1, strlen(str1));
  printf("argv[2]: '%s' (length: %d)\n", str2, strlen(str2));
  /* process data */
  /* done */
  return 0;
}

Compile and run:

$ gcc -o testArgs ./testArgs.c

$ ./testArgs
ERROR! At least one arg. missing!

$ ./testArgs Hello "Hello world"
argv[1]: 'Hello' (length: 5)
argv[2]: 'Hello world' (length: 11)

More info:

  • command line arguments: google c main argc argv
  • working with 0-terminated strings: google c strlen.
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56