Compare tokens with strcmp - c

I need to compare tokens.
I need to know two tokens which are equal.
This is my code. And something is going wrong when comparing- compilator just crashes.
Can you help me to find error?
int main()
{
int i=0;
char* words[200];
char text[200];
printf("Enter one sentence \n ");
gets(text);
char *word = strtok(text, " ");
while(word!=0)
{
words[i++] = strdup(word);
printf("[%s]\n", word);
word=strtok(NULL, " ,.!?");
}
for (k=0; k<199; k++)
{
for (j=k+1; j<200; j++)
{
if (strcmp(words[k],words[j])==0)
{
printf("Equal words are %s",words);
}
else
{
printf("In this sentence aren't equal words");
}
}
}
getch();
return 0;

In your for loops you iterate until 200, not until the max number of entered words (i) is reached.
There is no guarantee which value the elements of an uninitialized array will have at runtime. They might be 0, but also might be any other random numbers. Which means, that doing strcmp with any array element beyond the number of entered words will result in undefined behavior.
Do your nested for-loop like this:
for (k=0; k < i-1; k++)
{
for (j=k+1; j < i; j++)
{
...
}
}

I realize this is an old question, but I found #elgonzo's answer helpful and was able to get your program to compile after a few other changes. I added the libraries, added \n to the print statements, and initialized the variables k and j which may have been part of your problem.
Here's my version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i=0, k=0, j=0;
char* words[200];
char text[200];
printf("Enter one sentence \n ");
gets(text);
char *word = strtok(text, " ");
while(word!=0)
{
words[i++] = strdup(word);
printf("[%s]\n", word);
word=strtok(NULL, " ,.!?");
}
for (k=0; k < i-1; k++)
{
for (j=k+1; j < i; j++)
{
if (strcmp(words[k],words[j])==0)
{
printf("Equal words are %s\n", *words);
}
else
{
printf("In this sentence aren't equal words\n");
}
}
}
return 0;
}

Related

Need some help how to enter a number and count it using array

I have to code in an array that can count an element. For example, if the user enters a 2, 2, 2, 1,1 then the user wants to count the number 2 then the result will be ELEMENT is 2 and FREQUENCY is 3. but I have a problem with the parts of " ENTER THE NUMBER YOU WANT TO BE COUNTED". I use scanf but when I run it I cannot enter any number.
Here's my code:
void frequency()
{
system("cls");
int num;
int count=0;
printf("Enter a number you want to be count: \n ");
scanf("i%", &num);
printf(" ELEMENT | FREQUENCY \n ");
for (i = 0; i<=n; i++)
{
if (a[i]==a[num])
count++;
}
printf(" \n %i ", num);
printf(" \t\t");
printf("%i \n ", count);
getch();
}
Your program requires understanding on two parts:
Get input and split input by delimiter, which can be done by using strtok.
Algorithm for finding the duplicated elements in an array.
#include <stdio.h>
#include <string.h>
int main() {
frequency();
return 0;
}
void frequency() {
char str[100];
printf("Enter a number you want to be count: \n ");
gets(str);
int init_size = strlen(str);
char delim[] = " ";
char *ptr = strtok(str, delim);
char *pch;
int arr[20];
int count = 0;
int ncount, i, j;
int a[count], Freq[count];
while(ptr != NULL) {
/*printf("'%s'\n", ptr);*/
/*Converts the string argument str to an integer (type int)*/
arr[count] = atoi(ptr);
/*strtok accepts two strings - the first one is the string to split, the second one is a string containing all delimiters*/
ptr = strtok(NULL, delim);
/*Initialize frequency value to -1*/
Freq[count] = -1;
count += 1;
}
/*Count the frequency of each element*/
for (i = 0; i < count; i++) {
ncount = 1;
for(j = i + 1; j < count; j++) {
/*Part to perform checking for duplicate elements*/
if(arr[i] == arr[j]) {
ncount++;
/*Make sure not to count frequency of same element again*/
Freq[j] = 0;
}
}
/*If frequency of current element is not counted*/
if(Freq[i] != 0) {
Freq[i] = ncount;
}
}
printf(" ELEMENT | FREQUENCY \n");
printf("-------------------------\n");
for (i = 0; i < count; i++) {
if(Freq[i] != 0) {
printf("\t%d\t\t\t%d\n", arr[i], Freq[i]);
}
}
}
Also, from your code:
You did not define i and n, which is required by your for loop. Also, since your for loop is for (i = 0; i<=n; i++), you have to define the value of n, which is the length of elements inputted by the user, in order to loop through the number of elements you expected.
int i, n, num;
...
...
for (i = 0; i<=num; i++)
Your scanf("i%", &num); should be scanf("%i", &num); instead.
You did not initialize your array a. You should have this line of code before assigning values to your array a. The value 20 can be adjusted by yourself depending on how many inputs are expected. Also, it can be coded in a flexible way instead of hardcoded as 20.
...
int i, num;
int count=0;
int a[20];
...
...
Lastly, it is a good practice to include the function's library before using it. In your case, you should include #include <conio.h> to use the getch() function.

