How to toupper char array in c? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
whats wrong here?
I want to check if the char in char array islower, so if it is it should be changed in uppercase.
#include <stdio.h>
int main(int argc, char *argv[]) {
char arr[100];
scanf("%s",&arr);
for(int i=0;i<sizeof(arr);i++){
if(int islower(arr[i])){
arr[i] = toupper(arr[i]);
}
}
printf("%s",arr);
return 0;
}

To measure the length of a string properly, use strlen, not sizeof
for(int i=0;i<strlen(arr);i++){ // Don't use sizeof on this line
Here's a simpler version:
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
char arr[100];
scanf("%s", arr);
for(int i=0;i<strlen(arr);i++){
arr[i] = toupper(arr[i]);
}
printf("%s",arr);
return 0;
}
Or even:
#include <stdio.h>
#include <ctype.h>
int main(void) {
char arr[100];
scanf("%s", arr);
for(char* c=arr; *c=toupper(*c); ++c) ;
printf("%s",arr);
return 0;
}

You are missing an include #include <ctype.h>
Also you don't need your if statement. toupper takes care of that internally (if you really want to keep islower remove the int in your if statement).

Add the header that declares islower and toupper.
#include <ctype.h>
In addiition,
if(int islower(arr[i])){
is not right. Remove the int.
if(islower(arr[i])){

whats wrong here?
The line: if(int islower(arr[i])){ fails compile for bad expression.
Change to: if(islower(arr[i])){
And in this line in your code may be looking beyond where it should:
for(int i=0;i<sizeof(arr);i++){
as you may be looking at space past the string terminator:
|s|t|r|i|n|g|\0|<unknown contents here, part of your legal memory, but are not part of the string>
it should be:
int len = strlen(arr);
for(int i=0;i<len;i++){

Related

How can I convert char[] array to char* [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to convert my char[] array to char* string in C programing language. Here is what I did. But I can't reach the solution . Please help me. Here is my code:
#include <stdio.h>
void fnk(char *chr){
printf("%s",chr);
}
int main(){
char charfoo[100];
int i=50;
gets(charfoo);
for(int j=0;j<i;j++){
fnk((char*) charfoo[j]);
}
}
I think you mean the following
fnk( &charfoo[i]);
Take into account that it would be better to write the loop the following way
for( int j=0; charfoo[j]; j++ ) {
fnk( &charfoo[j]);
}
Also function gets is unsafe and is not supported any more by the C Standard. Instead use function fgets For example
fgets( charfoo, sizeof( charfoo ), stdin );
In this case the loop can look like
for( int j=0; charfoo[j] != '\0' && charfoo[j] != '\n'; j++ ) {
fnk( &charfoo[j]);
}
If you want to output just one character in the function then the function should be defined like
void fnk(char chr){
printf("%c",chr);
}
and called like
fnk( charfoo[j]);
If we switch from gets() to fgets() as Vlad from Moscow suggests, then the return value from fgets() is of the same type as the argument to fnk() so we can just pass the result along:
#include <stdio.h>
void fnk(char *string) {
printf("%s", string);
}
int main() {
char charfoo[100];
fnk(fgets(charfoo, sizeof(charfoo), stdin));
return 0;
}
The next step in sophistication would be to save the result of fgets() to a char * pointer and test if it's NULL or not before passing it along (or not) to fnk().
If you want an actual char * copy of the array charfoo[] to exist in memory and not simply pass off char[] arrays as char * pointers, you could do the following which throws in lots of error checking too:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fnk(char *string) {
printf("%s", string);
}
int main() {
char charfoo[100];
char *input = fgets(charfoo, sizeof(charfoo), stdin);
if (input != NULL) {
char *string = strdup(input);
if (string != NULL) {
fnk(string);
free(string);
}
}
return 0;
}
But in all cases, it seems like you're making things more difficult than they need to be.
Calling fnk with i would just be the same call 50 time -- assuming you want to use j instead the code would look like this
#include <stdio.h>
void fnk(char *chr){
printf("%s",chr);
}
int main(){
char charfoo[100];
int i=50;
gets(charfoo);
for(int j=0;j<i;j++){
fnk(charfoo + j);
}
}
be aware that gets could cause buffer overflow if the input is longer than the charfoo (i.e. 99 bytes + newline + null) -- but that is a different story

Characters not showing up [C] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm making a fun little text-based game for fun and for some reason some text aren't showing up for the username.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
/* Forward declarations -- Prototypes */
void askCharacterName(char *name);
void printMainMessage(char* name);
int main(char* username) {
askCharacterName(username);
char* temp;
temp = &username;
printMainMessage(temp);
return (0);
}
void askCharacterName(char *name) {
char username[20];
printf("What is your desired username?");
scanf("%s", &username);
return *username;
}
void printMainMessage(char *name) {
printf("Hello %s. Welcome to Lamescape!\n", name);
}
Here is my output:
Welcome []. Welcome to Lamescape!
A couple of things:
Your main function should have a different signature.
Consider allocating your memory at a higher level so it is available in lower
levels of the program.
When modifying buffers in c, always pass their size too.
Functions with void return values are not expected to return anything.
After fixing these problems your errors went away.
#include <stdio.h>
#include <stdlib.h>
/* Forward declarations -- Prototypes */
void askCharacterName(char *name, unsigned size);
void printMainMessage(char* name, unsigned size);
int main()
{
char namebuffer[100];
askCharacterName(namebuffer, 100);
printMainMessage(namebuffer, 100);
return 0;
}
void askCharacterName(char *name, unsigned size)
{
printf("What is your desired username?");
scanf("%s", name);
}
void printMainMessage(char *name, unsigned size)
{
printf("Hello %s. Welcome to Lamescape!\n", name);
}
Passing the size has no immediate effect here. I leave it up to you to figure out how to ensure that the buffer is never used beyond its bounds.
The username in askCharacterName has its memory content at the program stack.
void askCharacterName(char *name) {
char username[20];
printf("What is your desired username?");
scanf("%s", &username);
return *username;
}
Allocating the space in heap for username in main looks to be what you are looking for.

C pointer to string returning warning [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following code:
int ver(unsigned char** v) {
unsigned char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char* argv[]) {
unsigned char s[10];
ver(s);
printf("version = %s", s);
return 0;
}
I get the following warning that the pointer differs in signedness. Can you please advise.
Change the declaration of ver to:
int ver(unsigned char * v)
I.e. get rid of one of the * characters.
Warning about signedness is because strcpy and printf("%s") expect a char*, but you are passing unsigned char*
Also int ver(unsigned char ** v) should be int ver(char* v)
Actually the problem is this :
int ver(unsigned char ** v)
You have to change the function to that :
int ver(unsigned char * v)
You need to do 2 fixes:
1) Follow what others told you about the method declaration, changing to "int ver(unsigned char* v) {" ;
2) Just include <string.h> at your code.
Example:
#include <stdio.h>
#include <string.h>
int ver(unsigned char* v) {
unsigned char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char* argv[]) {
unsigned char s[10];
ver(s);
printf("version = %s", s);
return 0;
}
The cause of the warning:
strcpy() expects a signed char*, and is called with a unsigned char*.
But there should be other warnings as well:
Function ver() expects a pointer to a pointer:
int ver(unsigned char ** v){
ver(s) sends a single pointer to char:
unsigned char s[10];
ver(s);
To solve, you can change the code to:
int ver(char *v) {
char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char *argv[]){
char s[10];
ver(s);
printf("version = %s", s);
return 0;
}

Array with int and char in C

I need to put 3 strings on an array[3][3].
I tried to do it with pointers, but I only receive a single character.
#include <stdio.h>
int array[3][3]
char thing[5] = "thing";
main()
{
thing = array[0][0];
printf("%s", array[0][0];
}
Try this. With due respect your code absolutely incorrect and need many changes. You need to update your programming skills too.
#include <stdio.h>
#include <string.h>
char array[3][6]={0};
char *thing = "this";
main()
{
strcpy(array[0],thing);
printf("%s\n", array[0]);
}

Struggling with strings. What is wrong with my function?

I am trying to write a small function to trim left spaces from a string, but I cannot get it right. In this version, I get the following error:
bus error: 10
Could anyone please explain to me what I am doing wrong? I am not looking so much for an alternative piece of code, but would like to understand the errors in my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void trim_string(char *);
int main(int argc, char *argv[]) {
char *temp = " I struggle with strings in C.\n";
trim_string(temp);
printf("%s", temp);
return 0;
}
void trim_string(char *string) {
char *string_trimmed = "";
int i=0, j=0;
while (isblank(string[i])) {
i++;
}
while (string[i] != '\0') {
string_trimmed[j] = string[i];
i++;
j++;
}
string_trimmed[j] = '\0';
strcpy(string, string_trimmed);
}
I have now found a workaround solution, shown below. But I am still not very clear about what I did wrong in the first place:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LENGTH 100
void trim_string(char [MAX_LENGTH]);
int main(int argc, char *argv[]) {
char temp[MAX_LENGTH] = " I struggle with strings in C.\n";
trim_string(temp);
printf("%s", temp);
return 0;
}
void trim_string(char string[MAX_LENGTH]) {
char string_trimmed[MAX_LENGTH];
int i=0, j=0;
while (isblank(string[i])) {
i++;
}
while (string[i] != '\0') {
string_trimmed[j] = string[i];
i++;
j++;
}
string_trimmed[j] = '\0';
printf("c\n");
strcpy(string, string_trimmed);
}
Both string and string_trimmed point to string literals, here in main:
char *temp = " I struggle with strings in C.\n";
^
|
This is a string literal
temp points to a string literal and the standard says you are not allowed to modify them.
In the function trim_string you are modifying a them which is undefined behavior of which a bus error is one possible result, although anything can happen.
string_trimmed either needs to be an array like this:
char string_trimmed[n] ;
where n is the size of your input using strlen(string) would probably make sense or dynamically allocated via malloc which you would need to free at the end of your function. The same things goes for your input from main, this would work as a substitute:
char temp[] = " I struggle with strings in C.\n";
For completeness sake, the draft C99 standard section 6.4.5 String literals paragraph 6 says (emphasis mine):
It is unspecified whether these arrays are distinct provided their elements have the
appropriate values. If the program attempts to modify such an array, the behavior is
undefined.

Resources