Click here to Skip to main content
15,878,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The task is to convert a string from lowercase to uppercase without using array notation?

The hint I was given was "modify the string pointer itself", I thought you couldn't modify a string pointer. I'm not sure how to work the while loop.

Any help would be great.

What I have tried:

int main(void)
{

  uppercase_no_array("reverse");

  return(0);
}

void uppercase_no_array(char* input)
{

  char* copy = malloc(sizeof(char) * strlen(input));
  strcpy(copy, input);

  printf("%s", copy);

  while(copy != '\0')
  {

   
  }
return;

}
Posted
Updated 12-Sep-17 2:38am
Comments
Afzaal Ahmad Zeeshan 12-Sep-17 8:07am    
You're almost there, just add the statement inside that while loop and check if the character falls in lower case ASCII, and change it to upper case ASCII.

When you allocate the Memory, copy points at the first character in the space. You then use that to copy the input to an output area - you don't really need to do that at all, just use the two pointers you already have: copy and input
char* uppercase_no_array(char* input)
{
  char* mem = malloc(sizeof(char) * (strlen(input) + 1));
  char* copy = mem;
  char c;
  do
  {
  c = *input++;
  ... uppercase it here ...
  *copy++ = c;
  } while(c != '\0')
  return mem;
}
 
Share this answer
 
v2
Comments
Jochen Arndt 12-Sep-17 8:17am    
You forgot to allocate space for the NULL byte.
OriginalGriff 12-Sep-17 8:26am    
I was using his code, but yes, you are right.
Fixed.
OriginalGriff 12-Sep-17 8:27am    
If this wasn't an early student exercise, I'd also worry about deallocation, but ...
Quote:
The hint I was given was "modify the string pointer itself"
From my point of view that means that you can modify the string in-place using a pointer:
C++
char *p;
p = input_string;
while (*p)
{
    /* char_to_upper() has to be implemented */
    *p = char_to_upper(*p);
    p++;
}
 
Share this answer
 
Comments
Klaus-Werner Konrad 13-Sep-17 11:22am    
This would pretty sure lead to a Memory Access Violation; the Argument from main() is a string LITERAL ...
Jochen Arndt 13-Sep-17 11:40am    
No. They are not literals but the argv[] entries point to an allocated buffer. It is common to modify command line arguments in place.

From the C standard (5.1.2.2.1 Program startup):
"177 — The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination."
Klaus-Werner Konrad 25-Sep-17 10:07am    
Huh ...

int main(void)
{
uppercase_no_array("reverse");
return(0);
}

is CLEARLY a literal, not an command line argument

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900