C - Split string with repeated delimiter char into 2 substrings - c

I'm making a very simple C program that simulates the export command, getting an input with fgets().
Input example:
KEY=VALUE
Has to be converted to:
setenv("KEY", "VALUE", 1);
That's easy to solve with something similar to this code:
key = strtok(aux, "=");
value = strtok(NULL, "=");
The problem comes when the user input a value that start with one or several equals = characters. For example:
KEY===VALUE
This should be converted to:
setenv("KEY", "==VALUE", 1);
But with my current code it is converted to:
setenv("KEY", NULL, 1);
How I can solve this?
Thanks in advice.

Your second strtok() should not use = as the delimiter. You would only do that if there were another = that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.
key = strtok(aux, "=");
value = strtok(NULL, "");

strtok is probably overkill (and non-reentrant) when it's just one token. This will do,
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
char *key, *equals, *value;
if(argc != 2 || !(equals = strchr(key = argv[1], '=')))
return fprintf(stderr, "KEY=VALUE\n"), EXIT_FAILURE;
value = equals + 1;
*equals = '\0';
printf("key: <%s>; value: <%s>.\n", key, value);
return EXIT_SUCCESS;
}
Although strtok is probably easier to read. One may try strsep, but it is GNU C.

Related

Why does the strchr not take the program into an if condition?

I am trying to run the following code, but during execution, the code does not go into the if condition.
Why does the code not enter the if condition during runtime? I have marked the problem condition.
Running this program on Windows 10.
Thread model: posix
gcc version 5.1.0 (tdm64-1)
I have tried using the ternary operator and the if statement with a different string, and strchr works fine in that case.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main() {
static char str[] = "hello world";
static char inputTime[] = "12:05:10PM";
char *result = strchr(str, 'w');
long int tempNum = 0;
char *token, tempStr[10], delimit[] = ":";
if (strchr(str, 'w'))
printf("\nFound w");
else
printf("\nDid not find w");
(strchr(inputTime, 'P')) ? printf("\nTrue") : printf("\nFalse");
token = strtok(inputTime, delimit);
if (strchr(inputTime, 'P')) {
printf("Found PM\n");
tempNum = strtol(token, NULL, 10);
if (tempNum != 12)
tempNum += 12;
sprintf(tempStr, "%lu", tempNum);
}
printf("\ntempStr: %s", tempStr);
}
The above code gives me this output:
C:\Users\XX\Documents\Tests\c-programming>a.exe
Found w
True
tempStr: σ#
The strtok function splits the given input string into tokens. It does this by modifying the string to tokenize, placing a null byte in place of the delimiter to search for.
So after the call to strtok, inputTime looks like this:
{ '1','2','\0','0','5',':','1','0','P','M','\0' }
A null byte is put in place of the first :. So if you were to print inputTime you would get 12, meaning you won't find a P.
Because the input string is modified, you should search for P before calling strtok.

C - Is this the right way to use strtok in the following situation

If i have a string that contains 10X15. And i want to separate the 10 and 15. Would the following code be correct. I am concerned about the second part of the code, is putting "NULL" there the right thing to do.
char * stringSixrows = strtok(stringSix[0], "X");
char * stringSixcollumns = strtok(NULL, "NULL");
//I put the second null there cause its the end of string, im not sure if its right though.
I'd say the "canonical" way to obtain the "pointer to the remaining string" is:
strtok(NULL, "")
strtok searches for any of the delimiters in the provided string, so if you don't provide any delimiters, it cannot find anything and thus only stops at the end of the input string.
example
#include <stdio.h>
#include <string.h>
int main(void){
char stringSix[] = "10X15";
char *stringSixrows = strtok(stringSix, "X");
char *stringSixcolumns = strtok(NULL, "X");
printf("%s, %s\n", stringSixrows, stringSixcolumns);
return 0;
}
another way
char stringSix[] = "10X15";
char stringSixrows[3];
char stringSixcolumns[3];
char *p;
if(NULL!=(p = strchr(stringSix, 'X')))
*p = '\0';
else {
printf("invalid format\n");
return -1;
}
strcpy(stringSixrows, stringSix);
strcpy(stringSixcolumns, p+1);
printf("%s, %s\n", stringSixrows, stringSixcolumns);
const char *stringSix = "10X15";
int stringSixrows;
int stringSixcolumns;
if(2==sscanf(stringSix, "%dX%d", &stringSixrows, &stringSixcolumns))
printf("%d, %d\n", stringSixrows, stringSixcolumns);
You can use strtol to convert the string to numbers as well as seek to the next string. Below code safely does the intended operation:
char stringSix[] = "10X15";
char * pEnd;
long firstNumber = strtol (stringSix,&pEnd, 10);
pEnd = strtok(pEnd, "");
long secondNumber = strtol (pEnd,&pEnd, 10);

How to extract a substring from a string in C?

