My program is designed to allow the user to input a string and my program will output the number of occurrences of each letters and words. My program also sorts the words alphabetically.
My issue is: I output the words seen (first unsorted) and their occurrences as a table, and in my table I don't want duplicates. SOLVED
For example, if the word "to" was seen twice I just want the word "to" to appear only once in my table outputting the number of occurrences.
How can I fix this? Also, why is it that i can't simply set string[i] == delim to apply to every delimiter rather than having to assign it manually for each delimiter?
Edit: Fixed my output error. But how can I set a condition for string[i] to equal any of the delimiters in my code rather than just work for the space bar? For example on my output, if i enter "you, you" it will out put "you, you" rather than just "you". How can I write it so it removes the comma and compares "you, you" to be as one word.
Any help is appreciated. My code is below:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char delim[] = ", . - !*()&^%$##<> ? []{}\\ / \"";
#define SIZE 1000
void occurrences(char s[], int count[]);
void lower(char s[]);
int main()
{
char string[SIZE], words[SIZE][SIZE], temp[SIZE];
int i = 0, j = 0, k = 0, n = 0, count;
int c = 0, cnt[26] = { 0 };
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
lower(string);
occurrences(string, cnt);
printf("Number of occurrences of each letter in the text: \n");
for (c = 0; c < 26; c++){
if (cnt[c] != 0){
printf("%c \t %d\n", c + 'a', cnt[c]);
}
}
/*extracting each and every string and copying to a different place */
while (string[i] != '\0')
{
if (string[i] == ' ')
{
words[j][k] = '\0';
k = 0;
j++;
}
else
{
words[j][k++] = string[i];
}
i++;
}
words[j][k] = '\0';
n = j;
printf("Unsorted Frequency:\n");
for (i = 0; i < n; i++)
{
strcpy(temp, words[i]);
for (j = i + 1; j <= n; j++)
{
if (strcmp(words[i], words[j]) == 0)
{
for (a = j; a <= n; a++)
strcpy(words[a], words[a + 1]);
n--;
}
} //inner for
}
i = 0;
/* find the frequency of each word */
while (i <= n) {
count = 1;
if (i != n) {
for (j = i + 1; j <= n; j++) {
if (strcmp(words[i], words[j]) == 0) {
count++;
}
}
}
/* count - indicates the frequecy of word[i] */
printf("%s\t%d\n", words[i], count);
/* skipping to the next word to process */
i = i + count;
}
printf("ALphabetical Order:\n");
for (i = 0; i < n; i++)
{
strcpy(temp, words[i]);
for (j = i + 1; j <= n; j++)
{
if (strcmp(words[i], words[j]) > 0)
{
strcpy(temp, words[j]);
strcpy(words[j], words[i]);
strcpy(words[i], temp);
}
}
}
i = 0;
while (i <= n) {
count = 1;
if (i != n) {
for (j = i + 1; j <= n; j++) {
if (strcmp(words[i], words[j]) == 0) {
count++;
}
}
}
printf("%s\n", words[i]);
i = i + count;
}
return 0;
}
void occurrences(char s[], int count[]){
int i = 0;
while (s[i] != '\0'){
if (s[i] >= 'a' && s[i] <= 'z')
count[s[i] - 'a']++;
i++;
}
}
void lower(char s[]){
int i = 0;
while (s[i] != '\0'){
if (s[i] >= 'A' && s[i] <= 'Z'){
s[i] = (s[i] - 'A') + 'a';
}
i++;
}
}
I have the solution to your problem and its name is called Wall. No, not the type to bang your head against when you encounter a problem that you can't seem to solve but for the Warnings that you want your compiler to emit: ALL OF THEM.
If you compile C code with out using -Wall then you can commit all the errors that people tell you is why C is so dangerous. But once you enable Warnings the compiler will tell you about them.
I have 4 for your program:
for (c; c< 26; c++) { That first c doesn't do anything, this could be written for (; c < 26; c++) { or perhaps beter as for (c = 0; c <26; c++) {
words[i] == NULL "Statement with no effect". Well that probably isn't what you wanted to do. The compiler tells you that that line doesn't do anything.
"Unused variable 'text'." That is pretty clear too: you have defined text as a variable but then never used it. Perhaps you meant to or perhaps it was a variable you thought you needed. Either way it can go now.
"Control reaches end of non-void function". In C main is usually defined as int main, i.e. main returns an int. Standard practice is to return 0 if the program successfully completed and some other value on error. Adding return 0; at the end of main will work.
You can simplify your delimiters. Anything that is not a-z (after lower casing it), is a delimiter. You don't [need to] care which one it is. It's the end of a word. Rather than specify delimiters, specify chars that are word chars (e.g. if words were C symbols, the word chars would be: A-Z, a-z, 0-9, and _). But, it looks like you only want a-z.
Here are some [untested] examples:
void
scanline(char *buf)
{
int chr;
char *lhs;
char *rhs;
char tmp[5000];
lhs = tmp;
for (rhs = buf; *rhs != 0; ++rhs) {
chr = *rhs;
if ((chr >= 'A') && (chr <= 'Z'))
chr = (chr - 'A') + 'a';
if ((chr >= 'a') && (chr <= 'z')) {
*lhs++ = chr;
char_histogram[chr] += 1;
continue;
}
*lhs = 0;
if (lhs > tmp)
count_string(tmp);
lhs = tmp;
}
if (lhs > tmp) {
*lhs = 0;
count_string(tmp);
}
}
void
count_string(char *str)
{
int idx;
int match;
match = -1;
for (idx = 0; idx < word_count; ++idx) {
if (strcmp(words[idx],str) == 0) {
match = idx;
break;
}
}
if (match < 0) {
match = word_count++;
strcpy(words[match],str);
}
word_histogram[match] += 1;
}
Using separate arrays is ugly. Using a struct might be better:
#define STRMAX 100 // max string length
#define WORDMAX 1000 // max number of strings
struct word {
int word_hist; // histogram value
char word_string[STRMAX]; // string value
};
int word_count; // number of elements in wordlist
struct word wordlist[WORDMAX]; // list of known words
Related
As a part of my homework with C, I had to make a function which splits a string to all the words starting with the key(which is a letter) inserted.
Everything works great except for the free function,
When I try to free the dynamic matrix by function (rows and then skeleton)
I get an error that the program has triggered a breakpoint.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char **Split(char *str, char letter, int *size);
void free_mat(char **mat, int size);
int main() {
int size, i;
char letter;
char STR[100];
char **strings_arr;
printf("Please enter a string:\n");
flushall;
gets(STR);
printf("Please enter a letter for a check:\n");
letter = getchar();
strings_arr = Split(STR, letter, &size);
if (size > 0) {
printf("The number of words that starts with the letter '%c' in the string '%s' is: %d\n\n", letter, STR, size);
}
printf("The words are:\n");
for (i = 0; i < size; i++) {
printf("%d . %s\n", i+1,strings_arr[i]);
}
free_mat(strings_arr, size);
return 0;
}
void free_mat(char **mat, int size)
{
int i;
for (i = 0; i < size; i++)
{
free(mat[i]);
}
free(mat);
}
char **Split(char *str, char letter, int *size) {
int rows = 0, i, lengh = 0, j = 0, n = 0, m;
char **strings_array;
if ((str[0] == letter) || (str[0] == letter + 32) || str[0] == letter - 32) {
rows++;
}
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ' ') {
if ((str[i + 1] == letter) || (str[i + 1] == letter + 32) || str[i + 1] == letter - 32) {
rows++;
}
}
}
if (rows == 0) {
printf("There are no words starting with '%c' letter in this string\n\n", letter);
}
i = 0;
strings_array = (char*)malloc(rows * sizeof(char));
if ((str[0] == letter) || (str[0] == letter + 32) || str[0] == letter - 32) {
while (str[i] != ' ' && str[i] != '\0') {
lengh++;
i++;
}
strings_array[j] = (char*)malloc((lengh + 1) * sizeof(char));
for (n = 0; n < lengh; n++) {
strings_array[j][n] = str[n];
}
strings_array[j][n] = '\0';
j++;
}
for (i = 1; str[i] != '\0'; i++) {
if (letter == str[i] || letter == str[i] - 32 || letter == str[i] + 32) {
lengh = 0;
//k = 0;
m = i;
while (str[m] != ' ' && str[m] != '\0') {
lengh++;
m++;
}
strings_array[j] = (char*)malloc(lengh + 1);
for (n = 0; n < lengh; n++) {
strings_array[j][n] = str[i++];
}
strings_array[j][n] = '\0';
j++;
}
}
*size = rows; // sends back the number of words by referance
return strings_array;
}
Thanks!
A breakpoint is something that you manually insert in the code from your IDE or equivalent. It is used for debugging. When you run the code it is intended to stop when it reaches a breakpoint. So just remove the breakpoint and it should work as expected.
Note: Remove only the breakpoint. Not the code on that line.
You mentioned in comments below that you're using Visual Studio 2015. Here is the documentation for breakpoints in that software: https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019
But there are some other things about your code. First of all, use fgets instead of gets. Second, you seem to have posted the wrong version or something, because free_mat will not compile. However, that was easily solved by changing arr to mat.
There's also another error that xing mentioned in the comments. Change strings_array = (char*)malloc(rows * sizeof(char)) to strings_array = malloc(rows * sizeof(*strings_array)). The cast is not necessary, you picked the wrong type for the argument to sizeof and if you pass a dereferenced pointer instead of the type you'll save yourself a lot of problems in the future.
"Exercise 1-23. Write a program to remove all comments from a C program.
Don't forget to handle quoted strings and character constants
properly. C comments do not nest." K&R pg.34
Basically, I have two questions:
1)I'm completely new coding and I wanted to know if I'm at least thinking the problem the right way.
2)The code was built to ignore // till \n or /* till */. But whit the /* comment it always leaves one /.
Input: abc/*comment*/123
Output: abc/123
Input: abc/*123
Output: abc/
#include <stdio.h>
char s[1000]; //Principal array
int countS; //Number of char in array
int deletSingleLineComments(void);
int deletMultiLineComments(void);
int main(void){
int c;
while((c=getchar())!=EOF){
s[countS]=c;
++countS;
}
deletMultiLineComments(); //Function 1
deletSingleLineComments(); //Function 2
printf("\ns[]=\n%s\n\ncountS[]=%d\n",s,countS);
}
//Functions 1
int deletMultiLineComments(void){
char t[1000];
int i=0;
int inComment=0;
int diff=0;
int a,b,c;
while(i<=countS){
t[i]=s[i];
++i;
}
i=0;
while(i<=countS){
if(t[i]=='/' && t[i+1]=='*'){
inComment=1;
}
if(inComment==1){
++diff; //to equilibrate the number
}
if(inComment==0){
s[i-diff]=t[i];
}
if(t[i]=='*' && t[i+1]=='/'){
inComment=0;
}
++i;
}
s[i-diff+1]='\0';
countS=i-diff;
printf("\nt[]=\n%s\n",t);
}
//Function 2
int deletSingleLineComments(void){
int i=0;
char t[1000];
int inComment=0;
int diff=0;
while(i<=countS){
t[i]=s[i];
++i;
}
i=0;
while(i<=countS){
if(t[i] == '/' && t[i+1] == '/'){
inComment=1;
}
if(t[i]=='\n'){
inComment=0;
}
if(inComment==1){
++diff;
}
if(inComment==0){
s[i-diff]=t[i];
}
s[i-diff+1]='\0';
++i;
}
countS=i-diff;
}
Thank you.
while(i<=countS){ t[i]=s[i];... }
Note that character strings are zero based. For example "ABC" has length 3, it starts at zero index, and the last valid index is 2 (not 3). Therefore you should change the condition to i < string_length
while(i < countS){ t[i]=s[i];... }
Also be careful when accessing t[i+1], because while i is valid, i+1 can be out of bound.
if (i < (countS - 1))
if(t[i]=='/' && t[i+1]=='*')
In order to assign one string to another, you can introduce a second variable k, and increment k after each assignment. This method is easier (in my opinion) than using the diff variable and doing additions and subtractions.
In addition, rather than char t[1000];, you can use char *t = malloc(countS); to declare a temporary variable of length countS, then it has to be freed at the end with free(t). If your compiler supports variable length array, you can just put char t[countS].
Example:
char s[1000]; //Principal array
int countS; //Number of char in array
//Functions 1
void deletMultiLineComments(void)
{
char *t = malloc(countS);
int i = 0;
int k = 0;
int inComment = 0;
while (i < countS)
{
t[i] = s[i];
++i;
}
i = 0;
while (i < countS)
{
if (i < countS - 1)
if (t[i] == '/' && t[i + 1] == '*')
{
inComment = 1;
i+=2;
continue;
}
if (inComment == 1)
{
if (i < countS - 1)
if (t[i] == '*' && t[i + 1] == '/')
{
inComment = 0;
i+=2;
continue;
}
}
if (inComment == 0)
{
s[k] = t[i];
k++;
}
++i;
}
free(t);
s[k] = '\0';
countS = k;
printf("mulitline comment removed %s\n", s);
}
//Function 2
void deletSingleLineComments(void)
{
char *t = malloc(countS);
int i = 0;
int k = 0;
int inComment = 0;
while (i < countS)
{
t[i] = s[i];
++i;
}
i = 0;
while (i < countS)
{
if (i < countS - 1)
if (t[i] == '/' && t[i + 1] == '/')
{
inComment = 1;
i += 2;
continue;
}
if (t[i] == '\n')
{
inComment = 0;
}
if (inComment == 0)
{
s[k] = t[i];
k++;
}
i++;
}
free(t);
s[k] = '\0';
countS = k;
printf("single comment removed %s\n", s);
}
int main(void)
{
//get input
scanf("%s", s);
countS = 0;
while (s[countS]) countS++;
deletMultiLineComments(); //Function 1
deletSingleLineComments(); //Function 2
}
I have been stuck on this for a while now. I wrote my program to count word occurrence in an inputted string by the user as well to sort the words alphabetically. My issue is my program runs perfectly only if there are spaces in between the words inputted. For example, if I input "to to," my program will count those two words as two different words due to the comma rather than counting it as one word in "to" as I would like it to. It is that issue for all of my delimiters in the array const char delim[]. How can I fix this issue in my program? I really appreciate any help! My code is down below:
Edit: I took Bob's suggestion to use strchr() and it worked! My only issue is my program outputs the count for delimiters now. I was thinking of possibly writing an if statement when comparing words[i] with words[j] to see if they have the same value. Is that the best approach to it?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
const char delim[] = ", . - !*()&^%$##<> ? []{}\\ / \"";
#define SIZE 1000
int main(){
char string[SIZE], words[SIZE][SIZE], temp[SIZE];
int a = 0, i = 0, j = 0, k = 0, n = 0, count;
int c = 0, cnt[26] = { 0 };
int word = 0;
int x;
printf("Enter your input string:");
fgets(string, SIZE, stdin);
string[strlen(string) - 1] = '\0';
lower(string);
/*extracting each and every string and copying to a different place */
while (string[i] != '\0'){
if (strchr(", . - !*()&^%$##<> ? []{}\\ / \"", string[i]) != NULL){
words[j][k] = '\0';
k = 0;
j++;
}else {
words[j][k++] = string[i];
}
i++;
}
words[j][k] = '\0';
n = j;
printf("Number of occurences of each word unsorted:\n");
i = 0;
/* find the frequency of each word and print the results */
while (i <= n) {
count = 1;
if (i != n) {
for (j = i + 1; j <= n; j++) {
if (strcmp(words[i], words[j]) == 0) {
count++;
for (a = j; a <= n; a++)
strcpy(words[a], words[a + 1]);
n--;
}
}//for
}
//word == strtok(string, delim);
/* count - indicates the frequecy of word[i] */
printf("%s\t%d\n", words[i], count);
i = i + 1;
}//while
printf("Alphabetical Order:\n");
/* sort the words in the given string */
for (i = 0; i < n; i++){
strcpy(temp, words[i]);
for (j = i + 1; j <= n; j++){
if (strcmp(words[i], words[j]) > 0){
strcpy(temp, words[j]);
strcpy(words[j], words[i]);
strcpy(words[i], temp);
}
} //inner for
} //outer for
i = 0;
while (i <= n) {
count = 1;
if (i != n) {
for (j = i + 1; j <= n; j++) {
if (strcmp(words[i], words[j]) == 0) {
count++;
}
}
}
printf("%s\n", words[i]);
i = i + count;
}
}
Strip every word of that delimeter before comparing. Actually you don't even need a list of delimeters because words are 'alpha' other than that it's a delimeter.
Please try this, it works, it is an extract of your own code, a little bit modified, it will give you the count, then you have to write the rest, have fun.
#include <string.h>
#define YES 1
#define NO 0
int main( )
{
char string[1000];
int i = 0;
int j = 0;
int is_this_a_word = 0;
strcpy( string, " to or not ,tobe" );
while ( string[i++] != '\0' )
{
if ( strchr( ", . - !*()&^%$##<> ? []{}\\ / \"", string[i] ) != NULL )
{
is_this_a_word = NO;
}
else
{
if ( ! is_this_a_word )
{
is_this_a_word = YES;
j++;
}
}
}
printf( "I counted %d words", j );
getchar( );
}
My assignment is to allow the user to enter any input and print the occurrences of letters and words, we also have to print out how many one letter, two, three, etc.. letter words are in the string. I have gotten the letter part of my code to work and have revised my word function several times, but still can't get the word finding function to even begin to work. The compiler says the char pointer word is undeclared when it clearly is. Do I have to allocate memory to it and the array of characters?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void findLetters(char *ptr);
void findWords(char *point);
int main()
{
char textStream[100]; //up to 98 characters and '\n\ and '\0'
printf("enter some text\n");
if (fgets(textStream, sizeof (textStream), stdin)) //input up to 99 characters
{
findLetters(textStream);
findWords(textStream);
}
else
{
printf("fgets failed\n");
}
return 0;
}
void findLetters(char *ptr) //find occurences of all letters
{
int upLetters[26];
int loLetters[26];
int i;
int index;
for (i = 0; i < 26; i++) // set array to all zero
{
upLetters[i] = 0;
loLetters[i] = 0;
}
i = 0;
while (ptr[i] != '\0') // loop until prt[i] is '\0'
{
if (ptr[i] >= 'A' && ptr[i] <= 'Z') //stores occurrences of uppercase letters
{
index = ptr[i] - 'A';// subtract 'A' to get index 0-25
upLetters[index]++;//add one
}
if (ptr[i] >= 'a' && ptr[i] <= 'z') //stores occurrences of lowercase letters
{
index = ptr[i] - 'a';//subtract 'a' to get index 0-25
loLetters[index]++;//add one
}
i++;//next character in ptr
}
printf("Number of Occurrences of Uppercase letters\n\n");
for (i = 0; i < 26; i++)//loop through 0 to 25
{
if (upLetters[i] > 0)
{
printf("%c : \t%d\n", (char)(i + 'A'), upLetters[i]);
// add 'A' to go from an index back to a character
}
}
printf("\n");
printf("Number of Occurrences of Lowercase letters\n\n");
for (i = 0; i < 26; i++)
{
if (loLetters[i] > 0)
{
printf("%c : \t%d\n", (char)(i + 'a'), loLetters[i]);
// add 'a' to go back from an index to a character
}
}
printf("\n");
}
void findWords(char *point)
{
int i = 0;
int k = 0;
int count = 0;
int j = 0;
int space = 0;
int c = 0;
char *word[50];
char word1[50][100];
char* delim = "{ } . , ( ) ";
for (i = 0; i< sizeof(point); i++) //counts # of spaces between words
{
if ((point[i] == ' ') || (point[i] == ',') || (point[i] == '.'))
{
space++;
}
}
char *words = strtok(point, delim);
for(;k <= space; k++)
{
word[k] = malloc((words+1) * sizeof(*words));
}
while (words != NULL)
{
printf("%s\n",words);
strcpy(words, word[j++]);
words = strtok(NULL, delim);
}
free(words);
}
This is because you are trying to multiply the pointer position+1 by the size of pointer. Change line 100 to:
word[k] = malloc(strlen(words)+1);
This will solve your compilation problem, but you still have other problems.
You've got a couple of problems in function findWords:
Here,
for (i = 0; i< sizeof(point); i++)
sizeof(point) is the same as sizeof(char*) as point in a char* in the function fincdWords. This is not what you want. Use
for (i = 0; i < strlen(point); i++)
instead. But this might be slow as strlen will be called in every iteration. So I suggest
int len = strlen(point);
for (i = 0; i < len; i++)
The same problem lies here too:
word[k] = malloc((words+1) * sizeof(*words));
It doesn't makes sense what you are trying with (words+1). I think you want
word[k] = malloc( strlen(words) + 1 ); //+1 for the NUL-terminator
You got the arguments all mixed up:
strcpy(words, word[j++]);
You actually wanted
strcpy(word[j++], words);
which copies the contents of words to word[j++].
Here:
free(words);
words was never allocated memory. Since you free a pointer that has not been returned by malloc/calloc/realloc, the code exhibits Undefined Behavior. So, remove that.
You allocated memory for each element of word. So free it using
for(k = 0; k <= space; k++)
{
free(word[k]);
}
Your calculation of the pointer position+1 is wrong. If you want the compilation problem will go away change line 100 to:
word[k] = malloc( 1 + strlen(words));
I got some help earlier fixing up one of the functions I am using in this program, but now I'm at a loss of logic.
I have three purposes and two functions in this program. The first purpose is to print a sentence that the user inputs backwards. The second purpose is to check if any of the words are anagrams with another in the sentence. The third purpose is to check if any one word is a palindrome.
I successfully completed the first purpose. I can print sentences backwards. But now I am unsure of how I should implement my functions to check whether or not any words are anagrams or palindromes.
Here's the code;
/*
* Ch8pp14.c
*
* Created on: Oct 12, 2013
* Author: RivalDog
* Purpose: Reverse a sentence, check for anagrams and palindromes
*/
#include <stdio.h>
#include <ctype.h> //Included ctype for tolower / toupper functions
#define bool int
#define true 1
#define false 0
//Write boolean function that will check if a word is an anagram
bool check_anagram(char a[], char b[])
{
int first[26] = {0}, second[26] = {0}, c = 0;
// Convert arrays into all lower case letters
while(a[c])
{
a[c] = (tolower(a[c]));
c++;
}
c = 0;
while(b[c])
{
b[c] = (tolower(b[c]));
c++;
}
c = 0;
while (a[c] != 0)
{
first[a[c]-'a']++;
c++;
}
c = 0;
while (b[c] != 0)
{
second[b[c]-'a']++;
c++;
}
for (c = 0; c < 26; c++)
{
if (first[c] != second[c])
return false;
}
return true;
}
//Write boolean function that will check if a word is a palindrome
bool palindrome(char a[])
{
int c=0, j, k;
//Convert array into all lower case letters
while (a[c])
{
a[c] = (tolower(a[c]));
c++;
}
c = 0;
j = 0;
k = strlen(a) - 1;
while (j < k)
{
if(a[j++] != a[k--])
return false;
}
return true;
}
int main(void)
{
int i = 0, j = 0, k = 0;
char a[80], terminator;
//Prompt user to enter sentence, store it into an array
printf("Enter a sentence: ");
j = getchar();
while (i < 80)
{
a[i] = j;
++i;
j = getchar();
if (j == '!' || j == '.' || j == '?')
{
terminator = j;
break;
}
else if(j == '\n')
{
break;
}
}
while(a[k])
{
a[k] = (tolower(a[k]));
k++;
}
k = 0;
while(k < i)
{
printf("%c", a[k]);
k++;
}
printf("%c\n", terminator);
//Search backwards through the loop for the start of the last word
//print the word, and then repeat that process for the rest of the words
for(j = i; j >= 0; j--)
{
while(j > -1)
{
if (j == 0)
{
for(k=j;k<i;k++)
{
printf("%c", a[k]);
}
printf("%c", terminator);
break;
}
else if (a[j] != ' ')
--j;
else if (a[j] == ' ')
{
for(k=j+1;k<i;k++)
{
printf("%c", a[k]);
}
printf(" ");
break;
}
}
i = j;
}
//Check if the words are anagrams using previously written function
for( i = 0; i < 80; i++)
{
if (a[i] == ' ')
{
}
}
//Check if the words are palindromes using previously written function
return 0;
}
I was thinking that perhaps I could again search through the array for the words by checking if the element is a space, and if it is, store from where the search started to the space's index-1 in a new array, repeat that process for the entire sentence, and then call my functions on all of the arrays. The issue I am seeing is that I can't really predict how many words a user will input in a sentence... So how can I set up my code to where I can check for anagrams/palindromes?
Thank you everyone!
~RivalDog
Would be better,if you first optimize your code and make it readable by adding comments.Then you can divide the problem in smaller parts like
1.How to count words in a string?
2.How to check whether two words are anagrams?
3.How to check whether a word is palindrome or not?
And these smaller programs you could easily get by Googling. Then your job will be just to integrate these answers. Hope this helps.
To check anagram, no need to calculate number of words and comparing them one by one or whatever you are thinking.
Look at this code. In this code function read_word() is reading word/phrase input using an int array of 26 elements to keep track of how many times each letter has been seen instead of storing the letters itself. Another function equal_array() is to check whether both array a and b (in main) are equal (anagram) or not and return a Boolean value as a result.
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
void read_word(int counts[26]);
bool equal_array(int counts1[26],int counts2[26]);
int main()
{
int a[26] = {0}, b[26] = {0};
printf("Enter first word/phrase: ");
read_word(a);
printf("Enter second word/phrase: ");
read_word(b);
bool flag = equal_array(a,b);
printf("The words/phrase are ");
if(flag)
printf("anagrams");
else
printf("not anagrams");
return 0;
}
void read_word(int counts[26])
{
int ch;
while((ch = getchar()) != '\n')
if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
counts[toupper(ch) - 'A']++;
}
bool equal_array(int counts1[26],int counts2[26])
{
int i = 0;
while(i < 26)
{
if(counts1[i] == counts2[i])
i++;
else
break;
}
return i == 26 ? true : false;
}