string splitting logic in C programing [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 months ago.
Improve this question
Could you please provide me some code or solution how to split the below string in C programming
Sample string :
SMABCDEFGHIJK,887276617459,5,552612260849779,552612260840646,552612260843632,552612260843525,552612260846817
Output needed :
552612260849779,552612260840646,552612260843632,552612260843525,552612260846817
Basically for the input string we would need to Ignore first 3 positions and want rest of the string in different variable.
The positions to ignore and delimiter values will get the from the database table label.
So, If someone help me to give the logic that would be very helpful

In your case, you do not need to split the string. Simply ignore everything before the n-th occurrence of the delimiter.
char *ignoreFisrstN(const char *str, int delim, size_t ignoreCount)
{
char *result = NULL;
while(ignoreCount--)
{
if((str = strchr(str, delim))) str++;
else break;
}
if(str)
{
result = malloc(strlen(str) + 1);
if(result) strcpy(result, str);
}
return result;
}
int main(void)
{
char *s = "SMABCDEFGHIJK,887276617459,5,552612260849779,552612260840646,552612260843632,552612260843525,552612260846817";
for(size_t i = 0; i < 10; i++)
{
char *r = ignoreFisrstN(s, ',', i);
printf("%zu: `%s`\n", i, r ? r : "NULL");
free(r);
}
}
https://godbolt.org/z/xWe74sY46

Related

How to replace a multi-character character constant with one character [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 3 years ago.
Improve this question
I'm attempting to replace '%7C' with a '|' in C but i'm getting a multi-character character constant warning. I was wondering if it was possible to do this and if so how? I was attempting using the code below but it gave this warning.
Parse.c
char *parse(char *command){
char * newCommand = (char *)malloc(sizeof(char)*35);
newCommand = strtok(command, " ");
newCommand = strtok(NULL, "/run?command= ");
for(int i = 0; i<strlen(newCommand); i++){
if(newCommand[i] == '+')
{
newCommand[i] = ' ';
}
if(newCommand[i] == '%7C')
{
newCommand[i] = '|';
}
}
return newCommand;
}
Multi-character constants are not portable and should generally be avoided. Your code comes under the 'general' category.
Part of the solution to your problem is to do a string comparison (with strncmp):
if (strncmp(&newCommand[i], "%7C", 3) == 0)
{
newCommand[i] = '|';
}
However, you also need to remove the 7C. That requires more surgery on the loop:
int tgt = 0;
int len = strlen(newCommand);
for (int src = 0; src < len; src++)
{
if (newCommand[src] == '+')
{
newCommand[tgt++] = ' ';
}
else if (strncmp(newCommand[i], "%7C", 3) == 0)
{
newCommand[tgt++] = '|';
src += 2;
}
else
newCommand[tgt++] = newCommand[src];
}
newCommand[tgt] = '\0';
This maintains two indexes into the newCommand array, one from which you're reading (src) and one to which you're writing (tgt — dst would be an alternative name). The src += 2; skips over the 7C after replacing % with |.
Uncompiled code!
Also, in your function you have:
char *newCommand = (char *)malloc(sizeof(char)*35);
newCommand = strtok(command, " ");
This immediately leaks the allocated memory. Maybe you need to use strdup() or:
char *newCommand = malloc(strlen(command) + 1);
if (newCommand == NULL) …report error and bail out…
strcpy(newCommand, command);
And the next line:
newCommand = strtok(NULL, "/run?command= ");
splits on any sequence of any of the characters in the constant string; it does not look for that string. If you want to look for the string, then you need strstr() instead, and you need to run strtok() first, perhaps, to get the right starting point (maybe newCommand = strtok(NULL, ""), then char *end = strstr(newCommand, "/run?command= "); — and check for null pointers returned.
With the revised allocation, you need a new symbol to record the pointers returned by strtok() — such as char *token;.
All in all, there's a lot of work needed on your code.

null compare in C language [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 6 years ago.
Improve this question
i already have this code in C language, is a string of 15 data delimited by commas the code split the string by comma and store every data in a single variable called array[], the issue is that i have the last data comma if after the last comma there are no data then the variable x15 = 0, but if there are a value after the last , then convert that value to a int. i print the value of the array[15] to verify and this is null, so i out a condition for that but do not work, the program just break after compiling.
char buf[] ="¶bL0 L3,01,+08590323,-079343001,010215,00000000000000,-tN,000,012689997,001219456,000,7FF2,C07F,0,4,";
printf("\n \n string=[%s]\n\n", buf);
int i = 0;
int u;
char *p = strtok (buf, ",");
char *array[16];
char *y15;
while (p != NULL)
{
array[i++] = p;
p = strtok (NULL, ",");
}
for (i = 0; i <16; ++i){
if(array[15] == NULL){
wbt.x15=0;
}else{
wbt.x15=atoi(array[15]);
}
//printf("data: [%s]\n", array[i]);
}
You are using an uninitialized array element, which is cause for undefined behavior.
You have:
char *array[16];
The elements of the array are uninitialized. And then, you proceed to use:
if(array[15] == NULL){
wbt.x15=0;
}else{
wbt.x15=atoi(array[15]);
}
It's not clear why you have that check for every iteration of the loop but that's another problem. The problem with the posted code is that array[15] is not initialized. Using that value is a problem.
Make sure you initialize array properly. Use:
char *array[16] = {0};
Also, I think your for loop needs to be something like:
for (i = 0; i <16; ++i)
{
int x = 0;
if(array[i] != NULL)
{
x = atoi(array[i]);
}
// Now use x.
}

Copying string from source printing strange string of characters [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Im making a lexer and I'm trouble copying a string from my file buffer to the string property of the constructor. Here is the code i'm using to copy a string from the buffer.
static token_t* lexer_str(lexer_t* lexer) {
size_t str_len = 0;
while (true) {
if (lexer->len < 1) {
error_new(lexer->errors, lexer->len, lexer->pos, "Unterminated string.");
return NULL;
} else if (lexer_look(lexer, 0) == '\"') {
lexer_adv(lexer, 1);
break;
} else {
lexer_adv(lexer, 1);
str_len++;
}
}
char* string = malloc(str_len);
for (size_t idx = 0; idx < str_len; idx++)
string[idx] = lexer->src[lexer->ptr - str_len + idx];
token_t* token = token_new(lexer, _str);
token->string = string;
return token;
}
And here is the buffer.
"la la la" "me me me"
and here is the output, the string is coming out as "²²²²\"
Type:0 {
Line: 1
Pos: 0
Number: 10715872
Real: 10715872
String: ²²²²\
}
Why is this happening? Is it just me reading memory from the wrong place. Any help for how I could correctly copy the string into the token would be appricated.
First char* string = malloc(str_len); is too short, and your string is not null terminated after the copy (you copy a buffer given offset and len, the buffer does not contain an ending null char)
change to:
char* string = malloc(str_len+1); // 1 byte more
for (size_t idx = 0; idx < str_len; idx++)
string[idx] = lexer->src[lexer->ptr - str_len + idx];
string[str_len] = '\0'; // don't forget to null-terminate
If the source is, say, empty, then you have a non-null terminated string in string

Parsing string pointer in C - Troubleshoot [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am new to C
I am trying to parse the string by "spaces" and "commas", string that *ch pointing to, but I am getting the first element only. Not sure what I am doing wrong, and I have wasted my whole day already on this, but still couldn't figure out.
#include <stdio.h>
#include <string.h>
int main(){
char *ch = "This is a string, and fyunck you.";
char cmd[100], *temp;
int i = 0, size_ch = strlen(ch), count = 0;
/* as strtok only support string array */
for (i = 0; i < size_ch; i++){
if (ch[i] != ','){
cmd[count] = ch[i];
count++;
}
}
cmd[count] = '\0';
printf("cmd: %s\n", cmd);
ch = strtok(cmd, " ");
printf("ch: %s\n", ch);
while ( (ch = strtok(NULL, " ")) != NULL)
printf("%s\n", cmd);
}
Output
cmd: This is a string and fyunck you
ch: This
This
This
This
This
This
This
whereas, the output should be
Desire Output
cmd: This is a string and fyunck you
ch: This
is
a
string
and
fyunck
you
Note: I am not allowed to use external libraries.
P.S I am trying to replicate this code, Code
You're just printing the wrong variable in the last line.
Change
printf("%s\n", cmd);
to
printf("%s\n", ch);
and it should be fine.
Note this:
while ((ch = strtok(NULL, " ")) != NULL)
printf("%s\n", cmd);
You are updating ch, and outputting cmd, which remains unchanged.
To fix this, simply change it to:
while ((ch = strtok(NULL, " ")) != NULL)
printf("%s\n", ch);

Using a replace function in C programming [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How do I write a replace function which takes a pointer to a string as a parameter and replaces all spaces in that string with minus signs?
example :
input = "I love pies", output = "I-love-pies"
This is what you need:
#include <stdio.h>
int change_string(char *input) {
for (char *p = input; *p; p++) {
if (*p == ' ') *p = '-';
}
return 1;
}
int main() {
char input[] = "I love pies";
printf("%s\n", input);
change_string(input);
printf("%s\n", input);
return 0;
}

Resources