Solved

string functions

Posted on 2006-11-04
3
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Kent Olsen
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 and use switch statements in the C programming language.

749 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