Error: too few arguments to function 'strcmp' - c

I am having a few issues with my code. First: when I try to compile, I get error: too few arguments to function 'strcmp'. I have looked all over and made multiple changes and am still unable to get it to work. Second: when my code does compile (if I remove the strcmp part), it will not complete the count functions correctly. Can anyone please assist? Thank you!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count(char array[], int size);
int stringLen(char array[]);
int convert(char ch);
int value, n;
int main()
{
//char * str;
//char s;
char a[100];
char b[100];
char c[100];
int charCount = stringLen(a);
int lCount = count(a, charCount);
printf("Enter your string: \n");
scanf("%s \n", a);
printf("Enter your string: \n");
scanf("%s \n", b);
printf("Enter your string: \n");
scanf("%s \n", c);
printf("The count is %d, length is %d\n", lCount, charCount);
int i;
for(i = 0; i < charCount; i++)
{
char c = a[i];
printf("Char %s = %d \n", &c, value);
}
n = strcmp(char string1[], char string2[], char string3[]);
printf("The first string in the alphabet is: %d \n", n);
return 0;
}
int stringLen(char array[])
{
char count;
int index;
while(array[index] !=0)
{
count++;
index++;
}
return count;
}
int count(char array[], int size)
{
int count;
int i;
for(i = 0; i < size; i++)
{
if(array[i] == 'a')
{
count ++;
}
else if(array[i] == 'A')
{
count ++;
}
}
return count;
}

This is not right way to use strcmp.
n = strcmp(char string1[], char string2[], char string3[]);
strcmp is used for compararison of string. See doc
int result = strcmp (string1,string2)
If strings are same, function will return 0.

Related

Function in C that counts how many times a character read appears in a string

I'm using CodeBlocks and C as a programming language. I need to make a function that counts how many times a character read appears in a string (also read).
Line 8 error: subscripted value is neither array nor pointer nor vector
#include<stdio.h>
#include<string.h>
int find(char s, char ch, int l)
{
int i, j=0;
for(i=0; i<l; i++)
{
if(strcmp(s[i],ch)==0)
j++;
}
return j;
}
int main()
{
char s[30];
int i,j,l;
char ch;
printf("Enter the string: ");
gets(s);
l = strlen(s)+1;
printf("Enter the character: ");
scanf("%c",&ch);
j=find(s, ch, l);
printf("\n%c occurs %d times",ch,j);
return 0;
}
Your error was that in the function declaration you didn't mention that s was an array. You needed to use s[] and there was no need for strcmp function in the if statement.
#include<stdio.h>
#include<string.h>
int find(char s[], char ch, int l)
{
int i, j=0;
for(i=0; i<l; i++)
{
if(s[i] == ch)
j++;
}
return j;
}
int main()
{
char s[30];
int i,j,l;
char ch;
printf("Enter the string: ");
gets(s);
l = strlen(s)+1;
printf("Enter the character: ");
scanf("%c",&ch);
j=find(s, ch, l);
printf("\n%c occurs %d times",ch,j);
return 0;
}

Finding all suffix starting with a character X

I need to find all suffix starting with a character X. For example, for int suffix (char str [], char c) when the word is ababcd and the letter b it should return:
babcd
bcd
and the number 2.
This is my code:
#include <stdio.h>
#include <string.h>
int main()
{
char c;
char str[128];
int counter=0;
printf ("Please enter charachter and a string \n");
scanf("%c %s",&c,str);
counter = my_suffix(str,c);
printf("The string has %d suffix \n",counter);
return 0;
}
int my_suffix(char str[],char c) {
int counter = 0;
for (int i=0; i < strlen(str); i++)
{
if (str[i] == c)
{ puts(str+i);
counter++;
}
}
return counter;
}
I couldn't find why it's not running,
Thanks!
Your code is fine you should just written following method above int main()
int my_suffix(char str[],char c){...}

How to search for names by letters in a string Array?

