reading variable length string arrays as ints - c

So I'm trying to read input from file that is formatted like this:
0 3 4 1
1 2 4
2
3
4 2
The piece of code that I've written here:
for (int i = 0; i < 5; i++){
if (fgets(line, MAX_LEN, in) != NULL){
printf("\n%s\n", line);
int length = strlen(line);
printf("line = %d\n", length);
for (int j = 2; j < length; j+=2){
char a;
a = line[j];
int u = a - '0';
printf("line[%d] = %d\n", j, u);
}
}
}
is very crude but handles the above single digit type input, however I need it to handle double digit values. I thought about using sscanf() but the line inputs can contain an arbitrary number of values so I don't know how many times to call it. Any assistance here would be great as I'm at a loss... Thank you!

I suggest you use strtol
Here is an example of how to use it :
#include <stdio.h>
#include <string.h>
int main() {
char str[20]="1 8 0 8 99"; // or your line
char *pt;
int numbers[10]; // array to read the numbers
int index = 0, i;
numbers[index] = strtol(str, &pt, 10); // decimal numbers
index++;
while(*pt !='\0' )
{
numbers[index] = strtol(pt, &pt, 10);
index++;
}
for(i=0; i<index; i++) printf("%d ", numbers[i]);
return 0;
}
The output in this example will be :
1 8 0 8 99
Now I let you the task of integrating strtol in your actual program good luck :)

for (int i = 0; i < 5; i++){
if (fgets(line, MAX_LEN, in) != NULL){
printf("\n%s\n", line);
int length = strlen(line);
printf("line = %d\n", length);
int len, u;
sscanf(line, "%*d%n", &len);
for (int j=len;sscanf(line + j, "%d%n", &u, &len)==1;j+=len){
printf("line[%d] = %d\n", j+1, u);//+1 : one space
}
}
}

Related

Is my usage of fgets() and strtok() incorrect for parsing a multi-line input?

