0

Is it possible to use one string function inside another string function. See below.... strcmp(string1, strupr(string2)); Here I have used strupr() function to make the all characters of string2 in uppercase then It will use to compare with string1 by strcmp() function. Below is my program...

#include <stdio.h>
#include <string.h>
int main()
{
    char str[41], name[43];
    gets(str);
    gets(name);
    if (strcmp(strupr(str), name) == 0)
        printf("\nwow it works.");
    return 0;
}

Following is the error shown by compilor..

use of undeclared identifier 'strupr'; did you mean 'strdup'?
Suraj
  • 91
  • 1
  • 7
  • 3
    `is it possible to use one string function` It is possible. It is however not possible to use functions that do not exist, like `strupr`. – KamilCuk May 19 '21 at 05:16
  • 2
    Is `strupr` something you wrote yourself? Please show it. Regardless, this is not really to do with string functions per se. It's just basic C. If the inner function returns a value that is compatible with the parameter type required by the outer function then it will work. – kaylum May 19 '21 at 05:17
  • 4
    Even for simple examples like this, never ***ever*** use a [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) function like `gets`. Use [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude May 19 '21 at 05:18
  • 1
    Also please make it a habit to write *trailing* newlines in your `printf` calls. When you run your program in a terminal that will write the line you just printed. Otherwise with leading newline it will write the *previous* line (due to how the `stdout` buffering works), as well as the last output will be seen as part of the terminal prompt. – Some programmer dude May 19 '21 at 05:20
  • 1
    @Someprogrammerdude programmer dude Ok i will use fgets() from now. – Suraj May 19 '21 at 05:21
  • @kaylum Then how can I convert the string into uppercast. – Suraj May 19 '21 at 05:24
  • 1
    There's no built-in function for converting a string case in C. There's `toupper()` for converting a single character. You can loop over all the characters in the string and call `toupper()` to convert each one. – Barmar May 19 '21 at 05:30
  • @Barmar I have used it because it's given in book (Let us C). – Suraj May 19 '21 at 05:32
  • @Suraj Just subtract 32 from every character, ie. a - z. Don't touch other characters though. Loop over `str`, then do; `if (str[i] >= 97 && str[i] <= 122) {str[i] -= 32;}`. It's because the ASCII values of uppercase letters in 32 less than of lowercase letters. – Shambhav May 19 '21 at 05:34
  • 1
    Please show us how `strupr` is defined. We can't see your book and it matters how the function is written. If it returns the original string after modifying the input string then you can use it nested inside `strcmp`. Otherwise not. – kaylum May 19 '21 at 05:34
  • 2
    @ShambhavGautam That is terrible advice. Why not use the `toupper` function when it exists and works with the used encoding? ASCII isn't the only encoding out there. – Some programmer dude May 19 '21 at 05:35
  • @Someprogrammerdude Just trying to explain the logic, because someone else had already said to use `toupper`. – Shambhav May 19 '21 at 05:36
  • @Suraj `use of undeclared identifier 'strupr'; did you mean 'strdup'?` This clearly says that `strtoupr` is undeclared. Your book probably has made `strupr` itself and you forgot to copy it over to your code. Check your book again. – Shambhav May 19 '21 at 05:39
  • The book probably has a library of functions like that. It's not a standard function. – Barmar May 19 '21 at 05:40
  • @ShambhavGautam Please go through this link. https://www.geeksforgeeks.org/strupr-function-in-c/ – Suraj May 19 '21 at 05:44
  • 2
    @Suraj Read the note at the end: **This is a non-standard function that works only with older versions of Microsoft C.** – Barmar May 19 '21 at 05:47
  • 1
    Just because it's on the internet doesn't mean the info is of high quality. Multiple people have already told you that `strupr` is not a standard function. Throw that reference away and never look at it again. – kaylum May 19 '21 at 05:49
  • @Barmar Yes Got it. In my Clang Compilor. strupr() is not working in any case. – Suraj May 19 '21 at 05:49
  • @Suraj As Barmar has already pointed out, it's a non standard Microsoft thing. Microsoft is very non standard. Beware and always look at the notes. You should trust the compilers the most, second is actual humans and third is online tutorials. Always remeber this. – Shambhav May 19 '21 at 05:50
  • Got it. Thanks a lot everyone – Suraj May 19 '21 at 05:52
  • @ShambhavGautam Got it. Thanks – Suraj May 19 '21 at 05:57

2 Answers2

2

To condense the comments into an answer:

As long as the return type of the inner function matches (or is compatible with) the argument type of the outer function, this will work.

A simple example, which will print the number 3.

int add1(int a) {
    return a + 1;
}

int main(void) {
    printf("%d\n", add1(add1(add1(0))));
}

So as long as your strupr function returns a char * (or const char *, etc.) it will work as the function prototype for strcmp is:

int strcmp(const char *s1, const char *s2);

To create a basic function to uppercase an entire string, you simply loop through the string character by character and use the toupper function on each character. There are several ways to implement this loop.

Here is one such way:

char *upcase(char *s) {
    for (char *p = s; p && *p; p++)
        *p = toupper(*p);
    return s;
}

Note that this is a destructive function, which alters the contents of the string passed to it. It will not work with static strings (e.g., const char *foo = "abcdefg";).

Oka
  • 23,367
  • 6
  • 42
  • 53
0

Since your aim seems to compare the two strings in a case insensitive fashion, you should use stricmp:

if (stricmp(str, name) == 0)
        printf("\nwow it works.");

It has the additional benefits of not either changing your original string or allocating a new string you will have to free().

Vincent Fourmond
  • 3,038
  • 1
  • 22
  • 24