Do anybody knows how to search for a name in a string array? If i register the name 'jacob' and search for cob I need to get jacob shown instead of not showing up anything. I don't know if strcmp is the right way to do it. Any ideas?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 20
struct name{
char name[MAX];
};
void getRegister(struct name name[], int *nrNames);
void getSearch(struct name name[], int nrNames);
int readLine(char s[], int length);
int main(){
int run=1;
struct name name[MAX];
int nrNames=0;
while(run){
char choice;
printf("\n (1)Register\n(2)Search\n(3)Quit\n");
scanf(" %c%*c", &choice);
if(choice=='1') getRegister(name, &nrNames);
if(choice=='2') getSearch(name, nrNames);
if(choice=='3') run=0;
}
return 0;
}
void getRegister(struct name name[], int *nrNames){
char input[MAX];
printf("Enter name: ");
readLine(input, MAX);
(*nrNames)++;
}
void getSearch(struct name name[], int nrNames){
int i;
char input[MAX];
printf("Enter name: ");
readLine(input, MAX);
if(i>=0){
printf("Name/s:\n");
for(i=0; i<nrNames;i++){
if(strcmp(input, name[i].name)==0){
printf("\n%s\n",name[i].name);
}
}
}
}
int readLine(char s[], int length){
int ch, i=0;
while(isspace(ch=getchar()));
while(ch!='\n' && ch!=EOF) {
if(i<length) s[i++]=ch;
ch = getchar();
}
s[i]='\0';
return i;
}
Try to search for the match in the array. The code below displays a position for each occurrence of the second array in the first array. It uses naive approach. There are more efficient algorithms like Knuth-Morris-Pratt or Boyer-Moore algorithm.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 20
int main(){
char c;
char name[MAX], search_name[MAX];
int i = 0, j = 0, match = 0, count = 0;
printf("Register name: ");
while ((c = fgetc(stdin)) != '\n') {
if (i < MAX){
name[i++] = c;
}
}
name[i] = '\0';
printf("Search name: ");
i = 0;
while ((c = fgetc(stdin)) != '\n') {
if (i < MAX){
search_name[i++] = c;
}
}
search_name[i] = '\0';
i=-1;
match = 0;
do {
i++;
j = 0;
do {
if (name[i+j] == search_name[j])
match = 1;
else {
match = 0;
break;
}
j++;
} while (search_name[j] != '\0');
if (match)
printf("Match on position %d ", i);
} while (name[i+j] != '\0');
printf("\n");
return 0;
}
I found the soulution myself. Here is the code for those who get stuck as I did.
void searchName(const struct varor reg[], int nrOfGoods){
int i;
char name[20];
printf("Enter name: ");
readLine(name, WORDLENGTH);//gets input
if(i>=0){
printf("\nId.number \t Name \t\t\t Quantity\n");
for(i=0; i<nrOfGoods;i++){
if(strstr(reg[i].name, name)!=NULL){ //this should do the job
printf("%-17d%-24s%-5d\n",reg[i].idnumber,reg[i].name.reg[i].quantity);
}
}
}
}

Cut long strings into something shorter

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 24
void rez(char **c, char *s, int n, int ks);
void rez(char **c, char *s, int n, int ks)
{
int t = 0, j = 0;
char *p;
p = strtok(s," ");
t = t + strlen(p);
if (strlen(p)>N) *(c+j)=(char*)malloc((strlen(p)+1)*sizeof(char));
else *(c+j)=(char*)malloc((N+1)*sizeof(char));
while(p!=NULL)
{
if (t>N)
{
*(*(c+j)+t) = '\0';
t = strlen(p) + 1;
j++;
if (t>N) *(c+j)=(char*)malloc(strlen(p)+1);
else *(c+j)=(char*)malloc(N+1);
}
strcat(*(c+j), p);
c[j][t]=' ';
p = strtok(NULL, " ");
t=t+strlen(p)+1;
}
c[j][t]='\0';
for(j=0; j<ks; j++)
{
printf("\n %s", *(c+j));
}
}
int main(void)
{
FILE *fin;
int n, ks;
char s1[2048], filename[256];
char **c;
printf("Enter the file name->");
scanf("%s", filename);
fin=fopen(filename,"r");
if (!fin)
{
printf ("Error\n");
return -1;
}
while (fscanf(fin, "%[^\n]", s1)==1)
{
fscanf(fin, "%*[ \n]");
printf("\n String: %s \n", s1);
n=strlen(s1);
ks=n/(N-1)+1;
c=(char **)malloc(ks*sizeof(char*));
rez(c, s1, n, ks);
}
fclose(fin);
return 0;
}
This code should cut long strings into some shorter, but it gives "core dumped" in gcc. It doesn't exit from while in void rez().
In my mind, strtok() works incorrectly.
This is wrong:
char r[1]=" ";
because the string literal " " is TWO characters! It is both a space and a NULL-terminator (which is at the end of every string in C. But you explicitly said it should only be a 1 character array.

Bubble sort in C. Output is the same as input

As a school assignment we are writing a bubble sort program in c. The code I wrote works. The only thing is that the output returns the input and doesn't return the swapped input. I'm kinda stuck. No matter what I do i either get an error or nothing changes. Does anybody know what is going wrong? Any help would be highly appreciated!!
#include <stdio.h>
#include <string.h>
#define MAXLENGTH 100
void getString(char *str);
void printResult(char *str);
int greaterThan(char ch1, char ch2);
void swap(char *str, int index1, int index2);
int main(void) {
int len; // length of the entered string
char str[MAXLENGTH]; // input should be no longer than MAXLENGTH
getString(str);
len = strlen(str); // get length of the string, from sting.h
int j;
for (j = 0; j < len-j; j++) {
int i;
for (i = 0; i < len-j; i++){
char ch1 = str[i];
char ch2 = str[i+1];
if (greaterThan(ch1, ch2)) {
swap(str, i, i+1); // swap char 1
swap(str, i, i+1); // swap char 2
}
}
}
printResult(str);
return(0);
}
void getString(char *str) {
printf("Enter the string you would like to sort: ");
scanf("%s",str);
}
void printResult(char *str){
printf("Here is the sorted string: ");
printf("%s",str);
}
int greaterThan(char ch1, char ch2){
return (ch1 > ch2);
}
void swap(char *str, int index1, int index2){
char h = str[index1];
str[index1] = str[index2];
str[index2] = h;
}
Try this:
getString(str);
len = strlen(str); // get length of the string, from sting.h
int j;
for (j = 0; j < len-j; j++) {
int i;
for (i = 0; i < len-j; i++){
char ch1 = str[i];
char ch2 = str[i+1];
if (greaterThan(ch1, ch2)) {
swap(str, i, i+1); // swap char 1
}
}
}
printResult(str);
return(0);
}
Here:
if (greaterThan(ch1, ch2)) {
swap(str, i, i+1); // swap char 1
swap(str, i, i+1); // swap char 2
}
you are swapping twice while you should swap only once.
I was bad the below part of my answer's previous version is not bubble short:
Another problem is that you are using elements next to each other and you are not using i and j indexed ones to compare and swap. So you better should have something like this:
if (greaterThan(str[i], str[j]))
swap(str, i, j);
if (greaterThan(ch1, ch2)) {
swap(str, i, i+1); // swap char 1
swap(str, i, i+1); // swap char 2
}
In this code you are swamping twice.
For Example: str = ['a','b','c'] swap(str,1,2) for first swap the " str " will be ['a','c','b'] and for the second swap " str " will be ['a','b','c']. That's why your Output is the same as input.
You Just Need To Call swap function only once.
if (greaterThan(ch1, ch2)) {
swap(str, i, i+1);
}

Resources