Solved

string functions

Posted on 2006-11-04
3
303 Views
Last Modified: 2010-04-15
i want to write a function void shift(char *) that looks at the first character of its argument and converts the other characters, to be the same case, that is, if the first character is uppercase, it shifts the rest to uppercase; if the first character is lowercase, it shifts the rest to lowercase, and otherwise, it does nothing.
0
Comment
Question by:krampovpi
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17874562
Do you have a question about doing that?
the functions isupper, islower, toupper and tolower may be of interest.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 17875282

void SameCase (char *string)
{
  if (isupper (string)
    // Convert the entire string to upper case
  else if (islower (string))
    // convert the entire string to lower case
}


That's about it.

Kent
0
 
LVL 23

Accepted Solution

by:
Mysidia earned 250 total points
ID: 17875592
That's not right...  string is a pointer to the first character position,
therefore you need to dereference it to access the first character position of the string.

void SameCase (char *string)
{
   if (!isascii(*string)) {
         return;
   }

  if (isupper (*string)
    // Convert the entire string to upper case
  else if (islower (*string))
    // convert the entire string to lower case
}


You can use  toupper/tolower to get the lowercase/uppercase version of any character in the string.

Be sure to check isascii() first;  the islower/isupper/toupper/tolower and many other character functions
require that the argument either be EOF or a valid  unsigned char.

Meaning if the unsigned value of the character is 128 or higher, or the signed value is -1 or less and not EOF,
you need to either not call isupper/islower, OR you need to cast to an unsigned char.

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now