Can someone please help me with this problem, I want my program to print + with every end of line.
My code is:
#include <stdio.h>
#include <string.h>
int main()
{
char str[65];
char * pch;
char str1[65];
fgets (str, 100, stdin);
pch = strtok (str," ");
while (pch != NULL)
{
str1=pch;
printf("=");
printf ("%s\n",pch);
pch = strtok (NULL, " ");
}
return 0;
}
With input, "Muhannad Stack" (output wrong):
=Muhannad
=Stack
Correct:
=Muhannad+
=Stack+
Therefore, my problem is how to add + at the end of every printed line
Here is what would work:
#include <stdio.h>
#include <string.h>
int main()
{
char str[65];
char * pch;
char str1[65];
fgets (str, 100, stdin);
pch = strtok (str,"\n");
pch = strtok (pch," ");
while (pch != NULL) {
printf ("=%s+\n",pch);
pch = strtok (NULL, " ");
}
return 0;
}
Related
I have this code:
char *pch;
pch = strtok(texto," ");
while (pch != NULL)
{
pch = strtok (NULL, " ");
}
My "texto" variable have something like "This is my text example".
And i need to store each value comming from pch in while, in an array of characteres, but i really dont know how to do it.
I need something like that, each value in array will have a word, so:
"This is my text example".
Array[0][20] = This;
Array[1][20] = is;
Array[2][20] = my;
Array[3][20] = text;
Array[4][20] = example;
The pch in while having all these words already split, but I don't know how to add into a char array, or how I will declare him too.
Consider the following example:
#include <stdio.h>
#include <string.h>
#define MAXSTRLEN 20
#define MAXWORD 6
int main(void)
{
char arr[MAXWORD][MAXSTRLEN+1] = {0};
char str[] ="This is my text example";
char *pch;
int i = 0;
pch = strtok (str," ");
while (pch != NULL && i < MAXWORD)
{
strncpy(arr[i++], pch, MAXSTRLEN);
pch = strtok (NULL, " ");
}
return 0;
}
This should work. Use strcpy to copy pch to character array.
char str[] ="This is my text example";
char *pch;
int i = 0;
pch = strtok (str," ");
char a[10][20];
while (pch != NULL)
{
strcpy(a[i++], pch);
pch = strtok (NULL, " ");
}
And as #stackptr has suggested dont use strtok and instead use strtok_r . For more info on this.
I'm sorry in advance because I'm fairly new to programming and some things in my code will probably look like utter nonsense! I'm not entirely sure if I'm using atoi right.
I'm trying to create a program that splits a user input sentence into single words and doubles the number(float/integer) if a user inputs one.
For example, I have 3 cats would come out as:
I
have
6
cats
My program right now is able to split the sentence, but I can't get the integer to double. Can anyone help me with this?
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char sentence[100];
printf("Enter a sentence to split: ");
scanf("%[^\n]s", sentence);
char *pch;
int y;
y = atoi(sentence);
printf("After splitting:\n", sentence);
pch = strtok(sentence," ");
while (pch != NULL) {
printf("%s\n", pch);
pch = strtok(NULL, " ");
}
system("PAUSE");
}
And my output so far:
Enter a sentence to split: Hi, I have 7 cats.
After splitting:
Hi,
I
have
7
cats.
Press any key to continue . . .
Here is a simpler version with a test for all digit numbers:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char sentence[100];
char *pch;
printf("Enter a sentence to split: ");
if (!fgets(sentence, sizeof sentence, stdin))
return 1;
printf("After splitting:\n");
for (pch = strtok(sentence, " \n"); pch != NULL; pch = strtok(NULL, " \n")) {
if (pch[strspn(pch, "0123456789")] == '\0') {
printf("%d\n", atoi(pch) * 2);
} else {
printf("%s\n", pch);
}
}
system("PAUSE");
return 0;
}
If you want to parse floating point numbers too, you could use this code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void) {
char sentence[100];
char *pch, *pend;
double value;
printf("Enter a sentence to split: ");
if (!fgets(sentence, sizeof sentence, stdin))
return 1;
printf("After splitting:\n");
for (pch = strtok(sentence, " \n"); pch != NULL; pch = strtok(NULL, " \n")) {
value = strtod(pch, &pend);
if (*pend == '\0' && isfinite(value)) {
printf("%g\n", value * 2);
} else {
printf("%s\n", pch);
}
}
system("PAUSE");
return 0;
}
Note the test for isfinite() to avoid recognizing inf and nan as numbers.
NOTE: isfinite is part of C99, it is not supported by VisualStudio 12, but more recent versions do support it. For this older version, use _finite() defined in <float.h>.
Here is a working example, even if it's not really precise. Basically, we need to find out whether our current string is a number or not. In this case, I just tried to get the first element of the current string, and tried to determine if it's numeric or not.
Of course, if you want to be really sure, we need to iterate the string and check every char, if it is a number or not.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char sentence[100];
printf("Enter a sentence to split: ");
scanf("%[^\n]s", sentence);
char * pch;
int y;
y = atoi(sentence);
printf("After splitting:\n", sentence);
pch = strtok (sentence," ");
while (pch != NULL)
{
if(isdigit(pch[0]))
{
int number = atoi(pch);
number *=2;
printf("%d\n",number);
pch = strtok (NULL, " ");
continue;
}
printf("%s\n",pch);
pch = strtok (NULL, " ");
}
system("PAUSE");
}
For the split part I would recommend this version, it is easier to understand (considering that you've learned the for way of working). It does the same thing, but helps you organize your code.
char *p;
int i;
for(p = strtok(sentence, " "); p != NULL; p = strtok(NULL, " "))
{
int isNumber = 1;
for(i = 0; i < strlen(p); i ++)
{
if(!isDigit(p[i])
{
isNumber = 0;
break;
}
}
if(isNumber == 1)
{
int number = atoi(pch);
number *= 2;
printf("%d\n", number);
}
else
{
printf("%s\n", p);
}
}
For the number, I would recommend using the atoi function. Here you have a good reference.
To solve your problem, first of all, you need to check every single word you get. For example: You take it word by word and see if the "word" contains ONLY digits. If it contains only digits, you can use atoi to convert it to a number, multiply it by 2 and print the result.
On the other hand, if you find a char that is NOT a digit, you have letters or any other characters in your word, so it is not a word so you simply print that as it is.
Here's another (more compact) answer:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int is_numeric(char *s)
{
int i = 0;
while (s[i] != '\0') {
if (!isdigit(s[i]))
return 0;
++i;
}
return 1;
}
int main()
{
char sentence[255];
char *pch;
printf("Enter a sentence to split: ");
if (!fgets(sentence, sizeof(sentence), stdin)) {
return 1;
}
sentence[strcspn(sentence, "\n\r")] = 0; /* Strip newline */
pch = strtok(sentence, " ");
while (pch != NULL) {
if (atoi(pch)) {
printf("%d\n", 2 * atoi(pch));
} else {
printf("%s\n", pch);
}
pch = strtok(NULL, " ");
}
return 0;
/* system("PAUSE"); */
}
The key is that atoi will return 0 for a non-numeric argument.
Hey I'm having problems with my code I get creating the tokens and have it adding it add the tokens to a 2d array but it doesn't work correctly. Any idea why.
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="This a sample string";
char * st[4][0];
char * pch;
int i;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ");
for(i=0;i<4;i++)
{
st[i][0]=pch;
}
}
print(st, i);
return 0;
}
void print(char st[4][0], int i)
{
for(i=0;i<4;i++)
{
printf("%d - %s",i ,st[i][0]);
}
}
char * st[4][0];
You are allocating an array of zero length. later you try to access the first element, which is non-existent, and therefore you get undefined behaviour.
I cannot see why this array has two dimensions anyway. You only access the first element of the second dimension, why not:
char * st[4];
??
To be more precise I don't understand the usage of this variable at all. Why do you write the same value in all four elements?
There are a number of problems: compare with this code:
/* strtok example */
#include <stdio.h>
#include <string.h>
void print(char *st[4]) // Fixed parameter type
{
int i; // i is a local counter
for(i=0;i<4;i++)
{
printf("%d - %s\n",i ,st[i]);
}
}
int main ()
{
char str[] ="This a sample string";
char * st[4]; // Corrected array definition
char * pch;
int i=0; // Initialise counter i to 0
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ");
while (pch != NULL)
{
st[i]=pch; // Store string before overwriting pch, and only store in a single location
printf ("%s\n",pch);
pch = strtok (NULL, " ");
i++; // increment i inside loop
}
print(st);
return 0;
}
#include <stdio.h>
#include <string.h>
int main ()
{
char buff[] ="AA:BB:CC:DD:EE:FF";
char * pch;
int buffInt[6];
pch = strtok (buff,":");
int i = 0;
while (pch != NULL)
{
buffInt[i] = atoi(*pch);
printf ("%c\n",buffInt[i]);
pch = strtok (NULL, ":");
i++;
}
return 0;
}
Im trying to convert the buffer and convert it into int using atoi but it does not seem to work does anyone know any other way to do this.
I have a problem with strtok() - it does not return the input as expected.
void parse_input(const char *input,unsigned char *ctext, int mlen){
char * str = strdup(input);
char * pch = strtok(str,"-");
while (pch != NULL)
{
ctext[mlen] = (int) pch;
pch = strtok (NULL, "-");
mlen++;
}
On input like 1-2-3-4 I would want it to fill ctext with [1,2,3,4].
That doesn't work, however.
What am I doing wrong? Any help appreciated.
ctext[mlen] = (int) pch;
That stores the numeric value of the pointer, whereas you really want the character pointed to by the pointer. Time to read a good article/book/tutorial on pointers.
ctext[mlen] = *pch;
is what you're looking for.
You want to get the character in the first byte of pch -- not the address of pch
ctext[mlen] = *pch;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void parse_input(const char *input,unsigned char *ctext[], int *mlen){
char * str = strdup(input);
char * pch = strtok(str,"-");
while (pch != NULL){
ctext[(*mlen)++] = (unsigned char*)pch;
pch = strtok (NULL, "-");
}
}
int main(void){
unsigned char *ctext[16];
int mlen=0;
int i;
parse_input("1-2-3-4", ctext, &mlen);
printf("[ ");
for(i=0;i<mlen;++i){
printf("%s", ctext[i]);
if(i<mlen -1)
printf(", ");
}
printf(" ]\n");
//free(ctext[0]);
return 0;
}