Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Basically I want to get user's input and tokenize it. For example I type
4 <tab> 5 <tab> 6
I want to get just
4
5
6
But my piece of code does not work ;(
#include <stdio.h>
#include <string.h>
int main ()
{
char str;
scanf("%c",&str);
char *p = strtok(str, "\t");
while(p != NULL) {
printf("%s\n", p);
p = strtok(NULL, "\t");
}
}
You are getting mixed up between char and char*.
Try this instead:
#include <stdio.h>
int main ()
{
char str[1000];
while(scanf("%s", str)) {
printf("%s\n", str);
}
}
1000 is the maximum length of a single token. Adjust as necessary.
Related
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 3 years ago.
Improve this question
I'm a new c programmer.
I was asked to get an ID number as an input and if the ID length < 9 I need to put '0' before the number.
example:
input: 12345
output: 000012345
I can use the libraries: stdio, stdlib, string, math and time.
You can use printf for this:
#include <stdio.h>
int main () {
printf("%09d\n", 12345);
return(0);
}
Documentation
There is an easy solution in here.
But if you want a hard solution, where you juggle around with strings and such, you can do the following:
convert your id into a string (if it isn't already one, but argv should a string by default)
calculate the number of zeros you need, by doing 9-idString Length
create a "buffer" string variable
for loop from 0 to 9-idString and use strcat to add "0"s to your buffer string.
add your id to your buffer string using strcat
Example code would be something like this:
int id = 12345;
char idstring[10];
char buffer[10];
sprintf(idstring, "%d", id);
int missingZerosCount = 9 - strlen(idstring);
strcpy(buffer, "");
while (missingZerosCount > 0) {
strcat(buffer, "0");
missingZerosCount--;
}
strcat(buffer, idstring);
argv variant:
char idstring[10];
char buffer[10];
sprintf(idstring, "%s", argv[1]);
int missingZerosCount = 9 - strlen(idstring);
strcpy(buffer, "");
while (missingZerosCount > 0) {
strcat(buffer, "0");
missingZerosCount--;
}
strcat(buffer, idstring);
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Was talking to a colleague today on one-spot errors - I.e. errors (or at least patterns that should ring an alarm bell) in code that a decent programmer should be able to spot at a single glance like
x = malloc (strlen(y));
while (!feof (f)) {
...
}
char *f(){
char x[100];
...
return x;
}
Who has similar snippets of such patterns? I would suggest anyone who has been on SO for a while will have his personal favourites of those
char *buf;
scanf("%s", buf);
This is wrong, because no memory has been allocated for buf.
char buf[100];
scanf("%s", &buf);
This is wrong, because scanf expects a char *, not a char (*)[n].
char c;
while ((c = getchar()) != EOF)
putchar(c);
This is wrong, because EOF does not fit in the range of a char. Use int instead.
fflush(stdin);
fflush is undefined for input streams, like stdin, albeit this is implemented as an extension in some compilers, like Microsoft C.
#define IN 0;
Do not put semicolons at the end of a #define.
blk = realloc(blk, n);
If realloc fails, any contents in blk will be lost, because realloc will return NULL. To solve the problem, copy the return value into a temporary and only if the temporary is not NULL, copy to the final destination.
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 7 years ago.
Improve this question
How to find a number from each line of a text file?
For exemple in file is written:
Apple 500 America
Motorola 400 China
How i can find in a text file the int number (price)and to established if it is bigger than 450?
Given the format of the text file remains the same for all lines, you may use a combination of strtok and atoi to extract the number in between. Example:
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "Apple 500 America";
char *pch;
pch = strtok (str," \t\n"); // ignore 1st string
pch = strtok (NULL, " \t\n"); // get 2nd string
int i = atoi( pch ); // parse 2nd string to int
printf( "i = %d\n", i );
return 0;
}
Output:
i = 500
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
#include "stdafx.h"
#include<string.h>
#include<stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
char abc[28] ;
//abc[26] = '\0';
abc[0]=65;
char hj = abc[0];
for(int i=0; i<26; i++)
{
abc[i]=++hj;
printf("%d\n",i);
}
int l=strlen(abc);
abc[l]='\0';
printf("length of abc_array is %d\n",(strlen(abc)));
for(int i=0; i<strlen(abc); i++)
{
printf("%c",abc[i]);
printf("\n");
}
}
The output length of the string is 39, which is wrong. What is the error?
you need to set abc[i] = '\0' before you call strlen.
Standard strlen function is only applicable to strings. String is a sequence of characters terminated by zero character. Passing something that is not a string to strlen results in undefined behavior.
What you are passing to strlen in your code is not a string. This is why you get meaningless results from strlen.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
And I want it to print to output as is. Curently I am using this code
#include <stdio.h>
#include <ctype.h>
#define BUFFER_SIZE 2000
int main(void)
{
char buffer[BUFFER_SIZE];
while(fgets (buffer, BUFFER_SIZE, stdin) !=NULL)
{
printf("%s",buffer);
}
return 0;
}
Then I want the program to be able to skip html tags in the original text but I don't know exactly how to work around that.
You should use getline(3) (at least on Posix compliant systems). Your fgets based code won't work with very long lines (because a very long line would be "truncated": all of it would be read, but only BUFFER_SIZE characters would have been copied, and the rest of the line ignored).
You could code
char* linebuf=NULL;
size_t linesize=0;
while (!feof(stdin)) {
ssize_t linelen = getline(&linebuf, &linesize, stdin);
if (linelen<0) { perror("getline"); exit(EXIT_FAILURE); };
fputs(linebuf, stdout);
}
In the above code, the linebuf will (unless failure) be grown to the widest line size. You should free(linebuf) after that loop...