Reputation: 119
Ok so i am trying to write a function that checks whether or not a letter of a word exists within an array of strings(It does that for every letter in the word). After tinkering with it for a while, i figured that it crashed when it tried to use the strcmp(). I don't know what i am doing wrong since i just started learning C so any help would be appreciated. Here is the function:
char SingleChar(char *lex, int wordnum,char *word){
int i,j,k;
for(i=0;i<strlen(word);i++){
for(j=0;j<wordnum;j++){
for(k=0;k<strlen(lex[j]);k++){
if(strcmp(word[i],lex[k])){
return word[i];
}
}
}
}
return 0;
}
Upvotes: 0
Views: 1185
Reputation: 79
You have a misunderstanding about what char *
means. It is a pointer to character. In C string are just a pointer to a character, flowed by other characters and a null terminator. What this means in your case, is that lex
is a single string, not a list of strings.
ie
char *a = "imastring";
denotes that a is the address of a sequential piece of memory containing the characters [i][m][a][s][t][r][i][n][g][\0]. In C the null terminator is used to denote the end of a string.
What this means is that when you are calling strlen(lex[j])
you are just referencing a single character in lex and then reading to the end of the string, so your result will just decrease monotonically.
You probably want to do is use a double pointer. char ** list
will point to an address, which points to an address referencing a block of sequential characters.
char ** list = (char **)malloc(sizeof(char *) * 5);
would allocate you 5 sequential memory address which could then point to strings themselves. And you can assign them values as follows.
list[0] = a
I hope this helps.
Upvotes: 1
Reputation: 46
In C there is no true string type. A string in C is just an array of characters. An array of strings would be an array of pointers to an array of characters in memory.
If you wanted to check for a letter within an array of "strings". You would want a pointer that moves through each letter of the array and compares each character.
The strcmp() function will return true (1) or false (0), depending on whether the strings are equal or not.
So what you'd want I think is for your program to compare the characters of your word with every other word in the array of strings.
This program goes through the entire word then tells you if the letter exists. For each letter of whatever word you enter.
--
#include <stdio.h>
#include <string.h>
/*
Function to check for a letter of a word
in an array of strings */
void singleChar(char *word,int arrlength, char *strings[])
{
int length = 0;
length = strlen(word); /* Calculates the length of the string */
for(int y = 0; y < arrlength ; y++) /*Increments to the next word in the array */
{
for(int i=0; i <= length ; i++) /*increments to the next letter of the word you want to check */
{
for(int x=0; x < strlen(strings[y]) ; x++) /*Increments x based on the length of the string */
{
char *p = strings[y];
if(word[i] == p[x]) /*Compares the the first letter of both strings */
{
printf("The letter %c exists.\n", word[i]);
}
}
}
}
}
int main ( void )
{
/*Example */
char *p = "Hello";
char *a[2];
a[0]="Hello";
singleChar(p, 1,a);
}
Upvotes: 0
Reputation: 31
Don't really see an array of strings... Your C file should look roughly like this:
#include <stdio.h>
char SingleChar(char *lex, int wordnum, char *word){"your function in here"};
int main(){
// Declare your variables here
// Call your function here SingleChar(params)
return 0;
}
To compare characters:
if(word[i]==lex[k]){
return word[i];
break;
}
Not quite sure what you are trying to do with your function other than that. You need to be more specific, I don't see your array of strings input.
Upvotes: 0