Equivelant to Left or Right function of VB?

This is a discussion on Equivelant to Left or Right function of VB? within the C Programming forums, part of the General Programming Boards category; I need to get the 3 letters right of a string, the extension. Is there a function in string.h that ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    Question Equivelant to Left or Right function of VB?

    I need to get the 3 letters right of a string, the extension. Is there a function in string.h that can do that??
    I used Right and Left in Visual Basic.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I don't believe there's a standard one, but it'd be easy enough to build one yourself.......
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    No (In ANSI C atleast). But you can write your own with very little ease using the standard string functions.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Has anyone made something similar? I dont want the actual code, but which functions did you use to make it?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  5. #5
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >Has anyone made something similar? I dont want the actual code, but which functions did you use to make it?
    It depends on the way you implement it. strlen() is one which is real helpful.
    One way of implementing is, extract all the characters individually from the string in the range: [strlen(string) - 4] to [strlen(string)]

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just remember to account for strings that don't have enough characters (ie empty ones)........
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well if you start with

    char filename[] = "wibble.txt";

    Then you can locate the dot with

    char *p = strchr( filename, '.' );

    Or locate the .txt with

    char *p = strstr( filename, ".txt" );

    In both cases, p either points to the dot, or p is NULL indicating that nothing was found.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Oh thank you Salem, it worked strchr was the solution i needed!
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Unless you're using DOS (or potentially some other OS), you don't know for sure that there is only one "." in the file name.

    Example: In *nix, it's common to have:

    ".tar.gz" on the end of the file.

    One way is simply to loop through the string from the end. (Basicly strchr does the same thing, just from the beginning.)
    Code:
    for( x = strlen( s ) -1; x > -1; x-- )
    {
        if( s[x] == charToFind ) break;
    }
    Now, you could use strchr. Just use it with more than one character. Or, even better, use it in a loop:

    Code:
    c = strchr( string, tofind );
    for( temp = c; temp != NULL; temp = strchr( c, tofind ) )
        if( temp != NULL ) c = temp;
    That should do it.

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

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Correct me if I'm wrong, but wouldn't it be simpler to use strrchr().
    For example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char x[] = "filename.txt";
    	char *p;
    	
    	if ((p = strrchr(x, '.')) == NULL)
    		printf ("No extension\n");
    	else
    		printf ("Extension is %s\n", p+1);
    	
    	return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure. That'd work. However, are either of those two functions in the standard? My reference may be outdated, but it doesn't liste them as ANSI functions... (While it may be no big deal to the original poster, I'm generally pick regarding this.)

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

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Yes they are ANSI functions

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Salem
    Yes they are ANSI functions
    I need updated man pages then...

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

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by quzah
    Sure. That'd work. However, are either of those two functions in the standard? My reference may be outdated, but it doesn't liste them as ANSI functions...
    What 2 functions? I only used strrchr() and printf()? !! (I'm just being sarcastic, I guess you meant strchr() )

    Also, if your documentation doesn't liste them, it's probable that you have Ye Olde English version... I definately recommend updating.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I just use the man pages while at work for quick reference. Some one needs to update them. (They don't list strchr and strrchr as conforming to ANSI.) Still, over all they're a pretty good reference.

    (I've never seen a compiler that didn't have strchr, and had never looked for or used strrchr--actually, I very seldom need either function [never in the case of strrchr]--but I find it's better to not assume something is ANSI and find out later that it isn't.)

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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