if statement doesn't work? - c

wordCur is a string of capital letters, and dictionary is an array of strings, no matter what I input into wordCur, I am always returned 0.
Edit: I updated the code a little bit, and added an abridged version of the rest of the program for some context. As it is shown here, it just crashes when it gets to checkValid
int main() {
FILE *ifp;
ifp = fopen("dictionary.txt", "r");
int* lDist[26];
int* lUsed[26];
int dictLen;
int i;
fscanf(ifp, "%d", &dictLen);
char dictionary[dictLen][7];
char* letters[7];
int scoreCur = 0;
int scoreHi = 0;
char wordCur[7];
char wordHi[7];
int isWord = 0;
//reads the dictionary into the array
for (i = 0; i < dictLen; i++) {
fscanf(ifp, "%s", &dictionary[i]);
}
scanf("%s", wordCur);
isWord = checkValid(wordCur, dictLen, dictionary);
if (isWord == 1) {
scoreCur = calcScore(wordCur);
}
//fclose(ifp); not sure why, but this causes a crash
return 0;
}
int checkValid (char *wordCur,int dictLen, char dictionary[dictLen]) {
int valid = 0;
int i;
for (i = 0; i < dictLen; i++){
int helper = strcmp(wordCur, dictionary[i]);
if (helper = 0){
valid = 1;
}
}

wordCur is a string of capital letters
int checkValid (char wordCur,int dictLen, char dictionary[dictLen])
No, wordCur is a single character. Not a string. A string in C is represented as an array of characters, terminated by a character with the value 0. You need a pointer argument, char *wordCur.

Your code should probably look more like this:
int checkValid(const char *wordCur, // word to search for (string)
int dictLen, // no of entries in dictionary
char dictionary[][7]) // dictionary (array of strings)
{
int valid = 0;
int i;
for (i = 0; i < dictLen; i++)
{
if (strcmp(wordCur, dictionary[i]) == 0)
{
valid = 1;
break;
}
}
return valid;
}

wordCur is a string of capital letters, and dictionary is an array of strings
Try this:
int checkValid (const char *wordCur,int dictLen, const char *dictionary[])
By the way, you keep searching, even after you found what you are looking for, and the comaprison is wrong anyway for strings. I suggest:
for (i = 0; i < dictLen; i++){
if (strcmp(wordCur, dictionary[i]) == 0){
valid = 1;
break;
}
}

Related

Function returning different values inside another function

My first function checks for the number of letters that a substring contains from its string.
int num_matches(char* word, char* letters) {
char scrabble[128];
strcpy(scrabble, letters);
int num = 0;
for (int i = 0; i < strlen(word); i++) {
if (strchr(letters, word[i]) == NULL) {
return -1;
}
for (int j = 0; j < strlen(letters); j++) {
if (word[i] == letters[j]) {
num++;
scrabble[j] = '\0';
break;
}
}
}
return num;
}
It returns 4 for "QWOP", "QWOP". However, inside the following function it is returning the same incorrect value for every function call, even when buff and letters print as "QWOP", "QWOP" from my debugging attempt.
void read_words(int num_count[128], char* (*word_idx)[128], int argc, char** argv) {
FILE* fp = fopen(argv[1], "r");
char* letters = argv[2];
int idx = 0;
char buff[128];
int result = 0;
while (fgets(buff, 128, fp) != NULL) {
printf("buff:%s letters:%s\n", buff, letters);
result = num_matches(buff, letters);
printf("result: %d\n", result);
num_count[idx] = result;
char* word = malloc(strlen(buff) + 1);
strcpy(word, buff);
(*word_idx)[idx] = word;
idx++;
result = 0;
}
fclose(fp);
}
buff:QWOP
letters:QWOP
result: -1
My txt file:
ABC
DEFG
QWOP
QWOP
QUOKKA
QUOLL
QUASH
QUANDONG
Since fgets stops at a newline and there are no spaces in my text file, I don't think there should be any problems with reading buff.
fgets reads the data with \n character at the end.
Your function will fail in this case. It is very easy to test:
int main(void)
{
printf("%d\n", num_matches("ABCD", "ABCD"));
printf("%d\n", num_matches("ABCD\n", "ABCD"));
}
Result:
4
-1
You need to remove \n from the buff.
Another problem is the second function. The parameter char* (*word_idx)[128] is a pointer to an array of 128 pointers to char. I do not think that is something you want.

Adding strings to 2D array in C

I'm trying to write a program that looks for the first empty space in a 2D array and adds a custom string to that space. I have tried some things that i found on the internet but none seem to work or match my specific scenario. This is it:
#include <stdio.h>
#include <string.h>
int tags[10] = {1,2,3,4,5};
char owners[10][10] = {"per1", "per2", "per3", "per4", "per5"};
int tagAdd;
char ownerAdd;
int i;
int addBool;
int j;
int len;
int main()
{
printf("Enter the tag ID you want to add: ");
scanf("%d", &tagAdd);
printf("Enter the tag owners name: ");
scanf("%d", &ownerAdd);
len = strlen(ownerAdd);
while (i<10)
{
if (tags[i] == 0)
{
tags[i] = tagAdd;
owners[i][len] = ownerAdd; //This is the part I can't figure out
addBool = 1;
}
if (addBool == 1)
{
break;
}
i++;
}
i = 0;
addBool = 0;
len = 0;
while (i<10)
{
printf("tag[%d]", tags[i]);
len = strlen(owners[i]);
printf(" is owned by ");
while (j < len)
{
printf("%c", owners[i][j]);
j++;
}
printf("\n\r");
i++;
j = 0;
}
}
You cannot do this:
char ownerAdd;
scanf("%d", &ownerAdd);
len = strlen(ownerAdd);
You are passing the incorrect types. ownerAdd is a single char, scanf
expects with %d a pointer to int, you are passing a pointer to char and if
scanf converts the value, it will overflow. And strlen expects a char*
which points to a valid string (0-terminated). You are doing all this wrong.
This would be correct:
char ownerAdd[100];
scanf("%99s", ownerAdd);
len = strlen(ownerAdd);
And for replacing a value:
owners[i][len] = ownerAdd; //This is the part I can't figure out
is also wrong, because owners[i] is a char[10], you have to do:
strncpy(owners[i], ownerAdd, sizeof owners[i]);
owners[i][sizeof(owners[i]) - 1] = 0;
to copy the string.
The next error is that you don't initialize i and do (the first loop)
while (i<10)
{
...
}
this is going to fail. Same thing with j, it is uninitialized.

Rather than bubblesorting these names...the program crashes

I clearly am doing something wrong but, for the life of me, can't figure out what.
int main(int argc, char *argv[])
{
int done=0;
int end=0;
int didswap=0;
char *temp[2] = {0};
int i;
int x;
printf("This function Bubble sorts the Flintstones in alphabetical order!\n");
printf("The Flintstones names are:\nFred\nBarney\nWilma\nPebbles\nDino\n");
char *names[5] = {0};
names [0] = "Fred";
names [1] = "Barney";
names [2] = "Wilma";
names [3] = "Pebbles";
names [4] = "Dino";
while(end == 0)
{
for(i=0;i<4;i++)
{
if (strcmp(names[i],names[i+1])>0)
{
strcpy(temp[0],names[i]);
strcpy(temp[1],names[i+1]);
strcpy(names[i],temp[1]);
strcpy(names[i+1],temp[0]);
didswap = 1;
}
else
{
didswap = 0;
}
done = done+didswap;
}
if (done == 0)
end = 1;
else
done = 0;
}
printf("When alphabetized they are:\n");
for (i = 0; i < 5; i++)
{
printf("%s \n", names[i]);
}
system("PAUSE");
return EXIT_SUCCESS;
}
You have an array of string literals. These may be held in read only memory so you can't change their content. You can however change the order you store pointers to them in names by replacing
strcpy(temp[0],names[i]);
strcpy(temp[1],names[i+1]);
strcpy(names[i],temp[1]);
strcpy(names[i+1],temp[0]);
with
const char* tmp = names[i];
names[i] = names[i+1];
names[i+1] = tmp;
strcpy(temp[0],names[i]);
strcpy(temp[1],names[i+1]);
strcpy(names[i],temp[1]);
strcpy(names[i+1],temp[0])
names strings are string literals and string literals are immutable in C. Attempting to modifiy a string literal invokes undefined behavior.

Printing Array of Strings

I'm parsing a text file:
Hello, this is a text file.
and creating by turning the file into a char[]. Now I want to take the array, iterate through it, and create an array of arrays that splits the file into words:
string[0] = Hello
string[1] = this
string[2] = is
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TextReader.h"
#include <ctype.h>
void printWord(char *string) {
int i;
for (i = 0; i < strlen(string); i ++)
printf("%c", string[i]);
printf("\n");
}
void getWord(char *string) {
char sentences[5][4];
int i;
int letter_counter = 0;
int word_counter = 0;
for (i = 0; i < strlen(string); i ++) {
// Checks if the character is a letter
if (isalpha(string[i])) {
sentences[word_counter][letter_counter] = string[i];
letter_counter++;
} else {
sentences[word_counter][letter_counter + 1] = '\0';
word_counter++;
letter_counter = 0;
}
}
// This is the code to see what it returns:
i = 0;
for (i; i < 5; i ++) {
int a = 0;
for (a; a < 4; a++) {
printf("%c", sentences[i][a]);
}
printf("\n");
}
}
int main() {
// This just returns the character array. No errors or problems here.
char *string = readFile("test.txt");
getWord(string);
return 0;
}
This is what it returns:
Hell
o
this
is
a) w
I suspect this has something to do with pointers and stuff. I come from a strong Java background so I'm still getting used to C.
With sentences[5][4] you're limiting the number of sentences to 5 and the length of each word to 4. You'll need to make it bigger in order to process more and longer words. Try sentences[10][10]. You're also not checking if your input words aren't longer than what sentences can handle. With bigger inputs this can lead to heap-overflows & acces violations, remember that C does not check your pointers for you!
Of course, if you're going to use this method for bigger files with bigger words you'll need to make it bigger or allocate it dymanically.
sample that do not use strtok:
void getWord(char *string){
char buff[32];
int letter_counter = 0;
int word_counter = 0;
int i=0;
char ch;
while(!isalpha(string[i]))++i;//skip
while(ch=string[i]){
if(isalpha(ch)){
buff[letter_counter++] = ch;
++i;
} else {
buff[letter_counter] = '\0';
printf("string[%d] = %s\n", word_counter++, buff);//copy to dynamic allocate array
letter_counter = 0;
while(string[++i] && !isalpha(string[i]));//skip
}
}
}
use strtok version:
void getWord(const char *string){
char buff[1024];//Unnecessary if possible change
char *p;
int word_counter = 0;
strcpy(buff, string);
for(p=buff;NULL!=(p=strtok(p, " ,."));p=NULL){//delimiter != (not isaplha(ch))
printf("string[%d] = %s\n", word_counter++, p);//copy to dynamic allocate array
}
}

