Strings in C, how to get subString

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

I have a string:

char * someString;

If I want the first five letters of this string and want to set it to otherString, how would I do it?

回答1:

#include <string.h> ... char otherString[6]; // note 6, not 5, there's one there for the null terminator ... strncpy(otherString, someString, 5); otherString[5] = '\0'; // place the null terminator


回答2:

char* someString = "abcdedgh"; char* otherString = 0;  otherString = (char*)malloc(5+1); memcpy(otherString,someString,5); otherString[5] = 0;

UPDATE:
Tip: A good way to understand definitions is called the right-left rule (some links at the end):

Start reading from identifier and say aloud => "someString is..."
Now go to right of someString (statement has ended with a semicolon, nothing to say).
Now go left of identifier (* is encountered) => so say "...a pointer to...".
Now go to left of "*" (the keyword char is found) => say "..char".
Done!

So char* someString; => "someString is a pointer to char".

Since a pointer simply points to a certain memory address, it can also be used as the "starting point" for an "array" of characters.

That works with anything .. give it a go:

char* s[2]; //=> s is an array of two pointers to char char** someThing; //=> someThing is a pointer to a pointer to char. //Note: We look in the brackets first, and then move outward char (* s)[2]; //=> s is a pointer to an array of two char

Some links: How to interpret complex C/C++ declarations and How To Read C Declarations



回答3:

Generalized:

char* subString (const char* input, int offset, int len, char* dest) {   int input_len = strlen (input);    if (offset + len > input_len)   {      return NULL;   }    strncpy (dest, input + offset, len);   return dest; }  char dest[80]; const char* source = "hello world";  if (subString (source, 0, 5, dest)) {   printf ("%s\n", dest); }


回答4:

You'll need to allocate memory for the new string otherString. In general for a substring of length n, something like this may work for you (don't forget to do bounds checking...)

char *subString(char *someString, int n)  {    char *new = malloc(sizeof(char)*n+1);    strncpy(new, someString, n);    new[n] = '\0';    return new; }

This will return a substring of the first n characters of someString. Make sure you free the memory when you are done with it using free().



回答5:

strncpy(otherString, someString, 5);

Don't forget to allocate memory for otherString.



回答6:

#include <stdio.h> #include <string.h>  int main () {         char someString[]="abcdedgh";         char otherString[]="00000";         memcpy (otherString, someString, 5);         printf ("someString: %s\notherString: %s\n", someString, otherString);         return 0; }

You will not need stdio.h if you don't use the printf statement and putting constants in all but the smallest programs is bad form and should be avoided.



回答7:

Doing it all in two fell swoops:

char *otherString = strncpy((char*)malloc(6), someString); otherString[5] = 0;


回答8:

char largeSrt[] = "123456789-123";  // original string  char * substr; substr = strchr(largeSrt, '-');     // we save the new string "-123" int substringLength = strlen(largeSrt) - strlen(substr); // 13-4=9 (bigger string size) - (new string size)   char *newStr = malloc(sizeof(char) * substringLength + 1);// keep memory free to new string strcpy(newStr, largeSrt, substringLength);  // copy only 9 characters  newStr[substringLength] = '\0'; // close the new string with final character  printf("newStr=%s\n", newStr);  free(newStr);   // you free the memory 


回答9:

I think it's easy way... but I don't know how I can pass the result variable directly then I create a local char array as temp and return it.

char* substr(char *buff, uint8_t start,uint8_t len, char* substr) {     strncpy(substr, buff+start, len);     substr[len] = 0;     return substr; }


回答10:

You can use snprintf to get a substring of a char array with precision. Here is a file example called "substring.c":

#include <stdio.h>  int main() {     const char source[] = "This is a string array";     char dest[17];      // get first 16 characters using precision     snprintf(dest, sizeof(dest), "%.16s", source);      // print substring     puts(dest); } // end main

Output:

This is a string

Note:

For further information see printf man page.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!