我是Objective C的初学者,所以请不要说太复杂的词。我正在做一个删除某些字符(元音,辅音,点等)的程序,但是当我尝试让用户输入他们自己的语句时,编译器只是忽略我的fgets
(语句),它忽略了我的 while 循环条件第二个时间。问题出在第 81-87 行(允许用户输入他们想要删除的字符)和第 98 行(while 循环输入语句。同样我不是专业人士,只知道一些基础知识。抱歉格式不正确。我不太了解stackoverflow
#import <Foundation/Foundation.h>
#define max 100
//characters is the character that the function is going to check for
NSCharacterSet *isChar(NSString * characters)
{
NSCharacterSet * theChar=[NSCharacterSet characterSetWithCharactersInString:characters];
return theChar;
}
NSString *removeChar(NSString * myInput , NSString * characters)
{
NSString * name;
name=[NSMutableString stringWithString:myInput];
name = [[name componentsSeparatedByCharactersInSet: isChar(characters)] componentsJoinedByString: @""];
return name;
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
char myString[max];
char tempWord[max];
NSInteger length =0;
NSInteger otherlength=0;
NSString * NewString;
NSString * characters;
NSString * myInput;
int k = 0;
char l=('y');
NSLog(@"Enter a String");
fgets(myString, max, stdin);
length=strlen(myString);
myString [length - 1] = 0;
myInput= [[NSString alloc] initWithUTF8String:myString];
while (l=='y')
{
NSLog(@"What would you like to do?");
NSLog(@"1:Remove Vowels");
NSLog(@"2:Remove Punctuation");
NSLog(@"3:Remove Constanants");
NSLog(@"4:Remove Digits");
NSLog(@"5:Remove whatever you want");
scanf("%d",&k);
if (k==1)
{
characters=@"aAeEiIoOuU";
}
if (k==2)
{
characters=@"!@#$%^&*()+_-|}]{[';:/?.>,<";
}
if(k==3)
{
characters=@"qQwWrRtTyYpPsSdDfFgGhHjJlLzZxXcCvVbBnNmM";
}
if (k==4)
{
characters= @"1234567890";
}
if (k==5)
{//My problem starts here
NSLog(@"Enter a String");
fgets(tempWord, max, stdin);
otherlength=strlen(tempWord);
tempWord [length - 1] = 0;
characters= [[NSString alloc] initWithUTF8String:tempWord];
}
NSLog(@"Your orignal string is: %@ ", myInput);
NewString=removeChar(myInput,characters) ;
NSLog(@"Your new string is: %@", NewString);
NSLog(@"Do you want to continue?");
//The scanf works the first time but when it goes thru the loop the second time it
//it gets ignored
scanf("%c",&l);
}
NSLog(@"Take Care :)");
}
return 0;
}