How do I get a substring in C? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
For example if:
z = "xxxx; yyyy";
How can I get the substrings so that
x = "xxxx"
and
y = "yyyy"
where "xxxx" and "yyyy" can be any string of any length?

You don't get much of built-in strings in C, let alone substrings. When you need a substring, you build it yourself by copying relevant portions of the string into a properly allocated memory buffer, and then you null-terminate the result.
Here is an example:
char *c = "xxxx; yyyy";
char x[5], y[5];
memcpy(x, &c[0], 4);
x[4] = '\0';
memcpy(y, &c[6], 4);
y[4] = '\0';
Demo.

Related

strlen(null_terminated_array) does printf("%zu", strlen(null_terminated_array)) prints size_t that includes '\0' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If I have a char *null_terminated_array="hello", I think printf("%zu",strlen(null_terminated_array)) should print 5 (not including '\0') and not 6 (including '\0').
Yes that is what you would expect.
Just be cautious if you were tempted to use sizeof rather than strlen on the array:
char *null_terminated_array="hello";
char char_array[6]="hello";
printf("null_terminated_array = %s\n",null_terminated_array);
printf("strlen(null_terminated_array) = %zu\n",strlen(null_terminated_array));
printf("sizeof(null_terminated_array) = %zu\n",sizeof(null_terminated_array));
printf("\n");
printf("char_array = %s\n",char_array);
printf("strlen(char_array) = %zu\n",strlen(char_array));
printf("sizeof(char_array) = %zu\n",sizeof(char_array));
for the platform that I ran this on this gives
null_terminated_array = hello
strlen(null_terminated_array) = 5
sizeof(null_terminated_array) = 8
char_array = hello
strlen(char_array) = 5
sizeof(char_array) = 6
The apparent discrepancy for sizeof(null_terminated_array) is to do with automatic memory allocation using blocks of 32bits (platform dependent).

How to split a string into 3 parts in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
the question is the following: "How do I split a string into 3 parts in C?"
The string is something similar to the following: "Roberta$$$Anna$$$$$$Massimo$$$"
I need to split it exactly after 10 characters (Roberta$$$, Anna$$$$$$, Massimo$$$), and please notice that they aren't separated by a spacebar, so I think I cannot use the strtok function or the library string.h to split them.
char source[] = "Roberta$$$Anna$$$$$$Massimo$$$";
char part1[11];
char part2[11];
char part3[11];
memmove(part1, &source[ 0], 10);
part1[10] = '\0';
memmove(part2, &source[10], 10);
part2[10] = '\0';
memmove(part3, &source[20], 10);
part3[10] = '\0';
You could use strncpy() or memcpy() instead of memmove().

C- Comparison operator instead of strcmp [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
What will happen if I use comparison operators to compare strings instead of strcmp in C? Will it compare its ASCII value and return results?
It will compare the addresses of the two pointers.
so:
char* a = "hello";
char* b = "test";
char* c = "hello";
char* d = a;
a == d; // true
a == b; // false
a == c; // true or false, depending on the compiler's behavior.
The third example will be true if the compiler decides to recycle the actual string data for "hello", but it has no obligation to do so.

Change value pointed to by strstr(); C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to make a program to find and replace some text in a string at the moment I'm trying to change "hello how are you" to "hello bow are you" as a test.
So firstly I find the "how" by using char *substring = strstr(mystring, newstr);
which returns a pointer to "(this position)how are you" now I have no idea how to change the next 3 letters. I can strlen(newstr) for the length of the string I'm replacing "how" with but I can't find a way to change mystring starting from the pointer newstr.
Change the first character by subscripting the substring.
substring[0] = 'b';
If you want to replace multiple characters, try a loop, or use memcpy. Don't use strcpy: you don't want the NUL terminator to be copied.
memcpy(substring, "how", 3);
*substring = 'b'; as posted by user EOF my solution was as so
for (int x = 0; x < strlen(newstring); x++){
*substring++ = newstring[x];
}

How to extract specified character string from string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
char string[]="DGS021J0W0S1000.0S20000S3000.0S4000.0S50.00S60.00F";
how to get S[1-5]
thanks!
Use strncpy() standard function
char S[6] = {0};
strncpy(S, string+1 , 5);
If you want to copy from the beginning of the string to the 5th charachter, then your question should be
how to get S[0-4]
and not S[1-5] because array index in C start from 0 and not from 1. and the solution for this case will be
char S[6] = {0};
strncpy(S, string , 5);
I think you are looking for substring methods.
You can do it in two for loops in C.

Resources