Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Getting middle of string (http://www.programmingforums.org/showthread.php?t=10164)

Prm753 Jun 3rd, 2006 6:32 PM

Getting middle of string
 
Hi again.

I have searched high and low for this but have found nothing really pertaining to what I want to do. Is there a function that will let you get at a specified position in a string? Not at the beginning or end, but at say, the middle. For example, if I had the string: "My name is Paul." and I wanted to get to the 6th character, how might I go about doing that if there is no such function to do it? (Without manually changing the string in the compiler)

Thanks in advance. :)

Jessehk Jun 3rd, 2006 6:47 PM

All I could find was a function that returns a pointer to a sub-string.

http://www.cplusplus.com/ref/cstring/strstr.html

Would that help? :)

Here's an example:

:

#include <stdio.h>
#include <string.h>

int main(void) {
        char str[] = "Hello, world!";

        char *subStr = strstr(str, "world");
        printf("%s, %s\n", "Goodbye", subStr);

        return 0;
}

/* OUTPUT:
 * Goodbye, world!
 */


Animatronic Jun 3rd, 2006 7:16 PM

"My name is Paul."[5] returns the sixth character, but maybe notwhat you ment.

Serinth Jun 3rd, 2006 7:17 PM

say you had a string str: "foshizzle", why don't you just strlen and then divide that by 2 and then printf str[half the value of strlen]

Adak Jun 3rd, 2006 7:38 PM

Quote:

Originally Posted by Prm753
Hi again.

I have searched high and low for this but have found nothing really pertaining to what I want to do. Is there a function that will let you get at a specified position in a string? Not at the beginning or end, but at say, the middle. For example, if I had the string: "My name is Paul." and I wanted to get to the 6th character, how might I go about doing that if there is no such function to do it? (Without manually changing the string in the compiler)

Thanks in advance. :)

Use pointer arithmetic to increment from the string's base address, up to whatever letter you want.

You're not searching for a substring, just "walking" up the string, right?

Adak

DaWei Jun 3rd, 2006 7:44 PM

Your question isn't really clear. That's why all your answerees are saying, "you meant so-and-so, right?) If you mean, find a substring, Jesse has what you need (unless you just want to roll your own). As Animatronic says, if you just want to get at a specified position in a string, what's wrong with myString [specifiedPosition]? If you want to find a character, there are such things as strchr and ilk. Pay no attention to Serinth's solution, which is just trash. I see no reason to use pointer arithmetic to increment a string's pointer up to what you want when you can go straight there, if you know what you want. If you don't know what you want, you're up shit creek, anyway.

Serinth Jun 3rd, 2006 7:48 PM

what's wrong with mine? it's basically saying use str[position] like you said.

edit: Yeah it's probably the unclear question :\

DaWei Jun 3rd, 2006 7:49 PM

No, it isn't.

jim mcnamara Jun 5th, 2006 10:05 AM

Quote:

Originally Posted by DaWei
No, it isn't.

Pending flame war? hope not.

To the OP:
C isn't like VB - it has a limited number of primitive functions. So if you want a substr function you have to write it.
:

/*
    this will work only for null terminated strings, and cannot test the size
    of the destination buffer, ie., (you know) len has to be less than the
    # of chars dest can hold.
*/
char *substr(char *dest, char *src, size_t start, size_t len)
{   
    char *p=src;
    char *q=dest;
    size_t i=0;

    while(i<start)
    {
        if(!*p) return dest;
        p++;
        i++;
    }
    for(i=0;i<len && *p;i++) *q++=*p++;
    *q=0x0;
    return dest;
}


http://www.cs.cf.ac.uk/Dave/C/chapter2_21.html
lists functions that most C compilers offer.

DaWei Jun 5th, 2006 10:53 AM

Quote:

Pending flame war? hope not.
Quote:

No, it isn't.
The sally wouldn't have been nearly that mild.


All times are GMT -5. The time now is 8:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC