You are on page 1of 4

7 ' -

( 1

:
double strToDouble(char str[]);

,
.(double-)
.2356.12- " -122356." ,
" ,"0" : .( ),
."145.63" ,"0.3-" ,"0.3
. -
. 100-
.stdlib.h
.

/* Assignment No: assignment 06*/


/* Question No: question 01*/
#include <stdio.h>
#define ARRAY_SIZE 101

int PowerOfTen(int power)


/* the function calculates the power of 10 for any power*/
{
int i=0, power_result=10;
if (power>1)
for (i=2 ; i<=power ; i++)
power_result= power_result*10;
else
if (power=0)
power_result=1;
return power_result;
}
double strToDouble(char str[])
{
double num=0.0;
int sign=1, counter=0, i=0, j=0;
if (str[0]!='-')
/* the number is positive*/
num=(str[0]-'0');
else
/* the number is negative*/
sign=-1;
for (i=1 ; (str[i] != '\0' && str[i] != '.' ) ; i++)
/* the loop put together all the digits from the second char*/
num=num*10+(str[i]-'0');
if (str[i]=='.')
/* the number is not an integer*/
{
for (j=i+1 ; str[j] != '\0' ; j++)
{
counter++;
num=num*10+(str[j]-'0');
}





10.

;))num=num/(PowerOfTen(counter

;)return (num*sign
}
)(int main
{
;]char string[ARRAY_SIZE
;double result
;int i=0

;)"printf ("please insert a string:\n


;)scanf("%s", string
;)result= strToDouble(string
;)printf("the number is %g \n", result
;return 0
}

(2

:
;)][int identify (char str

:
n ) n (1

k , ) k < n

, k- (

0 1- . .
:

:
:
:
:
:
:
:

1 :
aacd
1 :
aab
1 :
aaaabcc
0 :
aacdd
0 :
aabddd
0 :
aaccdd
0 :
aacc

. ) (,
' .
100 .
) -(.

:
:
cabc
aaacdd

ab
1ababababc

aaacdd
ab

* Assignment No: 7
* Question No: 2
#include <stdio.h>
#define FALSE 0
#define TRUE (!FALSE)
#define STR_LENGTH 100

int identify (char str[])


/* This function receives a string from the main program and checks whether or not it fulfills
the exercise requirements.
If they are not fulfilled, the function returns 0 (FALSE). If they are fulfilled, returns a different
number (TRUE).
This function assumes the string is correct according to the assignment's preposition.*/
{
int i=0, n=1, k=0, condition=FALSE;
char mid_char, k_char;
//In case of an empty string or a string with only one character:
if ((str[0]=='\0') || (str[1]=='\0'))
return FALSE;
for (i=1; str[i] != '\0'; i++)
{
if (str[i] == str[i-1])
if (condition == FALSE)
n++;
else k++;
if ((str[i] != str[i-1]) && (condition == FALSE))
if (str[i] != str[i+1])
{
condition= TRUE;
mid_char= str[i];
k_char= str[i+1];
continue;
}
else return FALSE;
if ((str[i] != str[i-1]) && (condition))
{
if (str[i-1] == mid_char)
{
k++;
continue;
}
if (str[i] != k_char)
return FALSE;
}
}

if ((n > k) && (condition))


return TRUE;
else return FALSE;

int main()
/* Note: Assumes the user only inserts strings that fulfill the assignment's definition of a proper
string*/
{
char str[4][STR_LENGTH+1];
int i=0, check=TRUE;
printf("Please insert 4 valid strings:\n");
for (i=0; i<4; i++)
scanf("%100s", str[i]);
printf("The following strings are correct:\n");
//Prints the strings that fulfill the requirements:
for (i=0; i<4; i++)
if (identify(str[i]))
{
printf("%s\n", str[i]);
//Checks if there's at least one string that fulfills the requirement (FALSE
if fulfills):
check= FALSE;
}
if (check)
printf("NONE are correct!\n");
}

return 0;

You might also like