Thread: String length from the middle?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    String length from the middle?

    is it possible to figure out the string length to a certian part?

    i mean if in a string you had testing, strlen would give back 7. Could i ask it the length to the first t? could i do it with memory address.

    also i dont want to just add a '\0' to a string position, i want the strings to be unchanged at the end.
    Last edited by mart_man00; 03-09-2003 at 01:18 PM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    This will find the number of chars to the word 'string' in Text, is this what you were looking for?

    Code:
    #include <stdio.h>
    #include <string.h>
         
    int main()
    {
       char Text[] = "is it possible to figure out the string length to a certian part?";
    
       char *s1 = strstr(Text, "string");
       int len =  s1-Text;
       printf("Length to 'string' = %d\n", len);
       return 0;
    }

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    will this work for you?

    Code:
    #include <stdio.h>
    
    int main()
    {
      char str[] = "where is this >!< located?";
      int length, i;
     	
      length = 0;
      for(i=0; str[i] != '\0'; i++)
     {
        if(str[i] == '!')
           { length++; break; }
     	  
       length++;
     }
     	
     printf("%s\n", str);
     	
     for(i=0; i < length-1; i++)
       putchar(' ');
        
     putchar('^');
        
     printf("\n%c found at position %d",
               str[length-1], length);
     	  
     return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i want output back like that, but it seems like its cheating. what if "string" is in a string more than once?

    i mean i have something like this

    Code:
    char text[] = "is it possible to figure out the string length to a certian part?";
    char *spot = text;
    ++*spot;
    searching for a strings seems like its a long and slower way of doing it. but your way is a good fake.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    I'm still not 100% sure what you are looking for? What is the cryteria for finding the position in the text. There's not a quick way of searching thought strings in C. Here are a few more examples.
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
       char text[] = "is it possible to figure out the string length to a    certian part?";
       char *spot = text;
    
       while(*spot && *spot != 'f')
    		++spot;
       printf("Count to 'f' = %d\n", spot-text);
    
       spot = text;
       char* find = "figure";
       while(*spot && strncmp(spot, find, strlen(find)))
    		++spot;
       printf("Count to 'figure' = %d\n", spot-text);
       printf("The rest of the text = %s\n", spot);
       printf("strlen from 'figure' = %d\n", strlen(spot));
       return 0;
    }
    Last edited by Scarlet7; 03-09-2003 at 02:59 PM.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    all i want it to beable to do is use a memory address like a '\0' with out changing the strings

    i tried subtracting addresses but i got a error(i did this on my windows laptop, my linux desktop fan died and it over heats to quick).

  7. #7
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    You've still lost me? The '\0' in a string indicates the end of the string. You say you want to use it? if need to know
    the address of '\0' its:
    Code:
    char string[] = "a string"
    char *address = string + strlen(string);
    If yo'ure trying to use memory address = '\0' = 0 then that will crash the program.
    Post some code.
    Last edited by Scarlet7; 03-09-2003 at 04:16 PM.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    !@#$%^&*

    char temp[] = "this is a string, just a string";
    char *spot = temp;
    *spot += 3;


    now, if i call strlen(temp) i get 31. strlen goes threw the string until it finds a \0. while its doing this it increments a var to return.

    the probelm with that is it will only stop when it fines a \0. i would like to calculate length of temp to a memory address not a \0.

    i dont want to use spot as a string and call strstr and find the start of it (but, that would work), that seems like more over head thats not really needed.

    i tried subtracting addresses

    int strlento(char *start, char *end) {
    return (&end - &start);
    }

    but i get a crash.

    any one get the idea?

  9. #9
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    The problem is that you're using the address of pointers to calculate, which is a pointer to a pointer. Remove the & as 'end' and 'start' are already pointers.

    Code:
    int strlento(char *start, char *end) { 
    return (end - start);
    }

    > i would like to calculate length of temp to a memory address not a \0.

    I don't know understand what you mean?
    Last edited by Scarlet7; 03-09-2003 at 05:21 PM.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    with out the &s i get NULL returned.

    > i would like to calculate length of temp to a memory address not a \0.

    I don't know understand what you mean?
    !@#$%^&*

    NOW WITH STRLEN IT GIVES BACK THE LENGTH OF THE STRING BY LOOKING FOR A \0 AND COUTING HOW MANY TIMES IT DIDNT HIT IT. I WOULD LIKE IT TO STOP AT A CERTIAN MEMORY ADRRESS, NOT JUST \0. SO I WANT TO MAKE A ADDRESS LIKE \0 BUT NOT \0.

    sorry, but this is driving me nuts. this function probally has the simplest thing wrong and i still cant see it

  11. #11
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Any closer?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    // count to address or '\0' which ever comes first
    int my_strlen(char *text, char *PleaseStopCountingAtThisAddress)
    {
    	char *start = text;
    	while(*text && text != PleaseStopCountingAtThisAddress)
    		text++;
    	return text-start;
    }
    int main()
    {
       char text[] = "is it possible to figure out the string length to a certian part?";
    
       char *PleaseStopCountingAtThisAddress = &text[25]; // 'o'	in out
       int len = my_strlen(text, PleaseStopCountingAtThisAddress);
    
       printf("len = %d  char at PleaseStopCountingAtThisAddress = %c\n", len, *PleaseStopCountingAtThisAddress);
    
    //OR
    
       len = PleaseStopCountingAtThisAddress-text;
       printf("len = %d\n", len) ;
    }
    If not post your code
    Last edited by Scarlet7; 03-09-2003 at 06:29 PM.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Are you after strcspn() ?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int my_strlen(const char *s, char c)
    {
      int len = 0;
      while (*s && *s != c) {len++; s++;}
      return len;
    }
    
    int main(void)
    {
      char s1[] = "This is a ! test";
      puts (s1);
      printf ("length to ! is %d\n", my_strlen(s1, '!'));
      printf ("length to ! using strcspn() is %d\n", strcspn(s1, "!"));
      return(0);
    }
    
    /*
    This is a ! test
    length to ! is 10
    length to ! using strcspn() is 10
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    no, thats looking for a character. i know the memory address. i get errors when i subtract the address.

    i know some one can do this.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int lengthto( const char * s, int tofirst )
    {
        char * ptr = strchr( s, tofirst );
        if( ptr )
            return ptr-s;
        return -1;
    }
    How hard is that?

    [edit]
    Code:
    int fromtolength( const char *s, int start, int tonext )
    {
        char *ptr = strchr( s+start, tofirst );
        if( ptr )
            return ptr-(start+s);
        return -1;
    }
    There. There's another version.
    [/edit]
    Quzah.
    Last edited by quzah; 03-10-2003 at 05:26 PM.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    char * ptr = strchr ( s, tofirst );
    why are you using strchr to SEARCH!!!. if it so dam easy why cant some on do it with out searching. how many times have i said that already. i dont mine if other people cant do it(we already know i cant), how many ..........ing times does it need to be said!!! DONT USE A SEARCH!!!!IT WILL JUST GIVE ME THE FIRST OCCURANCE AND IT MAYBE THE SECOND OR THIRD OR WHATEVER-TH!!!! USE ADDRESS AND IF POSSIBLE TELL ME WHY MY ATTEMPT TO SUBTRACT MEMORY ADDRESS FAILED!!!!

    ive been looking at this for way to long...........................................

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM