Putting information from a char array into a Dynamically created array - c

I'm trying to pass information from a char string that's been
tokenized using " ." as the set.
Turn those characters into integers using atoi()
Then send the values into dynamically allocated memory
I know the theory, I know how it's supposed to work, but I can't get the right syntax to make it work!
The second part, after I declare *Malloc_Array_ptr* is where I run into trouble.
So far, I've used the Malloc Pointer exactly how I'd use a regular array pointer, and I'm not getting any results on my printf test.
Can't find info that makes sense to me over google, I'm going crazy over this. I think I'm real close to figuring it out, I just need a nudge in the right direction >.<
Thank you! :)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
#define MIN_SIZE 2
void StrInput(char str[], int maxChars);
int main(int argc, char *argv[])
{
char Array[SIZE], *Array_ptr = strtok(Array, " .");
StrInput(Array, SIZE);
int i=1, *temp = Array_ptr;
//Strok initialized in order to use NULL next sequence.
//Temp stores the pointer in it's original form, before it gets butchered by strtok
while (Array_ptr != NULL)
{
Array_ptr = strtok(NULL, " .");
i++;
}
//Above code finds the number of tokens strtok worked on, and stores it as i.
//Dynamically Creates the array which can hold exactly the amount of tokens (i)
int *Malloc_Array_ptr = (int*)malloc(i* sizeof(int)), hold;
i=0;
while (Array_ptr != NULL)
{
temp = strtok(NULL, " .");
hold = atoi(temp);
Malloc_Array_ptr[i] = hold;
i++;
}
printf("Show me the money: %s \n", Malloc_Array_ptr);
system("PAUSE");
return 0;
}
/*----------------------------------------------------------------------*/
void StrInput(char str[], int maxChars)
{
int i=0, str_lenght;
while ((str[i] = getchar()) != '\n')
i++;
str[i+1]='\0';
if (i>maxChars || i<MIN_SIZE)
{
printf("Your sumbition dosn't fit the size criteria.\n");
printf("Please reenter:\n\n");
StrInput(str, maxChars);
}
}

This is problematic:
char Array[SIZE], *Array_ptr = strtok(Array, " .");
You are declaring the array, then trying to use strtok on the uninitialized array. You probably meant to do this:
char Array[SIZE], *Array_ptr = 0;
StrInput(Array, SIZE);
Array_ptr = strtok(Array, " .");

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
#define MIN_SIZE 2
void StrInput(char str[], int maxChars);
int main(int argc, char *argv[]){
char Array[SIZE], *Array_ptr, *strwk;
StrInput(Array, SIZE);
int i=0;
strwk=strdup(Array);//strtok would change the string. so make a copy.
Array_ptr=strtok(strwk, " .");
while (Array_ptr != NULL){
++i;//countup element
Array_ptr = strtok(NULL, " .");
}
int *Malloc_Array_ptr = (int*)malloc(i* sizeof(int));
i=0;
strcpy(strwk, Array);
Array_ptr = strtok(strwk, " .");
while (Array_ptr != NULL){
Malloc_Array_ptr[i] = atoi(Array_ptr);
++i;
Array_ptr = strtok(NULL, " .");
}
free(strwk);
int j;
//check print
for(j=0;j<i;++j)
printf("%d ", Malloc_Array_ptr[j]);
printf("\n");
// printf("Show me the money: %s \n", Malloc_Array_ptr);//Malloc_Array_ptr isn't (char*).
system("PAUSE");
return 0;
}
/*----------------------------------------------------------------------*/
void StrInput(char str[], int maxChars){
int i=0, ch;//str_lenght: unused
int InputOver = 0;
printf("input numbers :");
for(i=0;(ch = getchar()) != '\n';++i){
if(i > maxChars -1){//-1: for EOS(\0)
i = maxChars - 1;
InputOver = !InputOver;//true
break;
}
str[i]=(char)ch;
}
str[i]='\0';
if (InputOver || i<MIN_SIZE){
printf("Your sumbition dosn't fit the size criteria.\n");
printf("Please reenter:\n\n");
while('\n'!= ch){//clear input
ch = getchar();
}
StrInput(str, maxChars);
}
}

Related

Pig Latin Implementation in C

I'm trying to implement question 8.13 from C How to program, which is simply shifting left from the second char of the string and concat the first char of the string with "ay". For example:
jump -> umpjay the -> hetay and so on.
My try is here:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *appender(char s);
void shiftLeft(char [], int);
int main()
{
char s[100], *lastThree;
fgets(s, 100, stdin);
s[strcspn(s, "\n")] = 0;
char *tokenPtr = strtok(s, " ");
while(tokenPtr != NULL){
lastThree = appender(tokenPtr[0]);
//printf("lastThree : %s\n", lastThree); //for debugging
shiftLeft(tokenPtr, strlen(tokenPtr));
sprintf(tokenPtr, "%s%s ", tokenPtr, lastThree); // concatenation
printf("tokenptr:%s ", tokenPtr);
tokenPtr = strtok(NULL, " ");
}
return 0;
}
char *appender(char s){
char *returned = (char*)malloc(sizeof(char)*4); // allocation
snprintf(returned, sizeof(returned), "%c%s",s,"ay"); // append ay
return returned;
}
void shiftLeft(char s[], int len){
int i;
for(i=0;i<len-1;i++){
s[i]=s[i+1];
}
s[i] = '\0';
}
But code works wrong. It evaluates tokenPtr as yaay even if the input is only a word.

Sorting string lexicographically in c

