#include <stdio.h>
#include <ctype.h> /*header for standard tolower function*/
#define STRING_LEN 500
char * changeCase(char *);
void stripspaces(char*, char*, char*);
int isinteger(char*);
int isHex(char*);
void strrev(char*);
int main(void)
{
char line[STRING_LEN];
char line1[STRING_LEN];
char *p1 = line;
char *p2 = line;
int ok, ok2;
printf("Enter a string of up to %d characters:\n", STRING_LEN);
while((*p1++ = getchar()) != '\n');
changeCase(line);
puts("resulting string is:\n");
printf("%s", line);
stripspaces(line, p1, p2);
ok = isinteger(line);
if (ok)
{
printf("%s is an integer\n", line);
}
else
{
printf("\n%s is NOT an integer\n", line);
}
ok2 = isHex(line);
if (ok2)
{
printf("\n%s is a hex value \n", line);
}
else
{
printf("\n%s is NOT a hex value\n", line);
}
strrev(line);
printf("string is:");
printf("%s", line);
getch();
getch();
return 0;
}/*end main*/
char * changeCase(char *s){
char *ptr;
ptr = s; /* point to start of string*/
while ( *s != '\0') /*keep going until you reach nul (end of string)*/
{
if(!isdigit(*s))
{
if(isupper(*s))
{
*s = tolower(*s);
}
else if (islower(*s))
{
*s = toupper(*s);
}
}
s++; /*move to the next character*/
}
return ptr; /* return lowercase version of the string*/
}/*end stringToLower*/
void stripspaces (char *s, char *x1, char *x2){
*x1 = '\0';
x1 = s;
while(*x1 != '\0')
{
if(*x1 == ' ')
{
++x1;
continue;
}
else
*x2++ = *x1++;
}
*x2 = '\0';
printf("\nWith the spaces removed, the string is now:\n%s\n", s);
}
int isinteger(char *s) /* s is a string */
{ /* function returns 1(true) or 0(false) */
if(*s == '+' || '-' || ' '){
s++;
}
while (*s != '\0')
{
if (!isdigit(*s))
{
return 0;
}
s++; /* move to next character */
return 1;
}
}
int isHex(char *s)
{
while (*s != '\0'){
if (!isxdigit(*s)){
return 0;
}
else {
return 1;
} s++; }
}
void strrev(char *p)
{
int i,j;
j = strlen(p);
char temp;
for (i = 0; i < j; i++) // subscript i goes halfway
{
temp = p[i]; // swap
p[i] = p[j-1-i]; // opposite
p[j-1–i] = temp; // elements
}
}
this program is coming up with an error in the swap function at “p[j-1–i] = temp;” the error is as follows “stray “\150″ in program” wondering how I go about fixing this error
the function containing the error is supposed to reverse string line in memory so that when you print line after the function has been run the string is in reverse. not sure if this actually works, would be useful if someone could also point out any errors I’m making there too.
Take a close look at the second minus in:
On my machine it looks like this:
–. It’s longer than-, so it’s clearly a different character. The minus character is ASCII 45, whereas the character the compiler is complaining about is ASCII 150.To fix, simply replace that line with: