jurepure123
jurepure123

Reputation: 57

I'm having a problem with using pointers for strings

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
char slova(char a);
int main()
{
    char a[100];
    fgets(a,sizeof(a),stdin);
    char slova(a);
    return 0;
}
char slova(char a)
{
    int i,x=0,y=0,n;
    n=strlen(a);
    for(i=0;i<n;i++);
    {
        if(*(a+i)>='A' && *(a+i)<='Z') x++;
        if(*(a+i)>='a' && *(a+i)<='z') y++;
    }
    if(y>x) return -1;
    if(y<x) return 1;
    if(x==y || (x==0 && y==0)) return 0;
    
}

The task is to return -1 in case the string has more lowercase letters, to return 1 if it has more uppercase and to return 0 if it has 0 letters or equal number of uppercase and lowercase. I'm dealing with this type of a task for the first time and I have loads of mistakes and not sure how to fix them. Here are the errors.

11  14  C:\Users\x\Documents\asgsgg.cpp [Error] invalid conversion from 'char*' to 'char' [-fpermissive]
17  12  C:\Users\x\Documents\asgsgg.cpp [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]
20  11  C:\Users\x\Documents\asgsgg.cpp [Error] invalid type argument of unary '*' (have 'int')

The last one appears 2 times in line 20 and 2 times in line 21.

Upvotes: 0

Views: 66

Answers (1)

Barmar
Barmar

Reputation: 781068

You need to call the slova() function, not just declare it, and do something with the result, e.g. print the result.

The argument should be char *a, not char a, so it's a pointer to the string, not a single character.

You should use the functions isupper() and islower() rather than checking the character ranges yourself.

There's no need to check for (x==0 && y==0); if they're both 0 then x == y will be true. You can also use a series of else if followed by else at the end.

You have an extra ; at the end of the for(i=0;i<n;i++) line, so the code block after it is not in the loop.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int slova(char *a);
int main(void)
{
    char a[100];
    fgets(a,sizeof(a),stdin);
    printf("Result: %d\n", slova(a));
    return 0;
}

int slova(char *a)
{
    int i,x=0,y=0,n;
    n=strlen(a);
    for(i=0;i<n;i++)
    {
        if(isupper(*(a+i))) x++;
        if(islower(*(a+i))) y++;
    }
    if(y>x) return -1;
    else if(y<x) return 1;
    else return 0;
}

Upvotes: 1

Related Questions