Comparison of string with character array in c -
i trying write code encoding , decoding purpose.once have dictionary,the dictionary formed stored in array of structure. construction below
typedef struct tuple { char a; char* cod; } result; the original string character array
char str[100]; now need method compare characters in original array formed dictionary. dictionary formed this
a ---0 b ---1 c ---01 example: original string aabcab, encode should 0010101
the code comparing string dictionary info follows, when code executed results follows:[warning] passing argument 2 of 'strcmp' makes pointer integer without cast [enabled default].
help appreciated.
for(i=0; i<strlen(str);i++)//read original string; { j=0; while(j<number_of_elements_in_dictionary)// above example=3 { if (strcmp(str[i],values[j]->a)==0) //compare original string character //dictionary { printf("%s", values[j]->cod);//print corresponding code //dictionary j++; //check next value of dictionary } } } printf("last=%s", str_en);//to print dictionary info corresponding //the original string info class="snippet-code-html lang-html prettyprint-override">#include<string.h> #include<stdio.h> #include<limits.h> #include<stdlib.h> typedef struct node { char ch; int freq; struct node *left; struct node *right; }node; typedef struct tuple { char a; char* cod; }result; /*declaring heap globally not need pass argument every time*/ /* heap implemented here min heap */ node * heap[1000000]; result * values[200]; int heapsize; char * str; char str_en[100]; // str_en[0] = '\0'; /*initialize heap*/ void init() { heapsize = 0; heap[0] = (node *)malloc(sizeof(node)); heap[0]->freq = -int_max; } /*insert element heap */ void insert(node * element) { heapsize++; heap[heapsize] = element; /*insert in lastly place*/ /*adjust position*/ int = heapsize; while(heap[now/2] -> freq >= element -> freq) { heap[now] = heap[now/2]; /= 2; } heap[now] = element; } node * deletemin() { /* heap[1] #ifndef #elif #endifthe minimum element. remove heap[1]. size of heap decreased. heap[1] has filled. set lastly element in place , see if fits. if not fit, take minimum element among both children , replaces parent it. 1 time again see if lastly element fits in place.*/ node * minelement,*lastelement; int child,now; minelement = heap[1]; lastelement = heap[heapsize--]; /* refers index @ */ for(now = 1; now*2 <= heapsize ;now = child) { /* kid index of element minimum among both children */ /* indexes of children i*2 , i*2 + 1*/ kid = now*2; /*child!=heapsize beacuse heap[heapsize+1] not exist, means has 1 kid */ if(child != heapsize && heap[child+1]->freq < heap[child] -> freq ) { child++; } /* check if lastly element fits ot not suffices check if lastly element less minimum element among both children*/ if(lastelement -> freq > heap[child] -> freq) { heap[now] = heap[child]; } else /* fits there */ { break; } } heap[now] = lastelement; homecoming minelement; } void encode(result *value, int s) { int pos,i,j; pos=1; values[pos]=value;//im here values[pos]->a =value->a; values[pos]->cod=value->cod; printf("result= %c , %s", values[pos]->a, values[pos]->cod); pos++; /*the problem exists here while executing next for-loop, code doesn't execute due loop*/ for(i=0; i<strlen(str);i++){ j=0; while(j<4) { if(str[i]==values[j]->a) { printf("%s", values[j]->cod); j++; } } } printf("last=%s", str_en); } void print(node *temp,char *code, int s)//, char *buf) { int i,pos=1,j; if(temp->left==null && temp->right==null) { printf("\n\nchar %c code %s\n",temp->ch,code); result * value = (result *) malloc(sizeof(result)); value->a=temp->ch; value->cod= code; encode(value,s); return; } int length = strlen(code); char leftcode[512],rightcode[512]; strcpy(leftcode,code); strcpy(rightcode,code); leftcode[length] = '0'; leftcode[length+1] = '\0'; rightcode[length] = '1'; rightcode[length+1] = '\0'; print(temp->right,rightcode,s); print(temp->left,leftcode,s); } /* given list of characters along frequencies, our goal predict encoding of characters such total length of message when encoded becomes minimum */ int main() { char buf[250]; char character[26]; int = 0,j=0,count[26]={0}; char c = 97; init(); int distinct_char=0 ; char ch; int freq; int iter; printf("enter string"); scanf("%s", str); printf("string=%s",str); (i=0; i<strlen(str);i++) { for(j=0;j<26;j++) { if (tolower(str[i]) == (c+j)) { count[j]++; } } } for(j=0;j<26;j++) { if(count[j]>0) { printf("\n%c -> %d",97+j,count[j]); distinct_char++; character[j] = 97+j; } } printf("\n number of distinct_characters=%d\n", distinct_char); if(distinct_char==1) { printf("char %c code 0\n",c); homecoming 0; } for(j=0;j<distinct_char;j++) { printf("\ncharacter= %c , frequency=%d", character[j],count[j]); node * temp = (node *) malloc(sizeof(node)); temp -> ch = character[j]; temp -> freq = count[j]; temp -> left = temp -> right = null; insert(temp); } for(i=0;i<distinct_char-1 ;i++) { node * left = deletemin(); node * right = deletemin(); node * temp = (node *) malloc(sizeof(node)); temp -> ch = 0; temp -> left = left; temp -> right = right; temp -> freq = left->freq + right -> freq; insert(temp); } node *tree = deletemin(); char code[512]; code[0] = '\0'; print(tree,code, distinct_char); }
as warning suggests, you're missing pointer here. strcmp's signature reads
int strcmp(const char *s1, const char *s2); but both parameters of type char (the array indexing makes char char*, regular dereferencing , sec parameter char anyway).
however, want comparing single character string character. can utilize regular relational operators:
if(str[i] == values[j]->a) { // ... } note answering precise question code may wrong or ineffective anyway.
c arrays string structure string-comparison
No comments:
Post a Comment