2013-03-16 71 views
0

我应该建立一个程序,需要argv [1],并根据它将字符转换为小写或大写。但是我卡住cuz C不能比较指针一个字符串。关于如何比较一个指针和一个字符串的任何想法,我不想按字符比较它们。 下面是代码建立一个字符转换器程序与argv []

#include <stdio.h> 
#include <ctype.h> 
#include <string.h> 
int main (int argc,char *argv[]) 
{ 
    char c; 
    if(argc!=2) 
    printf("Wrong use of program \n"); 
    printf("The Format is Lower or Upper \n"); 
    return -1; 
    if ((strcmp(argv[1],"Lower"))==0) 
    { 
          while((c=getchar())!=EOF) 
          { 
                printf("-"); 
                putchar(tolower(c)); 
                printf("\n"); 
                } 
                } 
    if ((strcmp(argv[1],"Upper"))==0) 
    { 
          while((c=getchar())!=EOF) 
          { 
                printf("-"); 
                putchar(toupper(c)); 
                printf("\n"); 
                } 
                } 
    if ((strcmp(argv[1],"Lower"))!=0 && ((strcmp(argv[1],"Upper"))!=0)) 
    { 
          printf("Wrong use of program \n"); 
          printf("The Format is Lower or Upper \n"); 
          return -1; 
          } 

         return 0; 
} 

回答

2

首先,使用strcmp,如果char数组匹配,则返回0。

if (!strcmp(argv[1], "Lower")) 
{ 

其次,如果多个语句适用于if条件,则语句必须包含在{}中。

if (argc != 2) 
{ 
    printf("Wrong use of program \n"); 
    printf("The Format is Lower or Upper \n"); 
    return -1; 
} 
+0

虽然仍然没有工作。 – Lind 2013-03-16 14:12:34

+0

它现在适用于我。 – suspectus 2013-03-16 14:21:36

+0

我没有看到你的下一个edit.Thanks人。 – Lind 2013-03-16 15:30:37

3

你想要做的是使用函数strcmp或stricmp(不区分大小写)。