![]() |
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. :) |
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> |
"My name is Paul."[5] returns the sixth character, but maybe notwhat you ment.
|
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]
|
Quote:
You're not searching for a substring, just "walking" up the string, right? Adak |
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.
|
what's wrong with mine? it's basically saying use str[position] like you said.
edit: Yeah it's probably the unclear question :\ |
No, it isn't.
|
Quote:
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. :
/* http://www.cs.cf.ac.uk/Dave/C/chapter2_21.html lists functions that most C compilers offer. |
Quote:
Quote:
|
| 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