Consedering 2 strings, I want to do a program in C that returns the number of possible matches between this 2 strings.
For example
$>./a.out "abc" "a*"
1
Because there is only 1 possible match : *="bc"
$>./a.out "abc" "a**"
3
Because there is only 3 possible matches : (*="bc", *="") ; (*="", *="bc") ; (*="b", *="c")
$>./a.out "ab cool ab cool ab" "ab*ab*"
2
Because there is 2 possible matches : (*=" cool ", *=" cool ab") ; (*=" cool ab cool ", *="")
I did a function "int match(char*, char*)" that returns true when there exists a match and false when there does not exist.
But i would like now to count the possible matches.
Any advices or suggestions?
int match(char* string, char* star) {
if (string[0] == '\0' && star[0] == '\0')
return 1;
else if (star[0] == '*')
return match(string, star+1);
else if (string[0] == '\0')
return 0;
else if (string[0] == star[0]){
if (star[-1] == '*') {
if (!match(string+1, star+1))
return match(string+1, star);
}
return match(string+1, star+1);
}
else if (string[0] != star[0] && star[-1] == '*')
return match(string+1, star);
else if (string[0] != star[0] && star[-1] != '*')
return 0;
}
int count_match(char* string, shar* star) {
int i;
for (i = 0; star[i] != '\0'; i += 1) {
if (star[i] != '*') {
if (star[i] != string[i])
return 0;
}
else {
int count = 0;
int j;
for (j = i; string[j] != '\0'; j += 1) {
count += count_match(string + j; star + i + 1);
count += count_match(string + j; star + i + 1);
return count;
}
}
return (string[i] == star[i]);
}
Related
the push to vaccinate children has taken on fresh urgency amid concerns that the new omicron variant of the virus first identified in southern africa and hong kong in late november will spread quickly in the united states causing a surge in infections already back on the rise from the easily transmitted delta variant given the pervasiveness of delta and prospects of new variants spreading in the united states having as much immunity in the population as possible is critical said dr amesh adalja senior scholar at the johns hopkins center for health security
This is my assignment:
replace multiple spaces to one space between words and delete unnecessary spaces at the beginning and the end.
count the words
print edited string
dont use a new string, just edit.
I can't find problem. It should count the words but it can not do. Help me, please.
//Counting words program C
#include <stdio.h>
#define N 5000
int main(void) {
FILE *fp;
char text[N];
int k, d, leng, spacecount = 0;
int m, j, z, i, p, n;
if ((fp = fopen("soru.txt", "r")) == NULL) {
printf("Dosya acma hatasi!");
return 1;
}
fgets(text, N - 1, fp);
while (k < N && text[k] != '\0') {
leng++;
k++;
}
z = leng;
for (i = 0; i < leng; i++) {
if (i = 0 && text[i] == ' ') {
z--;
for (m = 0; m < leng; m++) {
text[m] = text[m + 1];
}
i--;
text[z] == '\0';
} else
if (text[i] ==' ' && text[i + 1] == ' ') {
z--;
for (j = i; j < leng; j++) {
text[j + 1] = text[j + 2];
}
i--;
text[z] == '\0';
} else
if (text[i] == ' ' && text[i + 1] == '\0') {
z--;
for (j = i; j < leng; j++) {
text[j] = text[j + 1];
}
i--;
text[z] == '\0';
} else
if (text[i] == '\0') {
break;
}
}
while (text[d] != '\0') {
if (text[d] == ' ')
spacecount++;
d++;
}
printf("kelime sayisi: %d" , spacecount + 1);
printf("\n cikti:%s ", text);
fclose(fp);
return 0;
}
I can't find problem. It should count the word but it can not do. Help me, please
for(i=0; i < leng; i++) {
if(i=0 && text[i]== ' '){
z--;
for(m=0; m< leng; m++ ){
text[m] = text [m+1];}
i--;
}
else if(1<i<z && text[i] ==' ' && text[i+1] == ' ' ){
z--;
for(j=i; j<leng ; j++) {
text[j+1] = text [j+2];}
i--;
}
else if(i=z && text[i] ==' ' && text[i+1] == '\0' ){
z--;
for(j=i; j<leng ; j++) {
text[j] = text [j+1]; }
i--;
}
},// I think problem in here. Endless loop
Your code is too complicated. You can solve the problem with 2 index variables: one to read the characters from the input line, one to write the relevant characters into the same buffer.
You would keep track of the previous character, starting with space, and detect the beginning of words as the current character is not a space following a space. You would thus count the words and only output a space before each word except the first on a line.
Here is a modified version:
//Counting words program C
#include <stdio.h>
#define N 5000
int main(void) {
FILE *fp;
char text[N];
int total_words = 0;
if ((fp = fopen("soru.txt", "r")) == NULL) {
printf("Dosya açma hatası!\n");
return 1;
}
while (fgets(text, N, fp) != NULL) {
int len = strlen(text);
int word_count = 0;
char c, lastc = ' ';
int i, j;
// strip the trailing newline
if (len > 0 && text[len - 1] == '\n') {
text[--len] == '\0';
}
for (i = j = 0; i < len; i++) {
c = text[i];
if (c != ' ') {
if (lastc == ' ') {
if (word_count > 0) {
// output a space between words
text[j++] = ' ';
}
word_count++;
}
text[j++] = c; // copy the non space character
}
lastc = c;
}
text[j] = '\0'; // set the null terminator
printf("kelime sayısı: %d\n", word_count);
printf("çıktı: %s\n", text);
total_words += word_count;
}
fclose(fp);
printf("\ntoplam kelime sayısı: %d\n", total_words);
return 0;
}
Note a silly bug in your code: if (i = 0 && text[i] == ' ') is parsed as if ((i = (0 && (text[i] == ' '))) != 0) which is always false and sets i to the value 0. C expression syntax is very powerful but somewhat error prone and confusing. I advise you to use -Wall or -Weverything as a compiler option to let the compiler warn about potential mistakes.
Similarly, you should not write if (1<i<z && ...: 1<i<z is parsed as 1<(i<z) which is always false. You must write 1 < i && i < z or more idiomatically i > 1 && i < z
Can't figure out the bug in my code. Every time I input a sentence, the count does increment but the word adds the first letter of the previous word and increments one letter every time. How do I fix this?
void numberOfWordOccurrences(char str[MAX_CHAR]) {
int count = 0, i = 0, j = 0;
char uniqueToken[99][999];
int tokenCount[99] = {0};
while(str[i] != '\0') {
char token[999];
while(str[i] != ' ' && str[i] != '\0') {
token[j++] = str[i++];
}
if(token[j - 1] == ':' || token[j - 1] == ',' || token[j - 1] == '.' || token[j - 1] == ';' || token[j - 1] == '?' || token[j - 1] == '!') {
token[j - 1] = '\0';
}
//null
token[j] = '\0';
//flag
int flag = -1;
for(j = 0; j < count; j++) {
if(strcmp(uniqueToken[j], token) == 0) {
//if flag is valid, then...
flag = j;
tokenCount[flag] = token[flag] + 1;
break;
}
}
if(flag <= 1) {
tokenCount[count] = tokenCount[count] + 1;
strcpy(uniqueToken[count++], token);
}
i++;
}
}```
first you have to set j=0 inside of your main while loop ,otherwise when you go inside of this loop for(j = 0; j < count; j++) jwill in increase , so here token[j++] = str[i++]; you won't start to copy str in token from j=0 that is why you have previous words letters.
second I believe this condition if(flag <= 1) should be if(flag == -1) because if for example first and fifth word are similar flag would be 0 and again that string would be copied in uniqueToken.
also pay attention if you reach \0 you with your two i++ you will pass it and here while(str[i] != '\0') you won't check it so I suggest while(str[i-1] != '\0') also before sending string check if there is anything in it(in a case str[0]='\0'.
look
void numberOfWordOccurrences(char str[]) {
int count = 0, i = 0, j = 0;
char uniqueToken[99][999];
int tokenCount[99] = { 0 };
while (str[i-1] != '\0') {
j = 0;
char token[999];
while (str[i] != ' ' && str[i] != '\0') {
token[j++] = str[i++];
}
if (token[j - 1] == ':' || token[j - 1] == ',' || token[j - 1] == '.' || token[j - 1] == ';' || token[j - 1] == '?' || token[j - 1] == '!') {
token[j - 1] = '\0';
}
//null
token[j] = '\0';
//flag
int flag = -1;
for (j = 0; j < count; j++) {
if (strcmp(uniqueToken[j], token) == 0) {
//if flag is valid, then...
flag = j;
tokenCount[flag] = token[flag] + 1;
break;
}
}
if (flag == -1) {
tokenCount[count] = tokenCount[count] + 1;
strcpy(uniqueToken[count++], token);
strcpy(uniqueToken[count], "\0");
}
i++;
}
}
i'm new in c programming and i need some help with this function because i cant figure it out,
i need to make a function that receives a string and prints out the similar words (the order of the letters ,the amount of the letters and if the letters are capital or small doesn't matter) for example:
if received "Nanny have you any cheap peach?"
the output is:
Nanny any
cheap peach
i can't use pointers ,and cant use string.h library.
i tried and came up with this but i had no luck on figuring it out
void FindSimilarWords(char str2[]){
int f,i,j,last,count=0,count1=0,k,letter,temp=0;
char word1[wordsize],word2[wordsize];
for (i = SIZE2 - 1; i >= 0; i--)
{
if (str2[i] != ' ' && str2[i] != '\0')
{
last = i;
break;
}
}
for (i = 0; i<= last; i++)
{
k = 0;
j = i;
do {
word1[k] = str2[j];
k++;
j++;
} while (str2[j] != ' '&&str2[j] != '\0');
word1[k] = '\0';
for (letter =last; letter >= j-1; letter--)
{
temp = letter;
while (temp != ' ')
{
count1++;
temp--;
}
f = 0;
for (k--; k >= 0; k--)
{
if (str2[j] == word1[k])
{
count++;
word2[f] = str2[j];
f++;
}
}
if (count == count1)
printf("%s %s\n", word1, word2);
else
while (letter != ' ')
letter--;
}
while (str2[i] != ' ')
i++;
}
}
Before reading this question please note that my question pertains to a school assignment. For this assignment the only function we are allowed to use is malloc(). Everything else must be done without the use of other libraries.
I'm calling a function ft_split_whitespaces. This function takes a string as input and "splits" it into words. Words are separated spaces, tabs and line breaks.
#include <stdio.h>
char **ft_split_whitespaces(char *str);
int main(void)
{
char *str = "what is";
char **test = ft_split_whitespaces(str);
}
With respect to the example above the result at each index should be
test[0] = "what"
test[1] = "is"
test[2] = NULL
However, I am only able to print the results of test[0]. All other indices do not get printed to display. Heres an example of some code that I assume should print the results of my function.
int i = 0;
while(test[i] != NULL)
{
printf("%s", test[i]);
i++;
}
When this portion of code is ran, only test[0] is printed to the output. I've been sitting here trying to debug my code for hours. If anyone has some spare time and doesn't mind looking over my code, I'd appreciate it tremendously. I have a feeling it may be an issue with how I'm using malloc, but I still cant figure it out.
#include <stdlib.h>
#include <stdio.h>
int count_whitespaces(char *str)
{
int space_count;
int i;
space_count = 0;
i = 0;
while(str[i] != '\0')
{
if(str[i] == ' ' || str[i] == 9 || str[i] == '\n')
{
if(str[i+1] != ' ' && str[i+1] != 9 && str[i+1] != '\n')
space_count++;
}
i++;
}
return (space_count);
}
int check_whitespace(char c)
{
if (c == ' ' || c == 9 || c == '\n' || c == '\0')
{
return (1);
}
else
return(0);
}
int count_characters(char *str, int i)
{
int char_count;
char_count = 0;
while(str[i])
{
if(str[i+1] != ' ' && str[i+1] != 9 && str[i+1] != '\n')
char_count++;
else
break;
i++;
}
return (char_count);
}
char **ft_split_whitespaces(char *str)
{
int i;
int j;
int k;
char **word;
int space;
i = 0;
j = 0;
k = 0;
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
if((word[j] = malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL)
return (NULL);
while (check_whitespace(str[i]) == 0)
{
word[j][k] = str[i];
i++;
k++;
}
j++;
}
}
j++;
word[j] = NULL;
j = 0
return word;
}
You forgot to reset k. The outer while loop in ft_split_whitespaces should look like that
while (str[i] != '\0') {
if (check_whitespace(str[i]) == 1){
i++;
}
else {
if ((word[j] =
malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL){
return (NULL);
}
while (check_whitespace(str[i]) == 0) {
word[j][k] = str[i];
i++;
k++;
}
word[j][k] = '\0';
j++;
k = 0; // reset k
}
}
So I actually figured out what was going wrong with my code as I finished typing this question (thanks stack overflow!). I decided to post it anyways, because I thought it might be a good learning experience for coding newbies such as myself.
This is where my issue was occurring.
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
if((word[j] = malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL)
return (NULL);
while (check_whitespace(str[i]) == 0)
{
word[j][k] = str[i];
i++;
k++;
}
j++;
}
}
I malloc'd my char **word outside of the while loop using the following code
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
And then within the actual while loop I malloc'd it again using
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
Using malloc multiple times on the same variable was causing all sorts of weird issues. So within the while loop I ended up using a new variable I declared as char *words. Here is that portion of the while loop with the correct code.
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
words = malloc(sizeof(char) * (count_characters(str, i) + 1));
k = 0;
while (check_whitespace(str[i]) == 0)
{
words[k] = str[i];
i++;
k++;
}
words[k] = '\0';
word[j] = words;
j++;
}
}
I am writing a function that looks for the first 4 whole numbers separated by a comma in a string.
For example, if the string is:
123,4,9.5,av4,3,g1,1,6
the function will extract to a different array the numbers: 123,4,3,1
Everything works well until I try entering an input with a space in the middle, which is not supposed to be considered as a valid number, but then the loop stops once it bumps into the space. Is there a solution for that?
I'm not allowed to use any other library than stdio.h.
Here's my code:
int getParameters(int parameters[], int size) {
char input[100];
int indexInput = 0, indexParameters = 0;
int skip = 0, numberSeen = 0, negativeSeen = 0;
int i = 0;
scanf("%s", input);
for ( ; input[indexInput]!= '\0' && indexParameters < size; ++indexInput) {
if (input[indexInput] == ',' && skip == 1) {
parameters[indexParameters] = 0;
skip = 0;
negativeSeen = 0;
} else if (input[indexInput] == ',' && negativeSeen == 1 && numberSeen == 1) {
printf(ERROR);
return -1;
} else if (input[indexInput] == ','&& numberSeen == 1) {
numberSeen = 0;
indexParameters++;
} else if (input[indexInput] == ',') {
continue;
} else if (input[indexInput] == '-' && skip == 1) {
continue;
} else if (input[indexInput] == '-' && numberSeen == 0 && input[indexInput+1] != '-') {
negativeSeen = 1;
} else if (input[indexInput] <= '9' && input[indexInput] >= '0') {
parameters[indexParameters] *= 10;
parameters[indexParameters] += input[indexInput] - '0';
numberSeen = 1;
} else {
skip = 1;
}
}
if (skip == 1)
parameters[indexParameters] = 0;
while (i < 4) {
printf("%d,", parameters[i]);
++i;
}
return 0;
}
The name of the %s format specifier is a bit misleading, it matches a sequence of non-whitespace characters. As you can see it's meant to read a word, not the whole string.
Perhaps you want to read a whole line?