Reverse Words in String

For some reason, I can't get this to work! Can anyone tell me where I've gone wrong? This is supposed to reverse the words in a give string (i.e from "this is a test" to "test a is this")
#include <stdio.h>
#include <stdlib.h>
char *reverse(char const *input)
{
char *ret = (char *)malloc(sizeof(char) * strlen(input));
int length = 0;
int numWords = 1;
int i;
for(i=0; input[i]!=NULL; i++)
{
length++;
if(input[i]==' ')
numWords++;
}
char words[numWords];
int currentWord = numWords;
for(i=0; input[i]!=NULL; i++)
{
if (input[i]==' '){
currentWord--;
}else{
words[currentWord] = words[currentWord] + input[i];
}
}
for(i=0; i < numWords; i++)
{
ret = ret + words[i];
}
return ret;
}
int main(int argc, char **argv)
{
int nTestcase = 0;
int i = 0;
char inputstr[100];
char *reversedStr = NULL;
scanf("%d\n", &nTestcase);
for (i = 0; i < nTestcase; i++)
{
fgets(inputstr, 100, stdin);
reversedStr = reverse(inputstr);
printf("%s\n", reversedStr);
free(reversedStr);
memset(inputstr, 0, 100);
}
return 0;
}
words[currentWord] = words[currentWord] + input[i];
You can't add characters to each other like that expecting string concatenation. And I imagine you expect words to be an array of words (i.e. strings), but its type is not that, words is just an array of characters.
Like #Tom said, you're doing this again in the last for loop:
ret = ret + words[i];
Joseph, unlike other programming languages ( c# or PHP ), string handling functions of C are quite basic.
You cannot add strings directly, however there are a host of library functions you can use to accomplish the same task.
Check out,
strtok - Use it to split the string to words. strtok reference
strncat - Use to concatenate strings
strings in C are just byte arrays terminated with the null character ( byte with value 0).
Here is shorter/cleaner way
private char[] reverseWords(char[] words) {
int j = words.length - 1;
for (int i = 0; i < (words.length)/2; i++) {
if(i==j)
continue;
char temp = words[i];
words[i] = words[j];
words[j]=temp;
j--;
}
int lastIndex = 0;
for (int i=0;i<words.length;i++){
if(words[i]==' '){
words = reverseWord(words,lastIndex,i-1);
lastIndex=i+1;
}
}
words = reverseWord(words,lastIndex,words.length-1);
return words;
}
private char[] reverseWord(char[] words, int from, int to) {
int j=to;
for(int i=from;i<((to/2)+from/2);i++){
if(i==j) continue;
char temp = words[j];
words[j]=words[i];
words[i]=temp;
j--;
}
return words;
}

Resources