Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

question 1: Write a getInteger function to check if a string is an integer. Firs

ID: 3546977 • Letter: Q

Question

question 1: Write a getInteger function to check if a string is an integer. First check the first character. If it's a positive (+) or negative (-) sign, check the next character. If it's a number between 0 and 9, check the next one until the end. If all are digits, display the number. If a non digit is encountered, the number is not a valid integer. Display a text saying so. If the first character is non digit, there is no need to check the next characters. The number is not a valid integer. Display a text saying so. If the first character is a digit, check the next chracter until the end. If all are digits, display the number.

Write a program to get a valid integer. The program asks the user to enter an integer, which can be positive or negative. Call the getInteger function to display either the integer or the text saying it's not a valid integer..

Sample run:

Write a program to get the number of characters. The program asks the user to type in any number of characters. Call the count function. Display the number of characters.

Sample run:

Explanation / Answer

q1)


#include<stdio.h>

#include<string.h>

int main()

{

char a[1000];

while(1)

{

int flag=0;

a[0]='';

printf("Enter an integer: ");

scanf("%s",&a);

int i,j=strlen(a);

if((a[0]>=48 && a[0]<=57) || a[0]=='+' || a[0]=='-')

{

i=1;

while(a[i]>=48 && a[i]<=57 && i<j)

{

i++;

}

if(i==j-1)

{

printf("The number you entered is ");

if(a[0]=='-')

{

for(i=0;i<j;i++)

{

printf("%c",a[i]);

}

}

else if(a[0]=='+')

{

for(i=1;i<j;i++)

{

printf("%c",a[i]);

}

}

}

else

{

flag=1;

}

}

else

{

flag=1;

}

if (flag==1)

{

printf("The number you entered is not a valid integer. ");

}

}

return 0;
}




q2)


#include<stdio.h>

#include<string.h>

int main()

{

char a[10000];

while(1)

{

a[0]='';

printf("Type in any number of characters: ");

scanf("%s",&a);

int i=0,j=strlen(a);

int count=0;

for(i=0;i<j;i++)

{

if(a[i]!=" ")

count++;

}

printf("The number of characters just entered is %d ",count);

}

return 0;

}