I'm modifying a 10-year old open-source utility written in C, even though I'm a PHP programmer, so the whole pointer business for working with strings is giving me a headache. Can anyone advise on why this string conversion snippet is not converting case?

#include <ctype.h>

int set_keysig (s, ks, init)
char s[];
struct KEYSTR *ks;
int init;
{
  int  sf, ok, l, rc;
  char w[81], *c, *r;
  int ktype, add_pitch, root, root_acc;

  c = s;
       /* ... omitting irrelevant, working code here ... */

 /* this is where the word w is supposed to be extracted from the line of text c* and also converted to lowercase. The word gets extracted correctly in all cases, but it does not get converted to lowercase - each letter in the word is mysteriously remaining in its original case. */
  r = w;
 *r++ = *c++;
 while (*c && (*c != ' ') && (*c != '+') && (*c != '-')) 
      *r++ = tolower(*c++);
 *r ='\0';
 /* the next line is how I'm checking whether the above lines worked */
 if (verbose>=20) printf ("word: <%s>\n", w);
}

More context: I'm now compiling this code using gcc 4.2 in XCode 3.1 on an Intel Mac. This code used to work fine when I previously compiled it years ago using gcc 3 in XCode 2 on either a PowerPC Mac or an Intel Mac.

Any ideas?

Recommended Answers

All 6 Replies

#
/* ... omitting irrelevant, working code here ... */
#

well, gonna be hard to tell whats going on, because that code is definitely not irrelevant, and i can't be sure if it's necessarily working.

specifically, i dont see what 'r' and 'c' are pointing to. is the memory being allocated? for all i know they could be uninitialized pointers. and what is 'verbose'?

This source code is plagued with syntax errors. printf ("word: <%s>\n", w); requires the header file stdio.h

int set_keysig (type s, type ks, type init);

Prototype missing ; and type, where type is any data type like int, float, structure, pointer, etc. char s[x]; length of string needs to be declared.

commented: I'll give you a cookie for trying. But sorry, it's completely valid C89; that's just an old form of a function definition. :P +18

>Any ideas?
That source code is making an easy job extra complicated.
All that is needed.

char *dress_down(char *s)
{
	while (*s) {
		*s = tolower(*s);
		++s;
	}
	return s;
}

Assuming a non-read-only string named text.
You'll call it, and it will convert only the characters that has an equivalent in lower case.

printf("%s\n", dress_down(text));

This is old school indeed:

int set_keysig (s, ks, init)
  char s[];
  struct KEYSTR *ks;
  int init;
{
   /* function body */
}

Should be updated to:

int set_keysig (char s[], struct KEYSTR *ks, int init)
{
   /* function body */
}

Other than that (and the missing stdio.h header and the fact that you're not returning anything), your posted code has no errors. The lowercasing code should set all but the first character of each word to lowercase. (This is assuming that the "working code" really does "work" ... which seems unlikely given your results.)

commented: Thank you for correcting me. +15

This is old school indeed:

Yes, before my time with the language. And even when I am aware of the difference in defining the function in the old way, I completely missed.

Thank you! The key was your observation that all except the first letter of the word would be converted to lowercase. The data this routine normally encounters is either initial-capitalized or all lowercase, so it didn't occur to me that the problem only related to the initial letter.

Therefore I simply had to comment out one line of code in the crucial part of the snippet I originally posted:

r = w;
/*  *r++ = *c++; */
     while (*c && (*c != ' ') && (*c != '+') && (*c != '-')) 
        *r++ = tolower(*c++);
    *r ='\0';

And yes, you were the only one to realize that, as I originally posted, there are no syntax errors in this code - this is all code that thousands of the people around the world have been using since the 1990s, and it still compiles today under gcc 4.2 with no errors. Apparently the authors were oldtimers in writing C, but nevertheless it still functions fine. I just don't understand why this initial-character problem cropped up now and not before. Anyway, problem solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.