CS50凯撒:分割故障问题
我目前正在参加CS50课程,并且问题集2(Caesar)遇到了问题。我尝试运行下面的代码,但它给了我**分段错误**警告,但我似乎无法发现我自己的问题。 请问有什么问题,我该如何解决?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
bool only_digits(string s);
int main(int argc, string argv[])
{
int i,n;
string text;
n = strlen(text);
string s = argv[1];
int k = atoi(argv[1]);
if (argc != 2 || !only_digits(s))
{
printf("Usage: ./caesar key\n");
return 1;
}
text = get_string("plaintext: ");
for(i=0; i<n; i++)
{
if(isupper(text[i]))
{
char r, z;
r = text[i] + k;
while (r>90)
{
z = r-90;
text[i]= 64 + z;
}
}
if(!isalpha(text[i]))
{
text[i]= text[i]+0;
}
}
}
bool only_digits(string s)
{
int i;
int n = strlen(s);
for (i=0; i<n; i++)
{
if(!isdigit(s[i]))
{
return false;
}
}
return true;
}
I'm currently taking the cs50 course and I'm having an issue with the problem set 2 (caesar).I tried running the code below but it gave me a **SEGMENTATION FAULT ** warning but I can't seem to spot the problem by myself.
Please what is the problem and how do I resolve it?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
bool only_digits(string s);
int main(int argc, string argv[])
{
int i,n;
string text;
n = strlen(text);
string s = argv[1];
int k = atoi(argv[1]);
if (argc != 2 || !only_digits(s))
{
printf("Usage: ./caesar key\n");
return 1;
}
text = get_string("plaintext: ");
for(i=0; i<n; i++)
{
if(isupper(text[i]))
{
char r, z;
r = text[i] + k;
while (r>90)
{
z = r-90;
text[i]= 64 + z;
}
}
if(!isalpha(text[i]))
{
text[i]= text[i]+0;
}
}
}
bool only_digits(string s)
{
int i;
int n = strlen(s);
for (i=0; i<n; i++)
{
if(!isdigit(s[i]))
{
return false;
}
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论