![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
|
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 |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
"My name is Paul."[5] returns the sixth character, but maybe notwhat you ment.
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 4
![]() |
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]
|
|
|
|
|
|
#5 | |
|
Hobby Coder
Join Date: May 2006
Posts: 57
Rep Power: 0
![]() |
Quote:
You're not searching for a substring, just "walking" up the string, right? Adak |
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 4
![]() |
what's wrong with mine? it's basically saying use str[position] like you said.
edit: Yeah it's probably the unclear question :\ |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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. /*
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. |
|
|
|
|
|
|
#10 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
Quote:
__________________
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 |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|