I tried using strncmp but it only works if I give it a specific number of bytes I want to extract.
char line[256] = This "is" an example. //I want to extract "is"
char line[256] = This is "also" an example. // I want to extract "also"
char line[256] = This is the final "example". // I want to extract "example"
char substring[256]
How would I extract all the elements in between the ""? and put it in the variable substring?
Note: I edited this answer after I realized that as written the code would cause a problem as strtok doesn't like to operate on const char* variables. This was more an artifact of how I wrote the example than a problem with the underlying principle - but apparently it deserved a double downvote. So I fixed it.
The following works (tested on Mac OS 10.7 using gcc):
#include <stdio.h>
#include <string.h>
int main(void) {
const char* lineConst = "This \"is\" an example"; // the "input string"
char line[256]; // where we will put a copy of the input
char *subString; // the "result"
strcpy(line, lineConst);
subString = strtok(line,"\""); // find the first double quote
subString=strtok(NULL,"\""); // find the second double quote
printf("the thing in between quotes is '%s'\n", subString);
}
Here is how it works: strtok looks for "delimiters" (second argument) - in this case, the first ". Internally, it knows "how far it got", and if you call it again with NULL as the first argument (instead of a char*), it will start again from there. Thus, on the second call it returns "exactly the string between the first and second double quote". Which is what you wanted.
Warning: strtok typically replaces delimiters with '\0' as it "eats" the input. You must therefore count on your input string getting modified by this approach. If that is not acceptable you have to make a local copy first. In essence I do that in the above when I copy the string constant to a variable. It would be cleaner to do this with a call to line=malloc(strlen(lineConst)+1); and a free(line); afterwards - but if you intend to wrap this inside a function you have to consider that the return value has to remain valid after the function returns... Because strtok returns a pointer to the right place inside the string, it doesn't make a copy of the token. Passing a pointer to the space where you want the result to end up, and creating that space inside the function (with the correct size), then copying the result into it, would be the right thing to do. All this is quite subtle. Let me know if this is not clear!
if you want to do it with no library support...
void extract_between_quotes(char* s, char* dest)
{
int in_quotes = 0;
*dest = 0;
while(*s != 0)
{
if(in_quotes)
{
if(*s == '"') return;
dest[0]=*s;
dest[1]=0;
dest++;
}
else if(*s == '"') in_quotes=1;
s++;
}
}
then call it
extract_between_quotes(line, substring);
#include <string.h>
...
substring[0] = '\0';
const char *start = strchr(line, '"') + 1;
strncat(substring, start, strcspn(start, "\""));
Bounds and error checking omitted. Avoid strtok because it has side effects.
Here is a long way to do this: Assuming string to be extracted will be in quotation marks
(Fixed for error check suggested by kieth in comments below)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char input[100];
char extract[100];
int i=0,j=0,k=0,endFlag=0;
printf("Input string: ");
fgets(input,sizeof(input),stdin);
input[strlen(input)-1] = '\0';
for(i=0;i<strlen(input);i++){
if(input[i] == '"'){
j =i+1;
while(input[j]!='"'){
if(input[j] == '\0'){
endFlag++;
break;
}
extract[k] = input[j];
k++;
j++;
}
}
}
extract[k] = '\0';
if(endFlag==1){
printf("1.Your code only had one quotation mark.\n");
printf("2.So the code extracted everything after that quotation mark\n");
printf("3.To make sure buffer overflow doesn't happen in this case:\n");
printf("4.Modify the extract buffer size to be the same as input buffer size\n");
printf("\nextracted string: %s\n",extract);
}else{
printf("Extract = %s\n",extract);
}
return 0;
}
Output(1):
$ ./test
Input string: extract "this" from this string
Extract = this
Output(2):
$ ./test
Input string: Another example to extract "this gibberish" from this string
Extract = this gibberish
Output(3):(Error check suggested by Kieth)
$ ./test
Input string: are you "happy now Kieth ?
1.Your code only had one quotation mark.
2.So the code extracted everything after that quotation mark
3.To make sure buffer overflow doesn't happen in this case:
4.Modify the extract buffer size to be the same as input buffer size
extracted string: happy now Kieth ?
--------------------------------------------------------------------------------------------------------------------------------
Although not asked for it -- The following code extracts multiple words from input string as long as they are in quotation marks:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char input[100];
char extract[50];
int i=0,j=0,k=0,endFlag=0;
printf("Input string: ");
fgets(input,sizeof(input),stdin);
input[strlen(input)-1] = '\0';
for(i=0;i<strlen(input);i++){
if(input[i] == '"'){
if(endFlag==0){
j =i+1;
while(input[j]!='"'){
extract[k] = input[j];
k++;
j++;
}
endFlag = 1;
}else{
endFlag =0;
}
//break;
}
}
extract[k] = '\0';
printf("Extract = %s\n",extract);
return 0;
}
Output:
$ ./test
Input string: extract "multiple" words "from" this "string"
Extract = multiplefromstring
Have you tried looking at the strchr function? You should be able to call that function twice to get pointers to the first and second instances of the " character and use a combination of memcpy and pointer arithmetic to get what you want.

