Trying to implement a word search in C - c

As the title says, I'm trying to make a word search in C. However, when I run what I have, I'm getting an aborted(core dumped) message.
My code:
void lefttoright(int rowcol, char **matrix, char* find){
int i, j, k, q, len, count = 0;
len = strlen(find);
for (i = 0; i < rowcol; i++){
for (j = 0; j < rowcol; j++){
if (matrix[i][j] == find[0]){
char* correct = malloc(sizeof(char) * 20);
for (q = j; q < rowcol; q++){
for (k = 0; k < len; k++){
if (matrix[i][q] == find[k]){
correct[k] = matrix[i][q];
}
}
if (strcmp(correct, find) == 0){
count++;
printf("%s\n", correct);
printf("%d\n", count);
}
}
free(correct);
}
else continue;
}
}
printf("%d", count);
}
.
if (strcmp(correct, find) == 0){
count++;
printf("%s\n", correct);
printf("%d\n", count);
}
prints
bagel
1
bagel
2
bagel
3
bagel
4
bagel
5
bagel
6
bagel
7
bagel
8
bagel
9
bagel
10
bagel
11
bagel
12
bagel
13
Aborted (core dumped)
What is making it abort? Also, what do I need to do differently to make count == 1 (the word only appears once)?
My entire program if needed:
#include <stdio.h>
#include <stdlib.h>
#include "scanner.h"
#include <string.h>
#include <ctype.h>
char** matrixMaker(int argc, char **argv);
void displayMatrix(int rowcol, char **matrix);
void lefttoright(int rowcol, char **matrix, char* find);
int main(int argc, char **argv){
FILE *fp = fopen(argv[2], "r");
int rowcol = atoi(argv[1]);
int a, i = 0, j;
unsigned char cha;
char **matrix;
matrix = malloc( sizeof(char *) * rowcol);
for (a=0; a<20; a++)
matrix[a] = malloc( sizeof(char) * rowcol);
for (i = 0; i < rowcol; i++){
for (j = 0; j < rowcol; j++){
cha = tolower((unsigned char)readChar(fp));
matrix[i][j] = cha;
}
}
char* find = malloc(sizeof(char) * 20);
displayMatrix(rowcol, matrix);
printf("Enter a word to find in the puzzle : \n");
scanf("%s", find);
while(find[i]){
putchar(tolower((unsigned char)find[i]));
i++;
}
lefttoright(rowcol, matrix, find);
free(matrix);
return 0;
}
void displayMatrix(int rowcol, char **matrix){
int r,c;
for (r = 0; r < rowcol; ++r){
for (c = 0; c < rowcol; ++c){
printf("%c",matrix[r][c]);
}
printf("\n");
}
}
void lefttoright(int rowcol, char **matrix, char* find){
int i, j, k, q, len, count = 0;
len = strlen(find);
for (i = 0; i < rowcol; i++){
for (j = 0; j < rowcol; j++){
if (matrix[i][j] == find[0]){
char* correct = malloc(sizeof(char) * 20);
for (q = j; q < rowcol; q++){
for (k = 0; k < len; k++){
if (matrix[i][q] == find[k]){
correct[k] = matrix[i][q];
}
}
if (strcmp(correct, find) == 0){
count++;
printf("%s\n", correct);
printf("%d\n", count);
}
}
free(correct);
}
else continue;
}
}
printf("%d", count);
}

Related

Multidimensional array - fscanf

