I already have a C Program that is complete and correct, but I just want it to h
ID: 3547097 • Letter: I
Question
I already have a C Program that is complete and correct, but I just want it to have an error checking element. For instance, if someone adds too many characters to an array with the specified length, I want the user to be able to receive an error message and re-type an accurate amount of characters. Example: If someone typed in 30001 characters in the string for a 30000 character limit, a message should be printed and the user should be able to re-type a correct value.
Here is a link with the program details (all of it complete, but I'll put it for your understanding):
https://www.dropbox.com/s/vv91smjitydrvbg/program_details.docx
Here is the completed code:
#include <stdio.h>
#include <string.h>
void func (char a[]);
int main (void)
{
int n;
char a[30000];
printf("How many strings would you like to zip? ");
scanf("%d",&n);
printf("Enter your string: ");
while(n--)
{
scanf("%s",&a);
func(a);
}
return 0;
}
void func(char a[])
{
int i,length= strlen(a),count=0;
char b=a[0];
for (i=0;i<length;i++)
{
if(a[i]==b)
{
count++;
}
else
{
printf("%d%c",count,a[i-1]);
count=1;
b=a[i];
}
}
printf("%d%c ",count,a[length-1]);
}
Explanation / Answer
the colored lines are my addition.
-------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
void func (char a[]);
int main (void)
{
int n;
char a[30000];
int length=0;
printf("How many strings would you like to zip? ");
scanf("%d",&n);
do
{
printf("enter your string ");
scanf("%s",&a);
length=strlen(a);
if (length>30000)
printf(" you have exceeded the desired character limit.please input another ");
}while(length>30000);
while(n--)
{
func(a);
}
return 0;
}
void func(char a[])
{
int i,length= strlen(a),count=0;
char b=a[0];
for (i=0;i<length;i++)
{
if(a[i]==b)
{
count++;
}
else
{
printf("%d%c",count,a[i-1]);
count=1;
b=a[i];
}
}
printf("%d%c ",count,a[length-1]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.