I'm writing an implementation of the Moore Voting algorithm for finding the majority element (i.e. the element which occurs more than size/2 times) in an array. The code should return the majority element if it exists or else it should return -1. Now my version of the majorityElement(int size, int arr[]) seems to work perfectly fine if I directly hardcode the integer array in the main() function and invoke it from there.
int majorityElement(int size, int arr[])
{
int majorityindex = 0;
int votes = 1;
int index;
for (index = 1; index < size; index++)
{
if (arr[index] == arr[majorityindex])
votes++;
else
votes--;
if (votes == 0)
{
majorityindex = index;
votes = 1;
}
}
int count = 0;
int i;
for (i = 0; i < size; i++)
{
if(arr[majorityindex] == arr[i])
count++;
}
if (count > (size/2))
return arr[majorityindex];
return -1;
}
However, I'm facing some issues if I try to read an input stream like these:
2
5
3 1 3 3 2
3
1 2 3
The first line of the input contains the number of test cases. The first line of the test case will be the size of the array and the second line will be the elements of the array.
I tried to read the input stream from within the main() function like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int majorityElement(int size, int arr[]);
int main()
{
char buf[3];
fgets(buf, MAX, stdin);
int n = atoi(buf);
char a[3];
char b[MAX];
int i;
int count;
int* num;
for (i = 0; i < n; i++)
{
count = 0;
fgets(a, MAX, stdin);
fgets(b, MAX, stdin);
int x = atoi(a);
char* num[x];
int arr[x];
int k = 0;
char* token = strtok(b, " ");
while (token != NULL)
{
num[k] = token;
arr[k] = atoi(num[k]);
token = strtok(NULL, " ");
k++;
}
printf("%d\n", majorityElement(x, arr));
}
return 1;
}
I took the size of buf[] and a[] as 3 during declaration as they must have sufficient space for the \n character read by fgets() as well as the terminating \0 character. As far as I know, the atoi() function ignores the \n character while converting the character array (string) into an integer. I tried to store the first entry of the input (i.e. the number of entries) in a character array buf, converted it into a string and stored it in a variable n. Similarly, I tried to obtain the size of a test array in a variable x and the test arrays (second line of test case) in an integer array arr. While buf and n seem to obtain the correct values in all cases, I'm not quite sure about arr. I'm aware that fgets() leaves a terminal \n character and that might be causing some havoc during tokenization using strtok, although I can't finger at why. I tried submitting this code on GeeksForGeeks. It gives absolutely correct outputs for the sample test case:
2
5
3 1 3 3 2
3
1 2 3
that is
3
-1
However, when I try to "submit" my solution it says:
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input:
4
1 2 2 1
Its Correct output is:
-1
And Your Code's output is:
1
I can't seem to make sense of this. If I manually write this in stdin:
1
4
1 2 2 1
the code outputs
-1
which is indeed the correct solution. This doesn't match with the output claimed during the submission i.e. 1. So I'm not really sure where I'm going wrong. Have I used fgets() or strtok() incorrectly in the main() function? Or is it something else?
Updated the main() function according to suggestions in the comments.
int main()
{
char buf[MAX];
fgets(buf, MAX, stdin);
int n = atoi(buf);
char a[MAX];
char b[MAX];
int i;
int count;
int* num;
for (i = 0; i < n; i++)
{
count = 0;
fgets(a, MAX, stdin);
fgets(b, sizeof(a), stdin);
a[sizeof(a)-1] = '\0';
b[sizeof(b)-1] = '\0';
int x = atoi(a);
int arr[x];
int k = 0;
char* token = strtok(b, " ");
while (token != NULL)
{
if (k > x)
break;
arr[k] = atoi(token);
token = strtok(NULL, " ");
k++;
}
printf("%d\n", majorityElement(x, arr));
}
return 1;
}
As pointed out by #Vlad, the MAX was set too low in my original array. The question says that the number of entries in an array is upper bounded by 10^7 and each array entry is upper bounded by 10^6 (7 digits). So MAX needs to be of the order 10^8. According to the suggestions in the comments, I'm now using dynamic allocation instead of variable length arrays.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10000000
int majorityElement(int size, int arr[])
{
int majorityindex = 0;
int votes = 1;
int index;
for (index = 1; index < size; index++)
{
if (arr[index] == arr[majorityindex])
votes++;
else
votes--;
if (votes == 0)
{
majorityindex = index;
votes = 1;
}
}
int count = 0;
int i;
for (i = 0; i < size; i++)
{
if(arr[majorityindex] == arr[i])
count++;
}
if (count > (size/2))
return arr[majorityindex];
return -1;
}
int main()
{
char* buf = calloc (MAX, sizeof(char));
fgets(buf, MAX, stdin);
int n = atoi(buf);
char* a = calloc (MAX, sizeof(char));
char* b = calloc(MAX, sizeof(char));
int i;
for (i = 0; i < n; i++)
{
fgets(a, MAX, stdin);
fgets(b, MAX, stdin);
a[strlen(a)-1] = '\0';
b[strlen(b)-1] = '\0';
int x = atoi(a);
int *arr = calloc(x, sizeof(int));
int k = 0;
char* token = strtok(b, " ");
while (token != NULL)
{
if (k > x)
break;
arr[k] = atoi(token);
token = strtok(NULL, " ");
k++;
}
printf("%d\n", majorityElement(x, arr));
free(arr)
}
free(buf);
free(a);
free(b);
return 1;
}
If I set MAX to 10^7 then the code passes all the test cases and is accepted for submission. However, if I set MAX to 10^8 (as required), I get a segmentation fault. How to overcome this?
Your program has several drawbacks.
For example within the function main there are unused variables declared like
int count;
int* num;
The function does take into account that -1 can be a valid value of the array.
There is a problem with the number of elements that can be specified in a test. It is a very big number (according to the description 1 <= N <= 10000000). So the value of MAX equal to 100 is too low. As a result the data can be read incorrectly and not completely. Also there can occur problems with the variable length arrays.
There is no need to use the function fgets because each integer number can be read using scanf.
I could suggest the following solution. Try it and see whether it will pass the tests.
#include <stdio.h>
#include <stdlib.h>
size_t majorityElement( const int a[], size_t n )
{
size_t majority_index = 0;
for ( size_t i = 1, votes = 1; i < n; i++ )
{
if ( a[majority_index] == a[i] )
{
++votes;
}
else
{
--votes;
}
if ( votes == 0 )
{
majority_index = i;
++votes;
}
}
size_t count = 0;
for ( size_t i = 0; i < n; i++ ) count += a[i] == a[majority_index];
return n / 2 < count ? majority_index : n;
}
int main(void)
{
size_t n = 0;
scanf( "%zu", &n );
for ( size_t i = 0; i < n; i++ )
{
size_t m = 0;
scanf( "%zu", &m );
if ( m != 0 )
{
int *a = calloc( m, sizeof( int ) );
for ( size_t j = 0; j < m; j++ ) scanf( "%d", a + j );
size_t majority_index = majorityElement( a, m );
printf( "%d\n", majority_index == m ? -1 : a[majority_index] );
free( a );
}
}
return 0;
}
If it will not pass the tests then it seems there is a bug in tests.:)
Or if the function return type may not be changed then the function definition can look like
int majorityElement( const int a[], size_t n )
{
size_t majority_index = 0;
for ( size_t i = 1, votes = 1; i < n; i++ )
{
if ( a[majority_index] == a[i] )
{
++votes;
}
else
{
--votes;
}
if ( votes == 0 )
{
majority_index = i;
++votes;
}
}
size_t count = 0;
for ( size_t i = 0; i < n; i++ ) count += a[i] == a[majority_index];
return n / 2 < count ? a[majority_index] : -1;
}

