Hi to everyone
Wich funcion should I use if I want to shift all the letters of a word ?
like if I have AAABC and I want to shift them using a X offset
X=1
BBBCD
X=2
CCCDE
I hope someone can help me thanx
Have a nice day !
Hi to everyone
Wich funcion should I use if I want to shift all the letters of a word ?
like if I have AAABC and I want to shift them using a X offset
X=1
BBBCD
X=2
CCCDE
I hope someone can help me thanx
Have a nice day !
This will give you the main idea, but you'll still need to
add some functionality to worry about wrap around
and vaild inputs etc.
Code:char * shiftStr (char str[], int offset) { unsigned int i; for (i = 0; str[i] != '\0'; i++) { str[i] += offset; } return str; }
DavT
-----------------------------------------------
ohh thanx man
mh, there is a way to say to the program that I want only shift on this letters?
abcdefghilmnopqrstuvz? (and not xywj and all char > z like @Ô╩─♥ and so on?)
I wrote that :
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLINE 40 char * shiftStr (char str[], char destination[], int offset) { unsigned int i; for (i = 0; str[i] != '\0'; i++) { destination[i] = str[i]+offset; switch(destination[i]){ case 'j': destination[i] = str[i]+offset+2; break; case 'k': destination[i] = str[i]+offset+1; break; case 'w': destination[i] = str[i]+offset+3; break; case 'x': destination[i] = str[i]+offset+2; break; case 'y': destination[i] = str[i]+offset+1; break; default : destination[i] = str[i]+offset; break; if (destination[i]>'z') destination[i]=destination[i]-'z'; } } return destination; } int main(int argc, char *argv[]) { char parola[MAXLINE]; char destination[MAXLINE]; int cont; scanf("%s", &parola); strcpy (destination,parola); shiftStr (parola,destination,1); printf("%s \n",destination); shiftStr (parola,destination,2); printf("%s \n",destination); shiftStr (parola,destination,3); printf("%s \n",destination); shiftStr (parola,destination,4); printf("%s \n",destination); shiftStr (parola,destination,5); printf("%s \n",destination); shiftStr (parola,destination,6); printf("%s \n",destination); shiftStr (parola,destination,7); printf("%s \n",destination); shiftStr (parola,destination,8); printf("%s \n",destination); shiftStr (parola,destination,9); printf("%s \n",destination); shiftStr (parola,destination,10); printf("%s \n",destination); shiftStr (parola,destination,11); printf("%s \n",destination); shiftStr (parola,destination,12); printf("%s \n",destination); shiftStr (parola,destination,13); printf("%s \n",destination); shiftStr (parola,destination,14); printf("%s \n",destination); shiftStr (parola,destination,15); printf("%s \n",destination); shiftStr (parola,destination,16); printf("%s \n",destination); shiftStr (parola,destination,17); printf("%s \n",destination); shiftStr (parola,destination,18); printf("%s \n",destination); shiftStr (parola,destination,19); printf("%s \n",destination); shiftStr (parola,destination,20); printf("%s \n",destination); shiftStr (parola,destination,21); printf("%s \n",destination); shiftStr (parola,destination,22); printf("%s \n",destination); shiftStr (parola,destination,23); printf("%s \n",destination); shiftStr (parola,destination,24); printf("%s \n",destination); shiftStr (parola,destination,25); printf("%s \n",destination); system("PAUSE"); return 0; }
I think with the red code it can work like I want but it seems to fail... but why ? I still have >z output
What do you mean by 'shift' ?Originally Posted by Cow
"Hello" -> "lloHe"
or
"Hello" -> "Jgnnq" ('Caesar code' : +2)
Emmanuel Delahaye
"C is a sharp tool"
it is exatly for the 'Caesar code' but for ITALIAN alphabet
What the hell is the Italian alphabet? I'm French, what is the difference with the French alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ?Originally Posted by Cow
Emmanuel Delahaye
"C is a sharp tool"
in italian there are not J K W X Y
Consider using isalpha() to test for alphabetic characters. I'll assume you want to wrap the characters, so if you "shift" a Z it becomes A, you could always just use isupper or islower, and wrap them with a bit of simple math. I'll leave that up to you, because to provide an example of how to do it, would do all the work for you.
[edit due to all the alphabetic nonsense you've posted since I started my post...]
Since you're apparently not using the standard 26 characters, just use a string which contains your "alphabet", and find your letter in it, then shift over by the offset, wrapping around to the start.
[/edit]
Quzah.
Last edited by quzah; 07-01-2004 at 10:11 AM.
Hope is the first step on the road to disappointment.
What if you process a foreign word ? I'm quite sure that it may occur, even in Italian...Originally Posted by Cow
If you insist, you can use two alphabet references:
(I guess the digits are left unchanged)
char const it_aplha_lwr[] = "abcdefghilmnopqrstuv";
char const it_aplha_upr[] = "ABCDEFGHILMNOPQRSTUV";
select the letter set with isalpha(), then the uppercase/lowercase subset with isupper() or islower(), then find the letter in the corresponding alphabet with strchr(), compute the position (difference of address), compute the position of the new letter (add n and wrap according to the number of letters of the alphabet), return the letter at the new position.
Sounds easy. Do your best and post it if you are stuck.
BTW, it's more a design question than a C one...
Last edited by Emmanuel Delaha; 07-01-2004 at 10:36 AM. Reason: Addition
Emmanuel Delahaye
"C is a sharp tool"
it works
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> char letters[21] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'z' }; int search(char c); int main(int argc, char *argv[]) { char *result; int offset, i; char word[40]; printf("Inserisci la parola da elaborare: "); scanf("%s", &word); printf("\n"); offset = 1; while(offset<21){ result = (char *) calloc(strlen(word) + 1, sizeof(char)); for (i = 0; word[i] != '\0'; i++) if (word[i] == ' ') result[i] = ' '; else result[i] = letters[(search(word[i]) + offset) % 21]; printf("%s\n", result); free(result); ++offset; } printf("\n"); system("PAUSE"); return 0; } int search(char c) { int i; for (i = 0; i < 21; i++) if (letters[i] == c) return i; return -1; }
Your search function is flawed. Your problem comes when you encounter a character that is not in the alphabet. In this case, your search function returns -1.
This, combine with the way you're using it, will always give you 'a'.
If you are trying to "encode" a message, it will be impossible to "decode" it back to the origional because of this.
Quzah.
Hope is the first step on the road to disappointment.
I know, I will surely add something to control it, anyway except human errors, I wil never have to analyze not italian wordsOriginally Posted by quzah
now i'm trying to add a cool thing, I bet I will ask help again in some hours lol
thanx everytone for your help
Ok my program works but I want to add a dictionary control.
I have the "masked" word, I write it in my program and it creates 20 new words. ONE of this is the word I was looking for.
I want to search that word using a dictionary.
I tried to add to my program some old code I wrote time ago :
(working!)Code:#include<stdio.h> #include<string.h> void main(void){ FILE *in; char *s; char cerca[20]; in=fopen("pippo.plu","r"); scanf("%s",&cerca); while(!feof(in)) { fscanf(in,"%s",s); if(strstr(cerca,s))printf("trovato"); } system("pause"); }
But when I put it in my last program, everything crushes...
I really dont know why... It seems all ok but obviously it is not..Code:#include <stdio.h> #include <stdlib.h> #include <string.h> char letters[21] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'z' }; int search(char c); int main(int argc, char *argv[]) { char *result; int offset, i; FILE *dizionario; char *s; char word[40]; printf("Inserisci la parola da elaborare: "); scanf("%s", &word); printf("\n"); offset = 1; while(offset<21){ result = (char *) calloc(strlen(word) + 1, sizeof(char)); for (i = 0; word[i] != '\0'; i++) if (word[i] == ' ') result[i] = ' '; else result[i] = letters[(search(word[i]) + offset) % 21]; printf("%s offset %2d", result,offset); dizionario = fopen("dizionario.txt","r"); while(!feof(dizionario)) { fscanf(dizionario,"%s",s); if(strstr(result,s)) printf(" <-- found --> %s",result); else printf("\n"); } fclose (dizionario); free(result); ++offset; } printf("\n"); system("PAUSE"); return 0; } int search(char c) { int i; for (i = 0; i < 21; i++) if (letters[i] == c) return i; return -1; }
Can u see where is the mistake??