Read from line with sscanf, including whitespaces, breaking on other character

I'm sorry for the sloppy title, but I didn't know how to format my question correctly. I'm trying to read a .txt, of which every line has information needed to fill a struct. First I use fgets to read the line, and then i was going to use sscanf to read the individual parts. Now here is where I'm stuck: normally sscanf breaks off parts on whitespaces, but I need the whitespace to be included. I know that sscanf allows ignoring whitespaces, but the tricky part is that I then need some other arbitrary character to separate the parts. For example, I have to break the line
Carl Sagan~Contact~scifi~1997
up into parts for Author,Name,Genre,year. You can see I need the space in Carl Sagan, but I need the function to break off the strings on the tilde character. Any help is appreciated
If your input is delimited by ~ or for instance any specific character:
Use this:
sscanf(s, "%[^~]", name);
[^ is conversion type, that matches all characters except the ones listed, ending with ]
Here is the sample program for testing it:
#include <stdio.h>
int main(int argv, char **argc)
{
char *s = "Carl Sagan~Contact~scifi~1997";
char name[100], contact[100], genre[100];
int yr;
sscanf(s, "%99[^~]~%99[^~]~%99[^~]~%d", name, contact, genre, &yr);
printf("%s\n%s\n%s\n%d\n", name, contact, genre, yr);
return 0;
}
You need strtok. Use ~ as your delimiter.
See the documentation: http://linux.die.net/man/3/strtok
strtok has some drawbacks but it sounds like it will work for you.
EDIT:
After reading this, it sounds like you can use sscanf cleverly to achieve the same result, and it may actually be safer after all.
#include <stddef.h>
#include <string.h>
#include <stdio.h>
char* mystrsep(char** input, const char* delim)
{
char* result = *input;
char* p;
p = (result != NULL) ? strpbrk(result, delim) : NULL;
if (p == NULL)
*input = NULL;
else
{
*p = '\0';
*input = p + 1;
}
return result;
}
int main()
{
char str[] = "Carl Sagan~Contact~scifi~1997";
const char delimiters[] = "~";
char* ptr;
char* token;
ptr = str;
token = mystrsep(&ptr, delimiters);
while(token)
{
printf("%s\n",token);
token = mystrsep(&ptr, delimiters);
}
return 0;
}
Output :-
Carl Sagan
Contact
scifi
1997

Can't we use strtok on different strings in a single program?

I was trying to write a calculator program and so part of this I need to evaluate an expression.
So I need to perform the operation based on the operator given. I am taking the whole expression into a string.
For example it might be 5+6 or 5*6.
So I have written it in this way:
char input1[20] = "";
char input2[20] = "";
char output[20] = "";
char *arg1= NULL, *arg2 = NULL;
int value;
getinput ( input1); //Function for getting the expression
strcpy (input2, input1);
if ( arg1 = strtok (input1, "*"))
{
arg2 = strtok (NULL, "");
value = atoi(arg1) * atoi(arg2);
}
else
{
char* arg1, *arg2;
arg1 = strtok ( input2, "+");
arg2 = strtok ( NULL, "");
value = atoi (arg1) + atoi(arg2);
}
sprintf (output,"%d", value);
printf ("The output value is %s", output);
This code works only if I give expression having multiplication. For example it works only if I give 5*6. This is not working if I give 5+6.
The problem is in the else part. It is not able to tokenize the string input2.
Can't I tokenize two different strings in a single program.
Where am I wrong? Can someone explain me this concept of why strtok is not working for secong string?
the first strtok will not return NULL for "3+5", but rather a pointer to the token "3+5"
(so the else statement won't get executed).
now the problem is that the second call to strtok (around line #12 in your code) will return NULL, and the subsequent call atoi(NULL) will segfault.
The first strtok call will not return NULL (unless your input string is either empty or only contains '*' characters), so the else statement will not be executed for a string like "5+6".
You probably want to use strchr (or similar) to figure out what operation is to be performed, and then get the operands.
case of input "3+5" result of strtok(input1, "*") is "3+5", That else clause is not executed because it should not be NULL.
Second parameter for strtok is an array use like this strtok (input1, "+*"). That means that it will tokenize if they get '*' or '+'.
I code this like below:
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ss[100] = "123+321";
int a = atoi(ss);
int aLength = (int)floor(log10((double)a))+1;
int b = atoi(ss+aLength);
if(ss[aLength] == '+')
printf("%d + %d = %d\n", a, b, (a+b));
else
printf("%d * %d = %d\n", a, b, (a*b));
return 0;
}

Resources