Reading in Large Integer txt file into 2D array

Attempting to create a program that reasons in a large Text File and filled them into Rows + Columns. Eventually I'll have to computer best path but having trouble just implementing an Array that can store the values.
#include <stdio.h>
#include <stdlib.h>
//max number of characters to read in a line. //MAXN=5 means 4 chars are read, then a \0 added in fgets. See reference for functionality
#define MAXN 100L
int main(void) //char** argv also ok {
int i=0, totalNums, totalNum,j=0;
size_t count;
int numbers[100][100];
char *line = malloc(100);
FILE* inFile ;
inFile = fopen("Downloads/readTopoData/topo983by450.txt", "r"); //open a file from user for reading
if( inFile == NULL) { // should print out a reasonable message of failure here
printf("no bueno \n");
exit(1);
}
while(getline(&line,&count, inFile)!=-1) {
for(;count>0; count--,j++)
sscanf(line, "%d", &numbers[i][j]);
i++;
}
totalNums = i;
totalNum = j;
for(i=0;i<totalNums; i++){
for(j=0;j<totalNum;j++){
printf("\n%d", numbers[i][j]);
}
}
fclose(inFile);
return 0;
}
count does not tell you how many numbers there are. Further: sscanf(line, "%d", &numbers[i][j]); will just scan the same number every time.
So this
for(;count>0; count--,j++)
sscanf(line, "%d", &numbers[i][j]);
should be something like:
j = 0;
int x = 0;
int t;
while(sscanf(line + x, "%d%n", &numbers[i][j], &t) == 1)
{
x += t;
++j;
}
where x together with %n helps you move to a new position in the string when a number has been scanned.
Here is a simplified version that scans for numbers in a string:
#include <stdio.h>
int main(void) {
char line[] = "10 20 30 40";
int numbers[4];
int j = 0;
int x = 0;
int t;
while(j < 4 && sscanf(line + x, "%d%n", &numbers[j], &t) == 1)
{
x += t;
++j;
}
for(t=0; t<j; ++t) printf("%d\n", numbers[t]);
return 0;
}
Output:
10
20
30
40

Count and get integers from a string using C