How can I pass a stdin as an argument for my function?

My first program. I would like it if the user enters a word made of letters and then it uses my loop function to output mixed up even and odd characters. Currently I cannot get it to compile. Bonus points if someone can show me how to loop the users input so after it asks the size to make the array, it prompts the user that many times for an "element" or word so that the function can scramble it and output it.
#include <stdio.h>
char transform(char str[]);
int main()
{ //Declare an array and size variable
int size = 0;
char str[size];
printf("How many elements?");
scanf("%d", &size);
printf("Please type an element: ");
//Get input from user
str[0] = scanf("%s", str);
transform(str);
printf("Please type another element: ");
//Get another input from user
str[1] = scanf("%s", str);
transform(str);
//This is the loop function that I programmed
char transform(char str[]);
{
//Loop that prints even characters
for (int i = 0; str[i] != '\0'; i++)
{
if(i % 2 == 0)
{
printf("%c", str[i]);
}
} //Space between even/odd characters
printf(" ");
//Loop that prints odd characters
for (int i = 0; str[i] != '\0'; i++)
{
if(i % 2 != 0)
{
printf("%c", str[i]);
}
}
printf("\n");
return 0;
}
}
#include <stdlib.h>
#include <stdio.h>
char transform(char str[]);
int main()
{ //Declare an array and size variable
int size = 0;
printf("How many elements?");
scanf("%d", &size);
for (int i = 0; i < size; ++i)
{
printf("Please type an element: ");
char str[2048]; //declare a wide buffer to be able to store lots of chars
scanf("%s", str);
transform(str);
}
return 0;
} //end your main here, by putting closing brace
char transform(char str[]) //define transform without semicolon, and outside of main
{ //This is the loop function that I programmed
//Loop that prints even characters
for (int i = 0; str[i] != '\0'; i++)
{
if (i % 2 == 0)
printf("%c", str[i]);
} //Space between even/odd characters
printf(" ");
//Loop that prints odd characters
for (int i = 0; str[i] != '\0'; i++)
{
if (i % 2 != 0)
printf("%c", str[i]);
}
printf("\n");
return 0;
}

Concatenation of first letter alike regardless of upper and lower case in C

i have been figuring out how to combine strings that has first same letter alike regardless of their cases. I have a code that if you input
babe,two,Bird,Tea
the output is always like this
babe,Bird,Tea,two
But the input that I want to see is like this
babeBird,Teatwo
What am i going to add or change in my code in order for me to do that. Here's my code:
#include<stdio.h>
#include<string.h>
#include <ctype.h>
int main()
{
char str1[1000][1000], str[1000], temp[1000];
int n, i, p, j, a;
char *ptr, *ptr1, letter;
printf("Enter how many arrays: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter string %d: ", i+1);
scanf("%s", &str1[i]);
}
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if (tolower((unsigned char)str1[i][0]) == tolower((unsigned char)str1[j][0])
){
strcpy(temp, str1[i]);
strcpy(str1[i], str1[j]);
strcpy(str1[j], temp);
}
if(strcasecmp(str1[i], str1[j])>0)
{
strcpy(temp, str1[i]);
strcpy(str1[i], str1[j]);
strcpy(str1[j], temp);
}
}
}
for(i=0; i<n; i++)
{
if (i != 0)
{
else if (str1[i][0] != letter)
{
printf(",");
}
}
{
printf("%s", str1[i]);
letter = str1[i][0];
}
}
}
The following code which decides whether to print a comma
if (str1[i][0] != letter)
does a case-sensitive comparison.
Change it to something like
if (tolower(str1[i][0]) != tolower(letter))
However, the code contains multiple bugs (too many to point them all out), so not sure it will work. You might want to do debugging.

Trying to remove substring from string in C, keep failing

