Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 3rd, 2006, 5:32 PM   #1
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
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.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Jun 3rd, 2006, 5:47 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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!
 */
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jun 3rd, 2006, 6:16 PM   #3
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
"My name is Paul."[5] returns the sixth character, but maybe notwhat you ment.
Animatronic is offline   Reply With Quote
Old Jun 3rd, 2006, 6:17 PM   #4
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 4 Serinth is on a distinguished road
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]
__________________
A girl talked to me once.

http://www.latestanime.com
Serinth is offline   Reply With Quote
Old Jun 3rd, 2006, 6:38 PM   #5
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 57
Rep Power: 0 Adak is an unknown quantity at this point
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
Adak is offline   Reply With Quote
Old Jun 3rd, 2006, 6:44 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 3rd, 2006, 6:48 PM   #7
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 4 Serinth is on a distinguished road
what's wrong with mine? it's basically saying use str[position] like you said.

edit: Yeah it's probably the unclear question :\
__________________
A girl talked to me once.

http://www.latestanime.com
Serinth is offline   Reply With Quote
Old Jun 3rd, 2006, 6:49 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
No, it isn't.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 5th, 2006, 9:05 AM   #9
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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.
jim mcnamara is offline   Reply With Quote
Old Jun 5th, 2006, 9:53 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Pending flame war? hope not.
Quote:
No, it isn't.
The sally wouldn't have been nearly that mild.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:08 PM.

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