Number of Word Counts in a Sentence - C program [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am thinking of a C program which will take a sentence as an input and display the words in it with number of occurances. e.g.
Input = I love you and I hate you
Output = I 2 love 1 you 2 and 1 hate 1
Can you suggest me the logic or code for this program? I have in below program seprated the words from a sentence.
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "I love you and I hate you";
char delims[] = " ";
int i =0;
char *result = NULL;
result = strtok (str, delims);
while(result !=NULL)
{
++i;
printf("%s\n",result);
result = strtok (NULL, delims);
}
}
Now How can I store these words with their number of occurances in the sentence.

Store words in a collection of some kind, together with a counter. For every word see if it already exists in your collection, and if it does then increase the counter.

Related

How to change the content of argv in C? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have been trying to search around the net but still couldn't find anything about it
I would like to do something like increase the file name,
int main(int argc , char *argv[]){
char charArray = {argv[1]};
char * string[] = {"-0001.c"};
int i = 0;
strcat(charArray[0] , string[0]);
puts(charArray[0]);
return 0;
}
so as expected, I execute it as
./file test
the output is
test-0001.c
what if I want to increase the number?
so that it can print out the following
test-0001.c
test-0002.c
test-0003.c
by using while loop?
Your program as currently written should not currently work. Try turning on warnings and fixing them. (with the compiler flags -Wall -Wextra -pedantic for GCC or clang)
That said, I'd use a variable-length array and sprintf:
for(int counter = 1; counter < 10000; counter++) {
char filename[strlen(argv[1]) + 8];
sprintf(filename, "%s-%04d.c", argv[1], counter);
// do something with filename
}

Efficiently checking anagrams? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
What is the most efficient way of checking if 2 strings (represented by const char *) are anagrams or not? I know we can sort and then compare. But, sorting is nlogn.
Thanks for the help.
EDIT: I got a vote down for not showing my attempt. So, my attempt is following:
int anagram(const char * c1, const char *c2){
char *s1=my_sort(c1);
char *s2=my_sort(c2);
return strcmp(s1,s2)==0?1:0;
}
It is from one of my blog posts :)
/**
* Works for 0-127 ASCII string
**/
int isanagram(const char* s1,const char* s2){
int hash[128];
int i;
for(i=0;i<128;i++)
hash[i]=0;
while(*s1) hash[*s1++]++;
while(*s2) hash[*s2++]--;
for(i=0;i<128;i++)
if(hash[i]) return 0;
return 1;
}
Explanation: Every char in the alphabet has a position in the hash table. For each char in s1 we increment the count for that char and for each char in s2 we decrement the count for the char in the hash table. if all of the char has 0 count at the end then both s1 and s2 have same number of each char, which is the definition of anagram.
Complexity: O(n) if n>128 , where n is the max of length of s1 and s2

How to convert a string containing a hex character code to the character value? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of chars "0x55".
What I want to do is convert it to a char which is going to be U (because ASCII 0x55 = U).
So how to do this conversion?
#include <windows.h>
int main()
{
array[] = "0x55"
char test;
**// I want to move the string to that test to be one character which is U**
}
Any suggestions?
I think this is what you are after:
int main(int argc,char**argv)
{
char array[] = "0x55";
int value;
char test;
sscanf(array,"%x",&value);
test = value;
return 0;
}
In C++, I would code it a little differently, but this seems more like a C question.

an efficent version similar to strstr [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
here is a general implementation
int stridx (char[] src, char[] str){
int i,j,k;
for(i=0;i < (src.len - str.len);i++){
for(j=i,k=0; str[k] != '\0' && str[k] == src[i]; j++,k++);
if( k> 0 && str[k]=='\0') return i;
}
return -1;
}
The worst case of the algorithm could be n^2, if we have aaaaaaaaaaaaaaaaaaaaaaaa (assuming both src and str are very long, and the length of them is very close).
Can I have a better algorithm?
You could use the Boyer-Moore algorithm, which is O(n). Here's sample C code.
It's not necessary to calculate the length of the strings :
char *strr(char *s1,char *s2)
{
int i,j;
for(i=0;s1[i];i++)
if(s1[i]==s2[0])
for(j=0;s1[i+j]==s2[j];j++)
if(!s2[j+1]) return &s1[i];
return NULL;
}
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm is another linear time algorithm. OTOH, the simple algorithms stay because in practise they are faster for most of the strings people actually search with.

Shortest C code to reverse a string [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
Not counting the function signature (just the body) can anybody produce C code shorter than this function that will reverse a string and return the result as a pointer to the reversed string.. (not using a string reverse library function either)?
char * reverse_str(char * s)
{
char c,*f=s,*p=s;while(*p)p++;while(--p>s){c=*p;*p=*s;*s++=c;}return f;
}
not much longer, but it works.
#include <string.h>
/* precondition: s!=const && s!=NULL && *s!='\0' */
char *mystrrev(char *s)
{
char *a=s,*e=s+strlen(s)-1;
while( a<e )
{
char c=*a;
*a++=*e;
*e--=c;
}
return s;
}

Resources