How would you convert alpha characters to n string to uppercase in C? [closed]

…衆ロ難τιáo~ 提交于 2019-12-02 13:39:53

It seems that OP wants to convert a string to upper case. Now here somehow OP messed up the assignment part. The code can be boiled down to assigning uppercase characters to the string passed.

But here, the code is doing something that compiler complains about. invalid conversion from 'const char*' to 'char'. Well we are trying to assign an char* to a char so it complained. The whole code can be simplified though with few thoughts. We will write a module that will convert a string to all uppercase. That's it. Then we will test it with "hello" or some string.

#include<stdio.h>
#include<ctype.h>
#include<string.h>
void toupperString(char s[])
{
  size_t len = strlen(s), i = 0;

  while(i < len) 
   {
    s[i] = toupper((unsigned char) s[i]);
    i++;
   }
}

int main(void) {
  char s[100];
  strcpy(s,"hello"); // char s[100]="hello" also works;
  toupperString(s);
  printf("%s\n",s );
  return 0;
} 

Use of your putchar was right. So this will work with added feature that it will now go on printing too. (You decide if it's feature - because simply the use will not be that useful other than printing it - always printing whatever is given to the toupperString is not a good idea).

void toupperString(char s[])
{
  size_t len = strlen(s), i = 0;

  while(i < len) 
   {
    s[i] = toupper((unsigned char) putchar(s[i]));
    i++;
   }
}

You have some suspicious use in the function also. For example the return value of it. Why returning the \0 character? Is it needed? If the answer is no then just make it void return type denoting you are not returning anything. Also as array decays into pointer the char array will be changed and you don't need to do anything to return it. As you are basically modifying the original array of characters.

Well we can write it more concisely like this as

char* toupperString(char *s)
{
    char *p = s;
    while( *s) 
    {
        *s= toupper((unsigned char) *s);
        ++s;
    }
    return p;
}

Your current function has the basic premise of what you're trying to achieve already, but suffers from a number of problems.

Firstly, calling it toupper is no doubt giving you headaches because you're implementing the function using the pre-existing toupper function. Your function needs to take a string and then return the modified string, but the existing toupper is for taking and return a single char. Notionally, since you're modifying the passed in string, you don't need to return anything, but it can't hurt to return it.

Secondly you're calling putchar for no readily apparent reason. You can leave that out and if required, print the updated string in later parts of your code.

Thirdly, you're returning s[i] which is just a char and given that i has been incremented the length of the string would be pointing at the terminating NUL character.

You also have the spurious line s[50] = "hello"; which is setting the 51st character of the passed in string to some value, which won't be any of the characters in "hello", but instead will be one of the bytes of the memory location of the string "hello". If you're attempting to declare s to be "hello", you should do that in the calling function which I've included as an example.

int main(void)
{
  char s[]="hello";
  make_string_upper(s);
  printf("%s\n",s);
}

char *make_string_upper(char *s)
{
  int i = 0;
  int len = strlen(s);

  while(i < len) //converting to upper until the length is reached.
   {
    s[i] = toupper(s[i]);
    i++;
   }
   return s;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!