I want to sort words of a string in lexicographical order.
For Example:
I have a string: I am Apple
Output should be: am Apple I
Problem (output):
enter the string
hello shamsh
the sorted array:
hello
It's not sorting the string and whole string is not being shown in the output, can anyone help me out here. Thanks!
Program code:
#include<stdio.h>
#include<string.h>
void main()
{
char a[25][25],t[25];
char s[200];
char * pch;
int count = 0;
int i,j ,n;
printf("enter the string\n");
gets(s);
pch = strtok (s," ,.-");
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ')
count++;
}
count=count+1;
i=0;
while(pch != NULL)
{
strcpy(a[i],pch);
pch = strtok (NULL, " ,.-");
i++;
}
for(i=0;i<count-1;i++)
{
for(j=i+1;j<count;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(t,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],t);
}
}
}
printf("the sorted array:\n");
for(i=0;i<count;i++)
printf("%s\n",a[i]);
}
If you try to print your string after you pch = strtok (s," ,.-"), you'll notice that your string is broken up. That's because strtok() is destructive and breaks up the string into tokens so you need to count the number of white spaces before calling strtok():
printf("enter the string\n");
gets(s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ')
count++;
}
count=count+1;
i=0;
pch = strtok (s," ,.-");
Also like Weather Vane said, don't use gets(), use fgets() instead oand remove the '\n' from end of the string afterwards. Also you can use realloc() to assign more memory to a dynamic array instead of using a static array since you wouldn't know the number of words in a string beforehand.
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
void main()
{
char** a = NULL;
char t[25];
char s[512];
char * pch;
int count = 0;
int i,j ,n;
printf("enter the string\n");
if(fgets(s,512, stdin)==NULL)
{
printf("failed to read string\n");
exit(-1);
}
/*remove '\n' from end of the string*/
char *pos;
if ((pos=strchr(s, '\n')) != NULL)
*pos = '\0';
pch = strtok(s, " ,.-");
while(pch)
{
a = realloc(a, sizeof(char*)*++count);
if(a==NULL)
{
perror("failed to allocate memory\n");
exit(-1);
}
a[count-1] = pch;
pch = strtok(NULL, " ,.-");
}
for(i=0;i<count;i++)
printf("%d: %s\n", i, a[i]);
///...compare array
Use qsort() for this sort of thing.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 0x100
int strcmp_wrapper(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main () {
char buffer[BUF_SIZE], *tokens[BUF_SIZE / 2 + 1];
int i = 0, j = 0;
printf("Enter a string: ");
fgets(buffer, BUF_SIZE, stdin);
tokens[0] = strtok(buffer, " ,.-\n");
while ((tokens[++i] = strtok(NULL, " ,.-\n")));
qsort(tokens, i, sizeof(tokens[0]), strcmp_wrapper);
while (j < i)
printf("%s\n", tokens[j++]);
return 0;
}
below is a compact working way of doing what you want. It prints the words of each line, sorted and separated by one space, without repeating words being repeated (if you want them repeated for sure you will be able to touch the program to make it work)
$ cat pru799.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DELIMITERS " \t\n,.-()&%$\"\'[]{}+-*/;:##|!\\<>=?"
#define LINE_SIZE 1024
#define MAX_WORDS 256
int compare(const char **p, const char **q)
{
return strcmp(*p, *q);
}
int main()
{
char line[LINE_SIZE];
char *words[MAX_WORDS];
int n_words;
while (fgets(line, sizeof line, stdin)) { /* while not eof */
char *p;
int i;
/* first get the words */
n_words = 0;
for (p = strtok(line, DELIMITERS); p; p = strtok(NULL, DELIMITERS)) {
if (strlen(p) == 0) continue; /* word is zero length */
if (n_words >= MAX_WORDS) {
fprintf(stderr, "MAX_WORDS(%d) exceeded\n", MAX_WORDS);
exit(EXIT_FAILURE);
}
words[n_words++] = p;
} /* for */
/* now we have all the words in the array of strings words, sort it */
qsort(words, n_words, sizeof words[0], (int(*)(const void *, const void *))&compare);
/* now print the words */
for (i = 0; i < n_words; i++) {
if (i) { /* all but the first one */
/* don't repeat words */
if (!strcmp(words[i], words[i-1]))
continue;
printf(" "); /* print a space between words */
}
printf("%s", words[i]);
}
printf("\n");
} /* while */
} /* main */

conflicting types for function returning a char array

Here's my code:
#include <stdio.h>
#include <string.h>
char input_buffer[1000];
void get_substring(){
int i;
int length;
printf("Please enter a string:\n");
scanf("%[^\n]s", input_buffer);
printf("Index of first character of substring:\n");
scanf("%d", &i);
printf("Length of substring:\n");
scanf("%d", &length);
printf("Substring is %.*s ", length, input_buffer + i);
}
int main(void) {
// your code goes here
//get_substring(0,4);
get_substring();
return 0;
}
That's my current code, I want to return a pointer of the input, instead of just displaying the substring. Sorry for the confusion everyone.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getSubstring(char* str,size_t start, size_t length)
{
// determine that we are not out of bounds
if(start + length > strlen(str))
return NULL;
// reserve enough space for the substring
char *subString = malloc(sizeof(char) * length);
// copy data from source string to the destination by incremting the
// position as much as start is giving us
strncpy(subString, str + start, length);
// return the string
return subString;
}
int main(int argc, char* argv[])
{
char *str = "Hallo Welt!";
char *subStr = getSubstring(str,0,20);
if(subStr != NULL)
{
printf("%s\n",subStr);
free(subStr);
}
}
This solution should give you a hint how you would start with such a problem.

Seg fault using strtok to parse stdin to an array

I'm getting a seg fault every time the code reaches the first strtok
token = strtok(commandLine," ");
I'm just trying to parse stdin and store it, using a space as a delimiter. A lot of problems I saw were people using strtok on a string literal, which I assume also applies to my case as well, but how do I work around that?
Thanks.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
int main(int argc, char* argv[]){
//Used for parsing
char commandLine[255];
char* tokens[10];
char* token;
int counter;
int i;
printf("gets to pt 1\n");
//Parsing
while( fgets(commandLine, 255, stdin) ){
printf("\n%s\n", commandLine);
token = strtok(commandLine," ");
printf("gets here");
counter = 0;
for(counter = 0; token != NULL; counter++){
strcpy(tokens[counter], token);
token = strtok(NULL, " ");
}
}
printf("gets to point2");
for(i = 0; tokens[i] != NULL; i++){
printf("%s ", tokens[i]);
}
EDIT:
Here is the working code.
As User93353 pointed out, I had to allocate memory for my tokens so I changed
char* tokens[10]
to
char tokens[10][100]
and my for loop wasn't ending correctly, had to change
tokens[i] != NULL
to
i<counter
-
int main(int argc, char* argv[]){
//Used for parsing
char commandLine[255];
char tokens[10][100];
char* token;
int counter;
int i;
printf("gets to pt 1\n");
//Parsing
while( fgets(commandLine, 255, stdin) ){
printf("\n%s\n", commandLine);
token = strtok(commandLine," ");
printf("gets here");
for(counter = 0; token != NULL; counter++){
strcpy(tokens[counter], token);
token = strtok(NULL, " ");
}
printf("gets to printing");
for(i = 0; i<counter; i++){
printf("%s", tokens[i]);
}
}
}
Allocate memory for each element of the tokens array.
Easy way is to declare it as
#define SOME_SIZE 100
char tokens[10][SOME_SIZE];
Otherwise, tokens[0], tokens[1] etc point to some random location in memory. strcpying to that random location is causing your program to crash.

Tokenized string of char to ints using atoi

I am trying to take user input: (1 345 44 23) and make it into a tokenized char string then into ints. Surprisingly I could not find much help for what I would think would be a common task.
Any ideas how to convert the char string into an in string using tokens?
My program crashes when it gets to the conversion (after the tokenization [I realize this is not a word]).
Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define StrSZE 81
void strInput (char str[], int maxChars);
void custatoi(char * tokenArray[], int * data, int numOfTok);
int main(int argc, char *argv[])
{
char str[StrSZE];
char* tokenArray;
int maxChars=StrSZE-1, cont=1, numOfToken=0, i=0;
int* data;
strInput(str, maxChars);
tokenArray = strtok(str, " \t");
while (tokenArray)
{
printf("token: %s\n", tokenArray);
tokenArray = strtok(NULL, " \t");
numOfToken++;
}
data = (int *) malloc(numOfToken * sizeof(int));
custatoi(tokenArray, data, numOfToken);
system("PAUSE");
return 0;
}
void strInput (char str[], int maxChars)
{
char garbage;
int k=0;
str[0]='\0';
printf("Please type a string of whole numbers (intigers).\n\n");
while ((k<80) && ((str[k] = getchar()) != '\n'))
k++;
/* Clears the keyboard buffer. */
if (k==80)
while((garbage = getchar()) != '\n')
;
/* Place null at the end of the line read in from user */
str[k]='\0';
printf("str after input is: %s\n\n", str);
}
void custatoi(char * tokenArray[], int * data, int numOfTok)
{
int i;
for (i=0; i < numOfTok; i++)
data[i] = atoi(tokenArray[i]);
}
I corrected the errors in yours code: There was some mistakes in main(), tokenArray data type was not correct.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define StrSZE 81
void strInput (char str[], int maxChars);
void custatoi(char* tokenArray[], int * data, int numOfTok);
int main(int argc, char *argv[])
{
char str[StrSZE];
int maxChars=StrSZE-1, cont=1, numOfToken=0, i=0;
int* data;
char* tokenArray[50]; // Declared correctly
strInput(str, maxChars);
tokenArray[i] = strtok(str, " \t"); // Also made a change here!
while (tokenArray[i])
{
printf("token: %s\n", tokenArray[i]);
i++;
tokenArray[i] = strtok(NULL, " \t");
numOfToken++;
}
data = (int *) malloc(numOfToken * sizeof(int));
custatoi(tokenArray, data, numOfToken);
printf("data\n");
for(i=0;i<numOfToken;i++){
printf(" %d\n",data[i]);
}
system("PAUSE");
return 0;
}
void strInput (char str[], int maxChars)
{
char garbage;
int k=0;
str[0]='\0';
printf("Please type a string of whole numbers (intigers).\n\n");
while ((k<80) && ((str[k] = getchar()) != '\n'))
k++;
/* Clears the keyboard buffer. */
if (k==80)
while((garbage = getchar()) != '\n')
;
/* Place null at the end of the line read in from user */
str[k]='\0';
printf("str after input is: %s\n\n", str);
}
void custatoi(char* tokenArray[], int * data, int numOfTok)
{
int i;
for (i=0; i < numOfTok; i++)
data[i] = atoi(tokenArray[i]);
}
At the end of the strtok loop, tokenArray will be set to NULL. You then pass it to custatoi, which presumably crashes when it tries to dereference it.
Note that tokenArray is not an array of strings; it's just a single string pointer (or a pointer to an array of characters). If you want to accumulate the tokens into an array, you'll have to create a separate array for that purpose.
The main problem is that custatoi() expects to work with an array of pointers to char, while tokenArray in main() is a mere pointer to char. The original code never collects all pointers to tokens in the input string into an array that custatoi() expects, there isn't such an array in the original code.
Please study the fixed code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define StrSZE 81
void custatoi(char* tokenArray[], int* data, int numOfTok);
int main(void)
{
char str[StrSZE];
char** tokenArray;
int numOfToken = 0, i;
int* data;
//strInput(str, maxChars);
strcpy(str, "1 345 44 23");
tokenArray = malloc(sizeof(char*));
tokenArray[numOfToken] = strtok(str, " \t");
while (tokenArray[numOfToken] != NULL)
{
printf("token: %s\n", tokenArray[numOfToken]);
numOfToken++;
tokenArray = realloc(tokenArray, sizeof(char*) * (numOfToken + 1));
tokenArray[numOfToken] = strtok(NULL, " \t");
}
data = malloc(numOfToken * sizeof(int));
custatoi(tokenArray, data, numOfToken);
for (i = 0; i < numOfToken; i++)
printf("data[%d]=%d\n", i, data[i]);
return 0;
}
void custatoi(char* tokenArray[], int* data, int numOfTok)
{
int i;
for (i=0; i < numOfTok; i++)
data[i] = atoi(tokenArray[i]);
}
Output (idone):
token: 1
token: 345
token: 44
token: 23
data[0]=1
data[1]=345
data[2]=44
data[3]=23

Resources