Easy String position question...

This is a discussion on Easy String position question... within the C Programming forums, part of the General Programming Boards category; Been out of the programming loop for awhile, and trying to ease myself back in. My first self appointed project ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    37

    Easy String position question...

    Been out of the programming loop for awhile, and trying to ease myself back in. My first self appointed project has me stumped so thought I'd fire off a quick easy question for all the pros here on the board.

    I have a string that i have a variable in, its 26 characters long. I want to read say the 10th character in. Is there a relatively easy way to find that? Here's what i have so far:

    Code:
    #include <stdio.h>
    
    int main()
    {
            char test[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            char single_test[] = " ";
    
            clrscr();
    
            /* Print test characters */
            printf("Variable: %s\r\n", test);
    
            /* Code here to go 10 characters out, and set the variable */
     
       /*  for ( i = 0; i < 10; ++i ) {
            single_test = strsomething  
    
            } */
        
            /* Print 10th character */
            printf("The 10th character is: %s\r\n", single_test);
    
            getch();
    
    }
    Any help would be appreciated.
    Last edited by Striph; 05-11-2009 at 04:46 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you want a single character, you want to use %c. You can use a character pointer, and increment it. Or you can use array[ x ] to find the spot. Remember that array indexes start at zero, not 1.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Just enough to jumpstart my brain. Thanks! Basically i have a 24x80 map that a person can move around. I wanted to be able to find their position on the screen, and if that position was equal to a T, then do something. I think this below will work out.


    Code:
    #include <stdio.h>
    
    int main()
    {
            char test[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            char single_test[] = " ";
            int i;
    
            clrscr();
    
            printf("Variable: %s\r\n", test);
    
            /* Code here to go 10 characters out, and set the variable */
    
            for (i = 0; i < 26; i++) {
            if (test[i] == 'T')
            printf("The %dth character is: %c\r\n", i + 1, test[i]); }
    
            getch();
    
    }
    Works great in this setup, just need to plug it into my movement routine.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    example for find character in array and print its index (somewhere I was write it here already)
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Спасибо

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

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