I am self teaching C programming.
I am trying to count number of int present in given string which are separated by space.
exp:
input str = "1 2 11 84384 0 212"
output should be: 1, 2, 11, 84384, 0, 212
total int = 6
When I try. It gives me all the digits as output which make sense since I am not using a right approach here.
I know in python I can use str.split (" ") function which can do my job very quickly.
But I want to try something similar in C. Trying to create my own split method.
#include <stdio.h>
#include <string.h>
void count_get_ints(const char *data) {
int buf[10000];
int cnt = 0, j=0;
for (int i=0; i<strlen(data); i++) {
if (isspace(data[i] == false)
buf[j] = data[i]-'0';
j++;
}
printf("%d", j);
}
// when I check the buffer it includes all the digits of the numbers.
// i.e for my example.
// buf = {1,2,1,1,8,4,3,8,4,0,2,1,2}
// I want buf to be following
// buf = {1,2,11,84384,0,212}
I know this is not a right approach to solve this problem. One way to keep track of prev and dynamically create a memory using number of non space digits encountered.
But I am not sure if that approach helps.
You want to build your number incrementally until you hit a space, then put that into the array. You can do this by multiplying by 10 then adding the next digit each time.
void count_get_ints(const char *data) {
int buf[10000];
int j = 0;
int current_number = 0;
// Move this outside the loop to eliminate recalculating the length each time
int total_length = strlen(data);
for (int i=0; i <= total_length; i++) {
// Go up to 1 character past the length so you
// capture the last number as well
if (i == total_length || isspace(data[i])) {
// Save the number, and reset it
buf[j++] = current_number;
current_number = 0;
}
else {
current_number *= 10;
current_number += data[i] - '0';
}
}
}
I think strtok will provide a cleaner solution, unless you really want to iterate over every char in the string. It has been a while since I did C, so please excuse any errors in the code below, hopefully it will give you the right idea.
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[19] = "1 2 11 84384 0 212";
const char s[2] = " ";
char *token;
int total;
total = 0;
token = strtok(str, s);
while (token != NULL) {
printf("%s\n", token);
total += atoi(token);
token = strtok(NULL, s);
}
printf("%d\n", total);
return 0;
}
You can check the ascii value of each character by doing c-'0'. If it's between [0,9], then it's an integer. By having a state variable, when you're inside an integer by checking if a given character is a number of space, you can keep track of the count by ignoring white space. Plus you don't need a buffer, what happens if data is larger than 10,000, and you write pass the end of the buffer?, undefined behavior will happen. This solution doesn't require a buffer.
Edit, the solution now prints the integers that are in the string
void count_get_ints(const char *data) {
int count = 0;
int state = 0;
int start = 0;
int end = 0;
for(int i = 0; i<strlen(data); i++){
int ascii = data[i]-'0';
if(ascii >= 0 && ascii <= 9){
if(state == 0){
start = i;
}
state = 1;
}else{
//Detected a whitespace
if(state == 1){
count++;
state = 0;
end = i;
//Print the integer from the start to end spot in data
for(int j = start; j<end; j++){
printf("%c",data[j]);
}
printf(" ");
}
}
}
//Check end
if(state == 1){
count++;
for(int j = start; j<strlen(data); j++){
printf("%c",data[j]);
}
printf(" ");
}
printf("Number of integers %d\n",count);
}
I believe the standard way of doing this would be using sscanf using the %n format specifier to keep track of how much of the string is read.
You can start with a large array to read into -
int array[100];
Then you can keep reading integers from the string till you can't read anymore or you are done reading 100.
int total = 0;
int cont = 0;
int ret = 1;
while(ret == 1 && total < 100) {
ret = sscanf(input, "%d%n", &array[total++], &cont);
input += cont;
}
total--;
printf("Total read = %d\n", total);
and array contains all the numbers read.
Here is the DEMO
Example using strtol
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
int count_get_ints(int output[], int output_size, const char *input) {
const char *p = input;
int cnt;
for(cnt = 0; cnt < output_size && *p; ++cnt){
char *endp;
long n;
errno = 0;
n = strtol(p, &endp, 10);
if(errno == 0 && (isspace((unsigned char)*endp) || !*endp) && INT_MIN <= n && n <= INT_MAX){
output[cnt] = n;
while(isspace((unsigned char)*endp))
++endp;//skip spaces
p = endp;//next parse point
} else {
fprintf(stderr, "invalid input '%s' in %s\n", p, __func__);
break;
}
}
return cnt;
}
int main(void) {
const char *input = "1 2 11 84384 0 212";
int data[10000];
int n = sizeof(data)/sizeof(*data);//number of elements of data
n = count_get_ints(data, n, input);
for(int i = 0; i < n; ++i){
if(i)
printf(", ");
printf("%d", data[i]);
}
puts("");
}
Assuming you don't have any non-numbers in your string, you can just count the number of spaces + 1 to find the number of integers in the string like so in this pseudo code:
for(i = 0; i < length of string; i++) {
if (string x[i] == " ") {
Add y to the list of strings
string y = "";
counter++;
}
string y += string x[i]
}
numberOfIntegers = counter + 1;
Also, this reads the data between the white spaces. Keep in mind this is pseudo code, so the syntax is different.

