+5

strupr in C

I was using a string manipution function that uppercase the provided string //strupr(); But for unknown reasons I can't use pointer based string 🤔 like char *="all clear"; however char array works like char[10]="all clear"; And when I combine both the methods it shows no output on sololearn ide. https://code.sololearn.com/cz88Y6DaHLEI/?ref=app Please help me to understand this . I used cxxdroid and c4droid also they give errors by both the methods (char * and char [10] ) saying that strupr not defined 🤔 Subquestions : Well in C we don't have string data type so we use char array or char pointers . So... should I even use char pointers for strings ? Because this is deprecated in C++ as per my knowledge. Edit: < 12 sep 2019 code link removed adding code below > #include<stdio.h> #include<string.h> int main() { char s1[10]="All clear"; printf("%s",strupr(s1)); /* char *s2="All clear"; printf("%s",strupr(s2)); */ //uncomment above statements . return 0; }

8/19/2019 10:31:11 AM

🇮🇳Omkar🕉

5 Answers

New Answer

+4

devanille very good solution 👌 Like it. //just don't like that I'll need to use loops. Pointer based strings work with other standard string functions . But strupr isn't standard one (lately understood it's actually deprecated) Thank you so much.

+3

I'm not to sure, but I think it has something to do with the fact that string literals are immutable. Maybe, the strupr function modifies the string directly, but since a string literal is immutable, doing so will cause a segmentation error (no output in the playground). If I'm not mistaken, strupr is part of POSIX library, so not all compilers can support it. And, it's probably better to use string class in C++, but personally, I still use something like C-style string (char pointer and other things) because it's simpler for me, and it has become some sort of a habit

+3

Android or Linux C compilers will give you errors because 'strupr' is a non-standard function from Microsoft. It works on SL because SL uses a Windows server. I think there is no problem with the first string because you are passing the entire string to the strupr function, while in the second case, you are pasing a pointer (reference) to a string. One possible solution: https://code.sololearn.com/cIF38FlhK1DD/?ref=app

+3

Just a bit of advice, if you assign a string literal to a char pointer, like this one char* str = "Some_String" It's better (almost mandatory) to use const specifier const char* str = "Some_String" Like I said, something like that is actually immutable, some compilers will allow the absence of const specifier, but you will get segmentation fault anyway if you try to modify it. With the const specifier, you will get a compilation error instead of runtime error if you try to modify it or pass it to a function that will modify it

+3

🇮🇳Omkar🕉 You're welcome. Thanks to you for your kindly words. And thanks Agent_I for the advice. Btw, 'strupr' uses a loop internally: "The strupr() function replaces the string s with uppercase characters, by invoking the toupper function for each character in the string". https://users.pja.edu.pl/~jms/qnx/help/watcom/clibref/src/strupr.html