just having a little issue with strtok and strcmp.
I'm trying to compare the input of a user via fgets to some predetermined string:
char *token[100];
fgets(s, sizeof(s), stdin)
token[0] = strtok(s, " "); // Get first word
printf("tok: %s", token[0]);
printf("cmp: %d\n", strcmp(token[0], "/bin/echo");
Obviously it's not all the code but this shows my problem - if I enter "/bin/echo ..." (or anything for that matter), it will be put into token[0], and get printed. It prints correctly but when it prints the cmp value it's never 0. For /bin/echo, the cmp value is 1 for some reason.
Thanks.
EDIT to clear up confusion about s and token:
char s[1024];
char *token[100];
EDIT 2 - Added some other test cases:
I entered "/bin/echo hello world" to stdin
token[0] = strtok(s, " \n\0"); // Get first word
printf("token[0] is: %s", token[0]);
printf("cmp: %d\n", strcmp(token[0], "/bin/echo"));
Output:
token[0] is: /bin/echo
cmp: 1
And then I tried hardcoding the tokened string:
char str[] = "/bin/echo hello world";
token[0] = strtok(str, " ");
printf("token[0] is: %s", token[0]);
printf("cmp: %d\n", strcmp(token[0], "/bin/echo"));
Output:
token[0] is: /bin/echo
cmp: 0
here i have made small program
#include<string.h>
#include<stdio.h>
int main()
{
char str[] ="/bin/echo this is something";
char * token[100];
token[0] = strtok (str," ");
token[0] = strtok(str, " "); // Get first word
printf("cmp: %d\n", strcmp(token[0], "/bin/echo"));
return 0;
}
Here i have statically stored input string instead of fgets()
That works fine.
see http://codepad.org/IrGAXT8f
Use
char token[1000];
strcpy(token,strtok(s," "));
string's can't be assigned directly like this in c :)
also, include string.h
One needs to allocate memory dynamically for copying strings. Read about dynamic memory management first (malloc, calloc, etc...)
EDIT:
http://ideone.com/0UxEwO - works for me
#include <stdio.h>
#include <string.h>
int main()
{
char s[1024];
char *token[100];
fgets(s, sizeof(s), stdin);
token[0] = strtok(s, " \n\0");
printf("token[0] is: %s", token[0]);
printf("cmp: %d\n", strcmp(token[0], "/bin/echo"));
}
Related
I run the following c program
char str[80] = "String";
printf("hello %s\n", str);
scanf("%s", str);
printf("%s\n", str);
scanf("%[ABCDEF]", str);
printf("hello %s\n", str);
return 0;
For some reason on line 5 when it is suppose to input from Pattern %[ABCDEF], the console simply prints previous string (input from line 3). Why is that so?
Thats because the first scanf call doesn't read the newline character and the second call to scanf simply reads that newline character. To avoid this start the format string with a space like this:
#include <stdio.h>
int main(void)
{
char str[80] = "String";
printf("hello %s\n", str);
scanf("%s", str);
printf("%s\n", str);
scanf(" %[ABCDEF]", str);
printf("hello %s\n", str);
return 0;
}
However, you also need to make sure that str doesn't overflow if the user inputs a string longer than 79 characters.
I am able to use this to print my default string, but when I go to print a user input it does not work--instead I get some random characters and symbols back
int i=0;
char* tok=strtok(defaultString," ");
while(tok!=NULL){
i++;
if (i==defaultInd){
printf("%s \n", tok);
break;
}
tok=strtok(NULL, " ");
}
I believe that there is some issue with accessing memory or something store in a memory that I haven't actually initialized yet, but when I try to use
char* tok=(char *)malloc(strlen(strtok(sent, " ")+1));
it just stops printing things period.
My work around had been to print argv using a for loop but I still don't know what the actual problem is.
Here is what I have to print the user input
char* tok=strtok(argv," ");
while(tok!=NULL){
printf("%s\n", tok);
tok=strtok(NULL," ");
}
I get back a series of symbols (alphanumeric, greek, etc) instead of the string I input
In
char* tok=(char *)malloc(strlen(strtok(sent, " ")+1));
you just allocate a piece of memory whose size depends on the result of strtok, but the result of strtok is not copied into the returned block of memory
char* tok=strtok(argv," ");
while(tok!=NULL){
printf("%s\n", tok);
tok=strtok(NULL," ");
}
If argv is the second argument of main that one is not a char* but a char**, so your call is wrong
What did you expect to do ? if it is to print the arguments or the program you have to do something like :
while (*++argv != 0)
puts(*argv);
If you want to get each word from a group of words given in the first argument of the program :
#include <stdio.h>
#include <string.h>
int main(int argc, char ** argv)
{
if (argc == 2) {
char* tok=strtok(argv[1]," ");
while(tok!=NULL){
printf("%s\n", tok);
tok=strtok(NULL," ");
}
}
return 0;
}
Execution :
pi#raspberrypi:/tmp $ ./a.out "aze qsd wxc"
aze
qsd
wxc
As you see I have to use a string when I call ./a.out else there are 3 argument and the first argument is just aze
In
int i=0;
char* tok=strtok(defaultString," ");
while(tok!=NULL){
i++;
if (i==defaultInd){
printf("%s \n", tok);
break;
}
tok=strtok(NULL, " ");
}
if defaultString is for instance "11 22 33" and defaultInd values 2 you will print 22
but you do not said what are the value of these variables
char* tok = strtok(defaultString, " ");
while (tok != NULL){
myarg[count]=strdup(tok);
count ++;
printf("%s\n", tok);
tok = strtok(NULL, " ");
}
I want to know why strcasecmp() is returning 0 the first time I use it but not the second.
In this example i'm specifically entering "hello world" into standard input.
Instead of printing 0 0 it's printing 0 10. I have the following code.
#include "stdio.h"
#include "string.h"
int main(void) {
char input[1000];
char *a;
fgets(input, 1000, stdin);
a = strtok(input, " ");
printf("%d\n",strcasecmp(a,"hello")); //returns 0
a = strtok(NULL, " ");
printf("%d\n",strcasecmp(a,"world")); //returns 10
return 0;
}
What am I doing wrong?
The newline, you have entered after hello world is part of the world token because you use space as token separator.
If you use strtok(input, " \n"); instead of strtok(input, " "); the program will behave correctly. In fact, you probably want to use tabulator as token separator as well.
The whole program will be:
#include "stdio.h"
#include "string.h"
int main(void) {
char input[1000];
char *a;
fgets(input, 1000, stdin);
a = strtok(input, " \n\t");
if (a == NULL) return(-1);
printf("%d\n",strcasecmp(a,"hello"));
a = strtok(NULL, " \n\t");
if (a == NULL) return(-1);
printf("%d\n",strcasecmp(a,"world"));
return 0;
}
I am tasked with writing a C program that will take a string with hyphens in it, and check to see that the first group of the string (before the hyphen) is alphabet/letter only, the next group is numeric only, and the last group is alphabet/letter only. It is similar to this project: http://wps.aw.com/wps/media/objects/7257/7431666/Case_Studies/GaddisJavaCSO_CS6.pdf
So far I am stuck on splitting the string into 3 variables. I have read about strtok and manipulating the scanf function, but I haven't been successful:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char serial [50];
char * part1 = NULL, part2 = NULL, part3 = NULL;
printf("Enter Serial Number:\n");
scanf("%s", serial);
part1 = strtok (serial, "-");
part2 = strtok(NULL, "-");
part3 = strtok(NULL, "-");
printf("You entered %s\n", part1);
printf("You entered %s\n", part2);
printf("You entered %s\n", part3);
return 0;
}
you are using strtok wrong, pass parameters to it and it should work fine.
char * pch = strtok (serial, "-" );
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, "-");
}
or in your example you need to define each as a char* :
char * part1= strtok (serial, "-");
char * part2 = strtok(NULL, "-");
char* part3 = strtok(NULL, "-")
StrTok + example
strcpy(part1, strtok(serial, "-"));//premise: string has hyphen
strcpy(part2, strtok(NULL, "-"));
strcpy(part3, strtok(NULL, "-"));
You could utilize scanf's formatting rules to read your strings directly from the input line.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char part1[40], part2[40], part3[40];
int count, n;
do{
n = 0;
flushall();
printf("Enter Serial Number:\n");
count = scanf(" %39[A-Za-z]-%39[0-9]-%39[A-Za-z]%n", part1, part2, part3, &n);
if( count != 3 || n == 0 ){
printf("Recognize %i parts, %s\n", count, n == 0 ? "did not parse to the end" : "parsed to the end");
}
}while(count != 3 || n == 0);
printf("You entered %s\n", part1);
printf("You entered %s\n", part2);
printf("You entered %s\n", part3);
return 0;
}
This is quite a strict form of parsing the input and requires the user to keep the outer form. You can easily filter allowed strings by not using %s but rather something like %[0-9]. The best way for me to filter serialnumber inputs was always Regex if available... but i dont think this is part of your homework yet :)
I have the following code:
char *s1, *s2;
char str[10];
printf("Type a string: ");
scanf("%s", str);
s1 = &str[0];
s2 = &str[2];
printf("%s\n", s1);
printf("%s\n", s2);
When I run the code, and enter the input "A 1" as follow:
Type a string: A 1
I got the following result:
A
�<�
I'm trying to read the first character as a string and the third character as an integer, and then print those out on the screen. The first character always works, but the screen would just display random stuffs after that.... How should I fix it?
You're on the right track. Here's a corrected version:
char str[10];
int n;
printf("type a string: ");
scanf("%s %d", str, &n);
printf("%s\n", str);
printf("%d\n", n);
Let's talk through the changes:
allocate an int (n) to store your number in
tell scanf to read in first a string and then a number (%d means number, as you already knew from your printf
That's pretty much all there is to it. Your code is a little bit dangerous, still, because any user input that's longer than 9 characters will overflow str and start trampling your stack.
scanf("%s",str) scans only until it finds a whitespace character. With the input "A 1", it will scan only the first character, hence s2 points at the garbage that happened to be in str, since that array wasn't initialised.
Try this code my friend...
#include<stdio.h>
int main(){
char *s1, *s2;
char str[10];
printf("type a string: ");
scanf("%s", str);
s1 = &str[0];
s2 = &str[2];
printf("%c\n", *s1); //use %c instead of %s and *s1 which is the content of position 1
printf("%c\n", *s2); //use %c instead of %s and *s3 which is the content of position 1
return 0;
}