Seg. fault read line-by-line

I am trying to calculate the mode or the integer that appears the most for each line.
I get an print two values and then segmentation fault.
for (i = 0; i < count; i++) {
if (array[i]) {
int i, j, k, cnt = 1, p, big;
int b[MAX_NUM] = {0};
printf("count:%d\n", count);
for (i = 1; i <= array[i]; i++) {
for (j = i + 1; j <= array[i]; j++) {
if (array[i] == array[j])
printf("cnt:%d\n", cnt);
cnt++;
}
printf("cnt2:%d\n", cnt);
b[k] = cnt;
k++;
cnt = 1;
}
big = b[k];
p = 1;
for (i = 2; i <= array[i]; i++) {
if (big < b[i]) {
big = b[i];
p = i;
}
}
printf("The element that occurs offen is %d\n", array[p]);
printf("And it has occurred %d times\n", b[p]);
}
}
}
}
}
return 0;
}
EDIT:
See the look here in my code. The values that are printed are the numbers on each line of the file followed by a blank line like this:
1
2
3
4
5
6
5
4
5
14
62
48
14
1
3
5
7
9
123
456
789
1234
5678
34
34
34
34
34
1
1
2
2
2
2
2
3
3
4
4
4
4
5
5
6
7
7
7
1
1
Integers: 9
.....
You redefine i and p in an inner scope where they shadow current definitions. This is obviously unintentional as the for expression looks quite wrong:
if (array[i]) {
int i, j, k=1, cnt = 1, p, big;
// ^
// Redefinition of i.
// You should use a different name for the loop index below
// Same remark for p, it is safer to not redefine local variables
// in inner scopes. Any { starting a block creates a new scope
// in which variables can be defined, or in this case redefined.
...
for (i = 1; i <= array[i]; i++) {
...
for (i = 2; i <= array[i]; i++) {
...
In the same area of the code, you use k without a prior initialization.
The code to compute maximum occurrences can be put into a separate function and simplified this way:
#include <stdio.h>
// get the number of ocurrences of val in array a of size n
int get_number_of_occurrences(int a[], int n, int val) {
int cnt = 0, i;
for (i = 0; i < n; i++) {
if (a[i] == val)
cnt++;
}
return cnt;
}
// return the index for the number that occurs the most
int get_max_occurrence_index(int a[], int n) {
int p = 0, cnt, max = 0, i;
for (i = 0; i < n; i++) {
cnt = get_number_of_occurrences(a, n, a[i]);
if (max < cnt) {
max = cnt;
p = i;
}
}
return p;
}
int main() {
int i, n, a[20], max;
printf("Enter the maximum number of elements\n");
scanf("%d", &n);
printf("Enter the elements\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
i = get_max_occurrence_index(a, n);
max = get_number_of_occurrences(a, n, a[i]);
printf("The element that occurs most oftenly is %d\n", a[i]);
printf("And it has occurred %d times\n", max);
return 0;
}
If you want to use this logic in your original program, you should use it for each line as you read the file instead of at the end where it only applies to the last line. The line parsing code is incorrect too: you take the first digit's ASCII value as the value instead of parsing it with strtol().
Here is a corrected version:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_NUM 1000
#define MAX_LINE_LEN 2048
#define N 100
void fatal(const char *msg) {
printf("%s\n", msg);
exit(1);
}
int main(int argc, char *argv[]) {
FILE *fp;
char filename[100];
char line[MAX_LINE_LEN];
char *p;
int array[MAX_NUM];
int index, count, max;
printf("Please enter the file name: \n");
if (scanf("%99s", filename) != 1) {
fatal("Error in entering file.");
}
if ((fp = fopen(filename, "r")) == NULL) {
fatal("Unable to open the file.");
}
while ((p = fgets(line, MAX_LINE_LEN, fp)) != NULL) {
/* skip white space */
p += strspn(p, " \t\n");
if (*p == '#' || *p == '\0') {
/* ignore comment and blank lines */
continue;
}
/* scan and convert the numbers */
for (count = 0; *p != '\0'; ) {
if (isdigit((unsigned char)*p)) {
array[count++] = strtol(p, &p, 10);
printf("%d\n", array[count]);
} else {
/* skip to next space or end of string */
p += strcspn(p, " \t\n");
}
/* skip white space after the number */
p += strspn(p, " \t\n");
}
index = get_max_occurrence_index(array, count);
max = get_number_of_occurrences(array, count, array[index]);
printf("The element that occurs most often is %d\n", array[index]);
printf("And it has occurred %d times\n", max);
}
fclose(fp);
return 0;
}
Maybe i just can not see through your code, but at no point do i see you loading the actual numbers from your file into any variable or array to work with.
You are loading a line with while ((p = fgets(line, MAX_LINE_LEN, fp)) != NULL) {
Inside that loop, you are breaking this line into tokens to count how many numbers you have.
As far as i can see, array[count]++; is used to count how many numbers are in each line. Using the index as the line number.
You should start by thinking about how to get your data into a usable format
You can start trying to load the values into a 2 dimensional array.
Using the first dimension for the line number and the second for the values.
If you don't understand your code well enough, you should start with more comments
What do you use your difines and variables for.
#define MAX_NUM 1000 //maximum number of lines
int array[MAX_NUM] = {0}; //index refers to line number of file. used to count numbers in each line.
// read file line by line, split every line into tokens to count amount of numbers
while ((p = fgets(line, MAX_LINE_LEN, fp)) != NULL) {
if (count >= MAX_NUM) {
fatal("Array error");
}
if (line[0] != '#') {
p = strtok(line, " ");
while (p != NULL) {
if (isdigit(*p)) {
array[count]++;
}
p = strtok(NULL, " ");
}
}
count++;
//printf("COUNT:%D\n", count);
}
Choosing good variable names in addition would be even better
#define MAX_NUM 1000 -> #define MAX_LINE_NUM 1000
I have no idea about what your variables int i, j, k, cnt = 1, p, big; do.
Give them better names and or comment them. Will not only help you, but your helpers that need to understand what you intend to do with them.
First i thought you needed help with the algorithm for the mode so i wrote this first:
Using very basic stuff to keep it as simple as possible.
Would be cleaner to put it into functions if you know how.
Did not use functions since it seems you do not know completely how to work with them (you should look into that)
This algorithm is doing the following:
Take the first number in the array
Run through the array and everytime you find that number, you increase a counter
Save the number and the count as highest and highestCnt
Repeat with every number in the array and overwrite highest and highestCnt whenever count > highestCnt
When there are multiple number with the highest occurrence it will only remember the number that was counted first. If you want to return all numbers with the highest occurrence, the coude would need to be changed.
Could do something like checking if count == highestCnt and then set something so you know there is no single number with the highest count until you find one with an even higher count.
#include<stdio.h>
#define sizea 100
int main(void) {
int array[sizea] = { 1,3,6,8,3,6,7,4,6,9,0,3,5,12,65,3,76,5,3,54,
1,3,6,89,3,6,7,4,6,9,0,4,5,12,65,3,76,5,3,54,
1,9,6,8,3,45,7,4,6,9,0,89,5,12,65,3,76,5,3,54,
6,3,6,8,3,6,7,4,6,9,0,23,5,12,65,3,76,5,3,54,
1,3,6,90,3,6,7,4,6,9,0,5,5,12,65,3,76,5,3,54 };
int number;
int count = 1;
int highest = 1;
int highestCnt = 1;
int end = sizea - 1; //end defines at what element in the array the loop will end
int j; //j is used to load a number that will be count
int i; //i is used run through the array and compare every number the the one that is being count
for (j = 0; j <= end; j++) {
number = array[j]; // load a number to count
count = 1; // start counting at 1
for (i = j+1; i <= end; i++) {
// if we find the same number again, we increase the counter
// then we load the last element into the current array position
// then we change decrement "end" by 1
// this is like throwing out all the numbers we allready count
// using while instead of if so it will check the last element that was moved to current position as well
// check for i <= end so it doesnt count twice when the last element equals our number
while (array[i] == number && i <= end) {
count++;
array[i] = array[end];
end--;
}
}
// if the count of the number is highers the the previus highest, it's obviously our new highest count.
if (count > highestCnt) {
highest = number;
highestCnt = count;
}
}
printf("number: %i, count: %i", highest, highestCnt);
}

Arbitrary-strings using getline

So basically what my program did before i had to change it so that it would accept arbitrary values, was to take x-amount of words and the size of the words would also be arbitrary. (both are user inputted). I did this via a multiArray.
Then sorted according to alphabetical-order.
I'm just going to put it out there as my code is shit and I'm very unfamiliar with the usage of arbitrary-strings and pointers. I've read up on it in the manual but the concept needs to sink in a little bit first i believe. Anyhow, I get the error: "Abort trap: 6" when i run the program. Could anyone please help me fix this problem so that i can see how the code would look like if it was actually working, i think that would help me understand both pointers and allocating memory a lot better. Forever in debt if you do.
Current code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LENGTH 10
int main(){ //8
char *name;
char tname[] = {0};
char temp[] = {0};
int i=0, j=0, n=0;
ssize_t bytes_read;
size_t bytes_number;
printf("Enter the amount of words you want to input: ");
scanf("%d", &n);
printf("Enter %d words: ",n);
bytes_number = MAX_LENGTH;
name = (char *) malloc (bytes_number+ 1);
bytes_number = 0;
bytes_read = getline(&name, &bytes_number, stdin);
if (bytes_read == -1){
puts("ERROR!");
free(name);
}
for (i = 0; i < n; i++){
strcpy(&tname[i], &name[i]);
}
for (i = 0; i < n - 1 ; i++){
for ( j = i + 1; j < n; j++){
if (strcmp(&name[i], &name[j]) > 0){
strcpy(temp, &name[i]);
strcpy(&name[i], &name[j]);
strcpy(&name[j], temp);
}
}
}
printf("\n------------------------------------------\n");
printf("%-3s %4s %11s\n", "Input","|", "Output");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", &tname[i], &name[i]);
}
printf("------------------------------------------\n");
}
This
strcpy(&tname[i], &name[i]);
is completely wrong, if you just want to copy all the characters, then it's just
strcpy(tname, name);
which is equivalent to
for (size_t i = 0 ; name[i] != '\0' ; ++i)
tname[i] = name[i];
using strcpy(&tname[i], &name[i]) is wrong because it will copy all the bytes from name until '\0' is found, at every loop starting at the i-th character.
But this will fail again because tname is does not have room, it's an array with just one element.
Since you want to sort the strings, you DO NOT NEED TO COPY them. Just swap the pointers. Also
char temp[] = {0};
only allocates 1 character, thus
strcpy(temp, name);
will invoke Undefined Behavior.
Try this, maybe it's what you need
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
main(void)
{
char **words;
char *temp;
int word_count;
int actual_count;
char *word;
size_t length;
int result;
printf("Enter the amount of words you want to input: ");
if (scanf("%d%*c", &word_count) != 1)
return -1; // Input error
printf("Enter '%d' words:\n", word_count);
words = NULL;
word = NULL;
result = -1;
actual_count = 0;
length = 0;
for (int i = 0 ; i < word_count ; ++i)
{
char **pointer;
printf("Word(%d) > ", i + 1);
if ((length = getline(&word, &length, stdin)) <= 0)
goto cleanup;
// Grow the array of words
pointer = realloc(words, (i + 1) * sizeof(*pointer));
if (pointer == NULL)
goto cleanup; // Memory Exhausted
// Now it's safe to overwrite `words'
words = pointer;
words[i] = malloc(length);
if (words[i] == NULL)
goto cleanup; // Memory Exhausted
memcpy(words[i], word, length);
words[i][length - 1] = '\0'; // Replace '\n' with '\0'
actual_count += 1;
}
printf("Input : ");
for (int i = 0 ; i < actual_count ; ++i)
printf("%s\t", words[i]);
printf("\n");
for (int i = 0; i < actual_count - 1 ; i++)
{
for (int j = i + 1 ; j < actual_count ; ++j)
{
if (strcmp(words[i], words[j]) <= 0)
continue;
temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
printf("Output: ");
for (int i = 0 ; i < actual_count ; ++i)
printf("%s\t", words[i]);
printf("\n");
result = 0;
cleanup:
free(word);
for (int i = 0; i < actual_count ; i++)
free(words[i]);
free(words);
return result;
}
Note: This would consider an empty word (made completely of white space characters), as a valid word.

Resources