#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
}
}
这个程序在&#34; p [j-1-i] = temp;&#34;交换函数中出现错误。错误如下&#34; stray&#34; \ 150&#34;在节目&#34;想知道我如何解决这个错误
包含错误的函数应该反转内存中的字符串行,以便在函数运行后打印行时字符串反向。不确定这是否真的有效,如果有人也可以指出我在那里制造的任何错误,那将会很有用。
答案 0 :(得分:5)
仔细查看第二个减号:
p[j-1–i] = temp; // elements
在我的机器上看起来像这样:–
。它比-
长,所以它显然是一个不同的角色。减号是ASCII 45,而编译器抱怨的字符是ASCII 150。
要修复,只需将该行替换为:
p[j-1-i] = temp; // elements