checking for a code in a string

This is a discussion on checking for a code in a string within the C Programming forums, part of the General Programming Boards category; Hi guys i am encountering some problems. I need to write a prog that compares a user input string with ...

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

    checking for a code in a string

    Hi guys i am encountering some problems.

    I need to write a prog that compares a user input string with a code wode, if the code word is found, the program will return the positions of the code word found in the user input string. I manage to done part if i already fixed the code word in my prog, but somehow if i allow the user to input the code word to check for, the prog doesn't work as i have fixed the code word to 3 letters, is there a way where i can get the prog to check for any length of letters?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Obvious answer is don't limit the code word to three letters.

    You'll need some "large enough" buffer (256 characters, perhaps).

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    How are you currently doing it? You might try strstr().

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    current code is as follows:
    Code:
    main()
    {
          char string1[100];
          char string2[]="ABCD"; 
          int i,n;
          printf("Please enter a string : ");
          scanf("%s",string1);
          n=strlen(string1);
          for(i=0;i<=n;i++)
          {
                          if((string1[i]==string2[0]) && (string1[i+1]==string2[1]) && (string1[i+2]==string2[2]) && (string1[i+3]==string2[3]))
                          printf("\nCode found at %d", i+1);
          }
          
          system("pause");
                         
    }
    if i want to let the user to enter the code word, any idea how it can be done?
    Last edited by Salem; 09-30-2008 at 10:04 PM. Reason: Added [code][/code] tags, learn to use them yourself

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Code:
    n=strlen(string1);
    for(i=0;i<=n;i++)
    This for loop is dangerous. Say the string is "helloz." strlen(string1) will return 7. However, string1[7] is out of bounds.

    This link may be helpful. Make sure you try and learn from it, though.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    Quote Originally Posted by kpreston View Post
    Code:
    n=strlen(string1);
    for(i=0;i<=n;i++)
    This for loop is dangerous. Say the string is "helloz." strlen(string1) will return 7. However, string1[7] is out of bounds.

    This link may be helpful. Make sure you try and learn from it, though.
    don't really get it as i didn't learn address pointer before..

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by shin_ono View Post
    don't really get it as i didn't learn address pointer before..
    I don't have sufficient knowledge in the language to actually teach you. I'm just trying to point you in the right direction, so you can do your own research.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    thanks man, i will try it out

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > This for loop is dangerous. Say the string is "helloz." strlen(string1) will return 7. However, string1[7] is out of bounds.
    Actually it's not.

    string1[7] is going to be the nul terminator. And don't say "what if there isn't one", because then strlen() would segfault.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    I take it that using strstr() wouldn't be allowed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by zacs7 View Post
    > This for loop is dangerous. Say the string is "helloz." strlen(string1) will return 7. However, string1[7] is out of bounds.
    Actually it's not.

    string1[7] is going to be the nul terminator. And don't say "what if there isn't one", because then strlen() would segfault.
    My mistake. Thank you for correcting me. But in my defense, there's no reason to compare the null terminator.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > there's no reason to compare the null terminator.
    There certainly isn't... good pickup none-the-less.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    I think its best not to use strstr?

    can get the program to check for number of times the code (user input)..
    anyone can help?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    I must also point out: http://cpwiki.sourceforge.net/Implicit_main
    And reading strigs with scanf: (See signature)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 11:36 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Replies: 1
    Last Post: 10-31-2005, 10:36 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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