How to modify ASCII with prime numbers C program -
here's program asks user enter line of text , shows ascii code it's associated with, , after modifies ascii code adding plus 1 i, , prints out new character associated new ascii code.
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> int main() { char s[ 70 ]; // define character array of size 70 size_t i; // loop counter // use gets text user puts( "enter line of text: \a\n" ); fgets( s, 70, stdin ); // convert each character ascii , output puts("\n ascii code characters \n"); ( = 0; s[ ] != '\0'; ++i ) { printf(" %c:%d",(s[ ]),(s[ ])); } // end uppercase ascii // convert each character modified ascii , output puts( "\nthe line modified progressively higher ascii:\n" ); ( = 0; s[ ] != '\0'; ++i ) { printf( "%c", ( s[ ] + (i+1)) ); } // end modified characters puts(""); printf( " \n modified ascii code entered string \n\n\a"); ( = 0; s[ ] != '\0'; ++i ) { printf( " %c:%d",(s[ ] + (i+1)),(s[ ] +(i+1))); } // end modified ascii code printf( "\n \t final value of loop variable 'i' is:%d\n", i); puts( "" ); system("pause"); }
i'm having difficulty modifying ascii code , characters change first 7 prime numbers 2,3,5...17 , after first 7 prime numbers go in reverse 17,13,11,7,...2. after first loop g has ascii code 103, modified ascii code 105 , new letter i. can me out?
start declaring array storing pattern (primes, in ascending order , descending order, in such way reducing modulo arrays size cause cycle). example:
unsigned int prime[] = { 2, 3, 5, 7, 11, 13, 17, 13, 11, 7, 5, 3 };
now, rather adding (i + 1)
each character in loop, you'll want add prime[i % (sizeof prime / sizeof *prime)]
. example:
for (i = 0; s[i] != '\0'; i++) { putchar(s[i] + prime[i % (sizeof prime / sizeof *prime)]); }
p.s. technically, it's not required ascii. think of way: c , ascii go coffee , cows milk. people love it, use coconut milk or cream instead.
Comments
Post a Comment