help with passing values to a string?

This is a discussion on help with passing values to a string? within the C Programming forums, part of the General Programming Boards category; Hi guys i encountered some problems in comparing a string with another string. say if input1 is "abcdef" input2 is ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    13

    help with passing values to a string?

    Hi guys i encountered some problems in comparing a string with another string.

    say if input1 is "abcdef"
    input2 is "abc"

    the program should tells me that input2 shows up at position 1.

    this is my code
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <windows.h>
    #include <stdlib.h>
    
    
    
    main()
    {
          char input1[100], input2[100];
          char *p,*q,*w;
          char checker[100]; 
          int i,j,n,m,x;
          
          printf("Please enter a string : ");
          gets(input1);
          printf("Please enter the code : ");
          gets(input2);
          p = input1;
          q = checker;
          n=strlen(input1);
          m = strlen(input2);
    
    while(*p != '\0')
    {
    
                     for(j=0;j!=m;j++)
                      {
                           *(q+j) = *(p+j);
                      }
                      printf("%s\n%s\n",checker,input2);
                      if (strcmp(checker,input2) == 0)
                      printf("Matched at %d\n", i+1);
    //                   x = strcpy(checker,input2);
                       
    p++;
    }                             
    
    system("pause");
    }
    somehow unwanted characters showed up in my checker variable, anyone can assist me on this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Where do you ever put anything into checker?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    q points to the address of checker, as p points to input2
    so the string will be passed from p to q

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    For starters, do not use gets(). Use fgets(), or some other input reading function that is not so vulnerable to buffer overflow.

    Now, your current problem could be because you only check for j != m in the inner loop. Now, if input1 is shorter than input2, then *(p+j) would cause you to access input1 out of bounds, since m would be greater than the length of input1. Consequently, checker would be assigned characters that do not actually exist. Frankly, this could be because you have too many unnecessary variables with undescriptive names. (And what's the point of including <windows.h> here? You do not seem to use anything from <ctype.h> either.)

    Oh, and your indentation needs some work.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    i used your advise and have changed to fgets somehow i am still unable to get my checker to read in the string..
    windows.h is for the pause function

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    #include <stdlib.h>
    
    
    
    main()
    {
          char input1[100], input2[100];
          char *p,*q;
          char checker[100]; 
          int i,j,n,m;
          
          printf("Please enter a string : ");
          fgets(input1, 100, stdin);
    
          printf("Please enter the code : ");
          fgets(input2, 100, stdin);
    
          
          p = input1;
          q = checker;
          n = strlen(input1);
          m = strlen(input2);
          m = m-1;
          
    while(*p != '\0')
    {
    
                     for(j=0;j<m;j++)
                      {
                           *(q+j) = *(p+j);
                      }
                      printf("%s\n%s\n",checker,input2);
                      if (strcmp(checker,input2) == 0)
                      printf("Matched at %d\n", i+1);
    //                   x = strcpy(checker,input2);
                       
    p++;
    }                             
    
    system("pause");
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by shin_ono View Post
    q points to the address of checker, as p points to input2
    so the string will be passed from p to q
    Hey, so it does. Now: how do you get the terminating \0 character inside checker?

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    Quote Originally Posted by tabstop View Post
    Hey, so it does. Now: how do you get the terminating \0 character inside checker?
    by using the strlen of input2 assigned to m, somehow by using the fgets the strlen will be increased by 1, so i have to minus it from m after assigning it

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. "somehow ... the strlen of m was increased by 1" -- fgets gives you the newline character (as in, the enter key) as part of the input. If you don't want it, take it out. (If you search the forum, you'll see eight million examples (possibly an exaggeration) of just that.)
    2. That may be the length of m, but since your for loop with j stops before assigning the \0 character into q, then that assignment does not actually happen.
    3. You don't call the pause function, which is a good thing, since there isn't one. You do call system, which is in stdlib.h. (And even if you are referring to the pause command, one, that's a command in the dos shell, not windows.h, and two, that's the job of the shell itself, not your program -- your program doesn't parse what's inside of a system call, it just hands it off to the shell verbatim.)
    4. You do actually at some point need to use n, if you want this to work correctly.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    i changed to using array instead of pointers, but junk information still appears in checker..

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    #include <stdlib.h>
    
    
    
    main()
    {
          char input1[100], input2[100];
          char *p,*q;
          char checker[100]; 
          int i,j,n,m;
    
          printf("Please enter a string : ");
         
          fgets(input1, 100, stdin);
    
          printf("Please enter the code : ");
          
          fgets(input2, 100, stdin);
    
          
          p = input1;
          q = checker;
          n = strlen(input1);
          m = strlen(input2);
          m = m-1;
          n = n-1;
    
    for(i=0;i<n;i++)
    {
    
    				for(j=0;j<m;j++)
                     {
    
                          checker[j] = input1[i+j];
                     }
                      printf("%s\n%s\n",checker,input2);
                      if (strcmp(checker,input2) == 0)
                      printf("Matched at %d\n", i+1);
    
    }                             
    
    system("pause");
    }
    can't figure out why it happens..

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should read my last reply (again, perhaps).

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    Quote Originally Posted by tabstop View Post
    You should read my last reply (again, perhaps).
    don't really u, possible to show me?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I've simplified your code somewhat for these examples.
    For point 2:
    Code:
    char *foo = "Hello!";
    char bar[20];
    for (int i =0; i < strlen(foo); i++) { //sadly incorrect
        bar[i] = foo[i];
    }
    printf("%s\n", bar);
    For point 4:
    Code:
    char *foo = "Magic code";
    char *bar = "code";
    if (strcmp(bar, bar) == 0) //sadly incorrect, in two different ways
        printf("foo contains bar.\n");

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    Code:
    for(i=0;i<n;i++)
    {
    
    				for(j=0;j<m;j++)
                     {
    
                          checker[j] = input1[i+j];
                     }
                      printf("%s\n%s\n",checker,input2);
                      if (strcmp(checker,input2) == 0)
                      printf("Matched at %d\n", i+1);
    
    }
    the above should have dump what is inside input1 to checker. Say if inputs was "abcdef" and input2 was "abc", checker will have values of "abc", follows by "bcd" etc, but somehow junk appears after "abc" making the [if (strcmp(checker,input2) == 0)] not working as expected.

    checker is a temp place for me to allocate the characters that i want to check with input2 to determine if they match and move on to the next set.
    Last edited by shin_ono; 10-03-2008 at 07:54 PM.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you're not reading the replies, why are you still asking the questions? One last time: strings DO NOT STOP until they come to a \0 character. If you do not put a \0 character in your string, IT WILL NOT STOP BUT WILL KEEP GOING WITH "JUNK APPEARING AFTER" (whatever happened to be in that particular piece of memory beforehand).

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    thanks i think i got it, btw i do read your replies

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21