I know this question has been asked many times before but I simply cannot get my head around what I am doing wrong. Everytime I make some progress I get a new error. The code I am using is really basic because I am a newbie and our professor requires the usage of scanf and gets. This is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
int identify(char[], char[]);
int remove(char[], char[], int);
int scan(choice)
{
while(choice < 0 || choice > 7)
{
printf("Invalid input, choose again\n");
scanf("%d", &choice);
}
return choice;
}
int main()
{
char sentence[MAX_SIZE], word[MAX_SIZE];
int choice, i, j, k, deikths;
printf("Choose one of the following:\n");
printf("1. Give sentence\n");
printf("2. Subtract a word\n");
printf("3. Add a word\n");
printf("4. Count the words\n");
printf("5. Count the sentences\n");
printf("6. Count the characters\n");
printf("7. Is the phrase a palindrome?\n");
printf("0. Exit\n");
scanf("%d", &choice);
if(scan(choice) == 1)
{
printf("Give sentence:\n");
gets(sentence);
gets(sentence);
printf("%s\n", sentence);
}
else(scan(choice) == 2);
{
printf("Give word you want to subtract\n");
gets(word);
printf("%s", word);
deikths = identify(sentence, word);
if(deikths != -1)
{
remove(sentence, word, deikths);
printf("Sentence without word: %s\n", sentence);
}
else
{
printf("Word not found in sentence.\n");
}
}
}
int identify(char sentence[], char word[])
{
int i, j, k;
for(k = 0; word[k] != '\0'; k++);
{
for(i = 0, j = 0; sentence[i] != '\0'; i++)
{
if(sentence[i] == word[j])
{
j++;
}
else
{
j = 0;
}
}
}
if(j == 1)
{
return(i - j);
}
else
{
return -1;
}
}
int remove(char sentence[], char word[], int deikths)
{
int i, k;
for(k = 0; word[k] != '\0'; k++)
{
for(i = deikths; sentence[i] != '\0'; i++)
{
sentence[i] = sentence[i + k + 1];
}
}
}
The error I am getting, is that the remove function has conflicting types. Any help with fixing my code will be greatly appreciated, or even an alternative solution to my problem would bre great.
As established in the comments, the compiler error is generated because remove is already defined in the stdio.h. After changing, the name the code compiles successfully, but still doesn't work as expected.
identify is the function which is meant to find whether a substring exists in a string and return its position. This is very similar to how strstr from the standard library works - I'd suggest having a look at an implementation of that function, to better understand how this is done.
The function you implemented only correctly finds substrings of length 1, at the end of the string. I have highlighted errors in the code below which cause this.
int identify(char sentence[], char word[])
{
int i, j, k;
for(k = 0; word[k] != '\0'; k++); // <- this loops is never actually ran because of the trailing semicolon - this is however a good thing as it is redundant
{
for(i = 0, j = 0; sentence[i] != '\0'; i++)
{
if(sentence[i] == word[j])
{
j++;
}
else
{
j = 0; // <- this makes it so only matches at the end can be found - otherwise, j is just reset back to 0
}
}
}
if(j == 1) // <- this makes it so only matches of length 1 can be found
{
return(i - j); // <- this is only correct if the match is at the end of the sentence
}
else
{
return -1;
}
}
strremove is inefficient due to the nested loops and the range of characters copied needs to be shortened - right now data is access beyond the end of the array.
int strremove(char sentence[], char word[], int deikths)
{
int i, k;
for(k = 0; word[k] != '\0'; k++) // <- this loop is redundant
{
for(i = deikths; sentence[i] != '\0'; i++) // <- you need to add range checking to make sure sentence[i+k+1] doesn't go beyond the end of the string
{
sentence[i] = sentence[i + k + 1];
}
}
}
I will leave the problems in main as an exercise to you - this is an assignment after all.

Reverse string(Swaping)

I need help about this exam.I need to reverse the input string.
int main(void)
{
char str[30];
int strlen; int i=0; int count=0;int temp;int j;
printf("Enter the string \n");
gets(str);
while(str[i]!='\0')
{
i++;
count++;
}
strlen=count;
printf("The length of the string:%d\n", strlen);
i=0;
j=strlen;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
printf("Reverse string :%s",str);
return 0;
}
The problem is that at the end its not show me the string.
It shows me :
"Reverse string :"
and that is, no reverse string. Where is my mistake?
In your code you are doing
j=strlen;
j points to the last index of the string which will be '\0'
And later in the loop you set str[i]=str[j];
Therefore, the first index will be \0
Printing str will display nothing
In order to make the code correct set j=strlen - 1;
Try this:-
As In your code you are assigning j=strlen pointing to \0 or null character change it to j=strlen-1;
char str[30];
int strlen;int i=0;int count=0;int temp;int j;
printf("Enter the string \n");
gets(str);
while(str[i]!='\0')
{
i++;
count++;
}
strlen=count;
printf("The lenht of the string:%d\n",strlen);
i=0;
j=strlen-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
printf("Reverse string :%s",str);
The problem is with the assignment j=strlen;'.
People will suggest you to change it toj=strlen - 1;`
But, I'll recommend to resolve at its root.
while(str[i]!='\0')
{
i++;
count++;
}
After this loop, count will hold value as the index which has a '\0' of the string. Here, you should decrement count and then assign it to strlen. That makes the code more understandable.
With this change, you'll also have to change
printf("The length of the string: %d\n", strlen);
to
printf("The length of the string: %d\n", strlen+1);
as it would print
The length of the string: 4
instead of 5 for "Hello" because of the decrement.
Additionally, strlen is a function once you include string.h header file in your code. To keep it portable, you should always use naming convention which won't have standard, common function names (such as strlen) as a variable name in your code.
A slightly better version (using purely your logic) than your existing code will use one variable less, and will look like this:
#include<stdio.h>
int main(void)
{
//char str[30] = "Hello";
char str[30] = { [0 ... 29] = 0 };
//int str_length = 0;
int i = 0, count = 0, j = 0;
char temp = 0; //This should be a char rather than an int
printf("Enter the string \n");
gets(str);
while(str[i]!='\0')
{
i++;
count++;
}
printf("The length of the string: %d\n", count);
if(count) count--;
//str_length = count;
i = 0;
j = count;
while(i < j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
printf("Reverse string :%s", str);
return 0;
}

Resources