int main(){
int word, r=3, i, j;
FILE *fp1 = fopen("key.txt","r");
int **arr = (int **)malloc(sizeof(int *) * r);
for(i = 0;i<r;i++)
arr[i] = (int *)malloc(sizeof(int)*r);
int a = 0, b = 0;
while (!feof(fp1)) {
fscanf(fp1,"%d",&word);
if (b == r){
a++;
b=0;
continue;
}
arr[a][b++] = word;
}
for (i = 0; i < r; i++)
for (j = 0; j < r; j++)
printf("%d \n", arr[i][j]);
fclose(fp1);
}
And this is my key.txt.
0 -1 0
-1 2 -1
0 -1 0
I want to store key.txt in a dynamic 2d array but it did not work. All the time a part of it is missing. Which part is wrong?
You should use fscanf in while, not feof
You should delete continue.
The follow code could work:
#include <stdio.h>
#include <stdlib.h>
int main(){
int word, r=3, i, j;
FILE *fp1 = fopen("key.txt","r");
int **arr = (int **)malloc(sizeof(int *) * r);
for(i = 0;i<r;i++)
arr[i] = (int *)malloc(sizeof(int)*r);
int a = 0, b = 0;
while (fscanf(fp1,"%d",&word) == 1) {
if (b == r) {
a++;
b=0;
}
arr[a][b++] = word;
}
for (i = 0; i < r; i++) {
for (j = 0; j < r; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
fclose(fp1);
return 0;
}

Create a function to copy n char like strcpy in C

#include <stdio.h>
#include <stdlib.h>
void nhap(char* &scr, int *n)
{
do
{
printf("Input the string length:\n");
scanf_s("%d", n);
} while (n < 0);
scr = (char*)malloc(*n * sizeof(char));
for (int i = 0; i < *n; i++)
{
scanf_s("%c", (scr + i));
}
}
void xuat(char* scr, int n)
{
printf("\nThe content of string: ");
for (int i = 0; i < n; i++)
{
printf("%c", *(scr + i));
}
}
char* StringNCopy(char* dest, char* scr, int n)
{
if (n == NULL)
{
return NULL;
}
dest = (char*)realloc(dest, n * sizeof(char));
for (int i = 0; i < n; i++)
{
for (int j = *(scr + n); j > 0; j--)
{
*(dest + i) = *(scr + j);
}
}
*(dest + n) = '\0';
return dest;
}
void main()
{
char *a;
char *b=NULL;
int n;
nhap(a, &n);
xuat(a, n);
StringNCopy(b, a, 4);
printf("%s", *b);
free(a);
}
Excuse me, I have a problem, I want to create a function likes strcpy but there is some errors I can't fix by myself. I think it will copy n elements from char* scr to char* dest, but when I run my code, it crashed. Can you help me to fix the code and explain to me. I'm very thankful.
The for loops should be in this way
for (int i = 0; i < n; i++)
{
*(dest + i) = *(scr + i);
}
You dont need nested for loops for this, because you just need to traverse the array once and copy the values.
Corrected program
#include <stdio.h>
#include <stdlib.h>
void nhap(char* &scr, int *n)
{
do
{
printf("Input the string length:\n");
scanf("%d", n);
} while (n < 0);
scr = (char*)malloc((*n+1) * sizeof(char)); //allocated size should be n+1
fflush(stdin);
for (int i = 0; i < *n; i++)
{
scanf("%c", (scr+i ));
}
}
void xuat(char* scr, int n)
{
printf("\nThe content of string: ");
for (int i = 0; i < n; i++)
{
printf("%c", *(scr + i));
}
}
void StringNCopy(char* &dest, char* &scr, int n) //no need to return the string aas you can pass it as reference
{
if (n == NULL)
{
return;
}
dest = (char*)realloc(dest, (n+1) * sizeof(char)); //alloted size should be n+1
for (int i = 0; i < n; i++)
{
*(dest + i) = *(scr + i); //no need of nested loops
}
*(dest + n) = '\0';
}
int main()
{
char *a;
char *b=NULL;
int n;
nhap(a, &n);
xuat(a, n);
StringNCopy(b, a, 4);
printf("\n6%s", b);
free(a);
}
Tested and working fine.Observe the errors mentioned in comments

For loop won't stop

I've made an array that is between [0,1000], and I've printed it. Next step is to arrange the array using switch statements into five different cases 0 to 199, etc. When trying to do so the for loop won't stop. I tried putting a printf after countOne in case 1, no printout occurs either.
Any suggestions
Thanks for your help
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int n;
int arraySize;
int randN;
int rand();
int countOne = 0;
int countTwo = 0;
int countThree = 0;
int countFour = 0;
int countFive = 0;
int countSix = 0;
int *p;
int *p1;
int main()
{
printf("What is the size of the array\n");
scanf("%d", &n);
//MAKING THE N-size ARRAY
int array[n];
int i;
for (i = 0; i < n; i++) {
randN = rand() % 999;
array[i] = randN;
p = (int*)malloc(i * sizeof(int));
p[i] = array[i];
}
//SORTING THE N-size ARRAY
for (i = 0; i < n; i++) {
printf("%i\n", array[i]);
}
p = (int*)malloc(sizeof(int));
p1 = (int*)malloc(5 * sizeof(int));
p1[0] = countOne;
p1[1] = countTwo;
for (i = 0; i < n; i++) {
switch (i) {
case 1:
for (i = 0; array[i] >= 0 && array[i] <= 199; i++) {
countOne++;
}
case 2:
for (i = 0; array[i] >= 200 && array[i] <= 399; i++) {
countTwo++;
return countTwo;
}
}
}
}
HERE IS MY CODE AS OF CURRENT:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n;
int arraySize;
int randN;
int rand();
int countOne = 0;
int countTwo = 0;
int countThree = 0;
int countFour = 0;
int countFive = 0;
int countSix = 0;
int *p;
int *p1;
printf("What is the size of the array\n");
scanf("%d", &n);
//MAKING THE N-size ARRAY
int array[n];
int i;
for (i = 0 ; i < n; i++ )
{
randN=rand() % 999;
array[i]=randN;
p=(int*)malloc(i*sizeof(int));
p[i]= array[i];
}
//PRINTING THE N-size ARRAY
for (i = 0; i < n; i++)
{
printf("%i\n", array[i]);
}
//SORTING THE N-size ARRAY
int j;
for (j = 0 ; j < n ; j++)
{
switch(j)
{
case 1:
for(i = 0 ; array[i] >= 0 && array[i] <= 199; i++)
{
countOne++;
return countOne;
}
case 2:
for(i = 0 ; array[i] >= 200 && array[i] <= 399; i++)
{
countTwo++;
return countTwo;
}
}
}
HERE IS THE PRINT OUT:
What is the size of the array
3
823
7
347
There is 0 integers between 0 and 199
There is 0 integers between 200 and 399
The program has many problems.
Here is a simpler version:
#include <stdio.h>
#include <stdlib.h>
int compare_ints(const void *a, const void *b) {
const int *pa = a, *pb = b;
return (*pa > *pb) - (*pa < *pb);
}
int main(void) {
int i, n;
int stats[5] = { 0, 0, 0, 0, 0 };
printf("What is the size of the array?\n");
scanf("%d", &n);
//MAKING THE N-size ARRAY
int *array = malloc(n * sizeof(int));
int *saved = malloc(n * sizeof(int));
if (array == NULL || saved == NULL) {
printf("cannot allocate arrays\n");
exit(1);
}
for (i = 0; i < n; i++) {
int randN = rand() % 999;
saved[i] = array[i] = randN;
stats[randN / 200] += 1;
}
printf("initial array contents:\n);
for (i = 0; i < n; i++) {
printf("%i\n", array[i]);
}
printf("\n");
//SORTING THE N-size ARRAY
qsort(array, n, sizeof(*array), compare_ints);
printf("sorted array:\n);
for (i = 0; i < n; i++) {
printf("%i\n", array[i]);
}
printf("\n");
for (i = 0; i < 5; i++) {
printf("%d values between %d and %d\n", stats[i], i * 200, (i + 1) * 200 - 1);
}
printf("\n");
// do whatever else you are supposed to with array and saved
//...
free(array);
free(saved);
return 0;
}

C Program, string to int array command line sort

This program is supposed to sort numbers entered from the command line as -a for small to large sort, or -d for large to small sort. I compiled this program earlier, ran it and the output was fine. I tested it on my laptop later and then the sorting was all wasn't working correctly.
I entered ./sort -a 5 14 10 18 20 2 100 6 7 1 The Result: -1950355064 2 5 6 10 14 18 20 100 I entered: ./sort -d 5 14 10 18 20 2 100 6 7 1 The Result: 761262572 32767 18 14 10 6 20 5 100 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
// function declarations
void small_to_large(int a[], int n);
void large_to_small(int a[], int n);
int main(int argc, char *argv[])
{
int i;
char letter_d[3] = "-d"; // find "-d" in function
char letter_a[3] = "-a"; // find "-a" in function
int arg_d;
int arg_a;
char *find_letter = argv[1];
int stringToInt[N+1];
for( i = 0; i < argc; i++){
printf("%s ", argv[i]);
}
printf("\n");
arg_d = strcmp(find_letter, letter_d);
arg_a = strcmp(find_letter, letter_a);
if (arg_d == 0) {
printf("descend!\n");
for(i = 2; i<argc; i++) {
stringToInt[i] = atoi(argv[i]);
}
large_to_small(stringToInt, N);
for(i = 0; i < N; i++) {
printf(" %d", stringToInt[i]);
}
}
else if (arg_a == 0) {
printf("ascend!\n");
for(i = 2; i < argc; i++) {
stringToInt[i] = atoi(argv[i]);
}
small_to_large(stringToInt, N);
for(i = 0; i < N; i++) {
printf(" %d", stringToInt[i]);
}
}
else {
printf("Invalid command: %s\n", find_letter);
}
printf("\n");
return 0;
}
// small_to_large function
void small_to_large(int a[], int n)
{
int i, largest = 0, temp;
if (n == 1)
return;
for (i = 1; i < n; i++)
if (a[i] > a[largest])
largest = i;
if (largest < n - 1) {
temp = a[n-1];
a[n-1] = a[largest];
a[largest] = temp;
}
small_to_large(a, n - 1);
}
// large_to_small function
void large_to_small(int a[], int n)
{
int i, largest = 0, temp;
if(n == 1)
return;
for (i = n; i >= 2; i--)
if(a[i] < a[largest])
largest = i;
if (largest < n - 1) {
temp = a[n-1];
a[n-1] = a[largest];
a[largest] = temp;
}
large_to_small(a, n-1);
}
There are mainly two problems.
stringToInt[i] = atoi(argv[i]); : index of stringToInt should
start from 0.
large_to_small is wrong. Sorting algorithm may be the same. So you can use the same code.
Fix to sample:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// function declarations
void small_to_large(int a[], int n);
void large_to_small(int a[], int n);
#define OPT_D "-d"
#define OPT_A "-a"
int main(int argc, char *argv[]) {
int i;
for(i = 0; i < argc; i++){
printf("%s ", argv[i]);
}
printf("\n");
int n = argc - 2;//-2 : program_name and option
if(n <= 0){
printf("Usage : %s option(-a or -d) numbers...\n", *argv);
return EXIT_FAILURE;
}
int stringToInt[n];
for(i = 0; i < n; ++i)
stringToInt[i] = atoi(argv[i+2]);
char *opt = argv[1];
bool opt_d, opt_a;
opt_d = !strcmp(opt, OPT_D);
opt_a = !strcmp(opt, OPT_A);
if(opt_d){
printf("descend!\n");
large_to_small(stringToInt, n);
} else if(opt_a) {
printf("ascend!\n");
small_to_large(stringToInt, n);
} else {
printf("Invalid option: %s\n", opt);
return EXIT_FAILURE;
}
for(i = 0; i < n; i++) {
printf("%d ", stringToInt[i]);
}
printf("\n");
return 0;
}
void selection_sort(int a[], int n, bool test(int a, int b)){
int i, select = 0, temp;
if (n == 1)
return;
for (i = 1; i < n; i++)
if(test(a[i], a[select]))
select = i;
if (select != n-1) {
temp = a[n-1];
a[n-1] = a[select];
a[select] = temp;
}
selection_sort(a, n - 1, test);
}
static inline bool greater(int a, int b){
return a > b;
}
static inline bool smaller(int a, int b){
return a < b;
}
// small_to_large function
void small_to_large(int a[], int n) {
selection_sort(a, n, greater);
}
// large_to_small function
void large_to_small(int a[], int n){
selection_sort(a, n, smaller);
}
I believe I managed to get it to work by changing
large_to_small(stringToInt, N); ---> large_to_small(stringToInt, argc);
small_to_large(stringToInt, N); ---> small_to_large(stringToInt, argc);

C program store file to jagged array and sort it

I am trying to create C program to read a text file and sort it by ascending order. The example of text file is
2
3; 2, 5, 7
6; 4, 7, 8, 9, 5, 2
with the first line indicated the number of rows, the number after the ";" indicated elements each rows and elements separated by ",".
So my idea is to create a dynamic jagged array with rows as the first number, then point each row to the different array with element. Sort the pointer arrays first then sort elements of each arrays. This is what I have tried so far
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int SortLists();
int main ()
{
int i,j = 0;
char filename[10]; //name of the file
char line[100];
int rows = 3; //I have to initialized this to test my code first
int cols;
int **jaggedArr; //jagged array
jaggerArr = malloc (rows*sizeof(int*)) ;
printf("Enter the file name with .txt : ");
scanf("%s", filename);
FILE *filePtr = fopen(filename, "r");
int num;
if (filePtr != NULL)
{
while (fgets(line, sizeof(line), filePtr) != NULL) //read each line of the text
{
cols = atoi(strtok(line, ";")); //use strtk to break elements
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
jaggedArr[i][j] = atoi(strtok(line, ",")); //parse into the jagged array
}
}
}
fclose(filePtr);
}
}
int SortLists(int list[], int size) //sort method
{
int i,j,temp;
for (i = 0; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
As a beginner in C, I am not familiar with the idea of pointer, which a lot different with C#.
Sorry for my bad English as its not my first language. Thank you so much for helping me.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define S_(x) #x
#define S(x) S_(x)
void SortLists(int list[], int size);
int main(void){
char filename[FILENAME_MAX+1];
char line[100];
int rows, cols;
printf("Enter the file name with .txt : ");
scanf("%" S(FILENAME_MAX) "[^\n]%*c", filename);
FILE *filePtr = fopen(filename, "r");
if(!filePtr)
return EXIT_FAILURE;
fscanf(filePtr, "%d ", &rows);
int **jaggedArr;
jaggedArr = malloc (rows * sizeof(int*));
int *sizeArr = malloc(rows * sizeof(int));
int r = 0, c;
while (fgets(line, sizeof(line), filePtr) != NULL){
sizeArr[r] = cols = atoi(strtok(line, ";"));
jaggedArr[r] = malloc(cols * sizeof(int));
for (c = 0; c < cols; ++c){
jaggedArr[r][c] = atoi(strtok(NULL, ","));
}
SortLists(jaggedArr[r++], cols);
}
fclose(filePtr);
//check print and deallocation
for(r = 0;r < rows; ++r){
for(c = 0; c < sizeArr[r]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
free(jaggedArr[r]);
}
free(jaggedArr);
free(sizeArr);
return 0;
}
void SortLists(int list[], int size){
int i,j,temp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (list[i] > list[j]){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
for extra question.
#include <stdio.h>
void SortLists(int list[], int size);
void SortRows(int *jaggedArr[], int size, int *rowSize);
int main(void){
int row1[] = {4,7,8,9,5,2};
int row2[] = {2,5,7};
int *jaggedArr[] = { row1, row2};
int rows = 2;
int sizeArr[] = {6,3};
int i, r, c;
for(i=0;i<rows;++i)
SortLists(jaggedArr[i], sizeArr[i]);
for(r = 0;r < rows; ++r){
for(c = 0; c < sizeArr[r]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
}
printf("\n");
SortRows(jaggedArr, rows, sizeArr);
for(r = 0;r < rows; ++r){
for(c = 0; c < sizeArr[r]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
}
return 0;
}
void SortLists(int list[], int size){
int i,j,temp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (list[i] > list[j]){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
void SortRows(int *jaggedArr[], int size, int *rowSize){
int i,j,temp,*tempp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (rowSize[i] > rowSize[j]){
//swap in pairs
temp = rowSize[i];
rowSize[i] = rowSize[j];
rowSize[j] = temp;
tempp = jaggedArr[i];
jaggedArr[i] = jaggedArr[j];
jaggedArr[j] = tempp;
}
}
}
}
#include <stdio.h>
void SortLists(int list[], int size);
void SortRows(int *jaggedArr[], int size);
int main(void){
int row1[] = {6, 4,7,8,9,5,2};//The first element represents the number of elements.
int row2[] = {3, 2,5,7};
int *jaggedArr[] = { row1, row2};
int rows = 2;
//int sizeArr[] = {6,3};//Not required
int i, r, c;
for(i=0;i<rows;++i)
SortLists(jaggedArr[i]+1, jaggedArr[i][0]);
SortRows(jaggedArr, rows);
for(r = 0;r < rows; ++r){
for(c = 1; c <= jaggedArr[r][0]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
}
return 0;
}
void SortLists(int list[], int size){
int i,j,temp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (list[i] > list[j]){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
void SortRows(int *jaggedArr[], int size){
int i,j,*tempp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (jaggedArr[i][0] > jaggedArr[j][0]){
tempp = jaggedArr[i];
jaggedArr[i] = jaggedArr[j];
jaggedArr[j] = tempp;
}
}
}
}

Resources