I have been able to kind of get my transposition cipher to work a little bit. However, i am running into problems such as not being able to take in more than 5 characters in a text file. Currently my program is also not able to go to a new line when the encrypted text is outputted into a output file. I also am having trouble cycling my transposition cipher over and over again.
For example, if trans1.txt contained the text "JacksJacksJacks" all in one line it should print "csJakcsJakcsJak" all on the first line of the trans2.txt
Also the transposition cipher should reset every line. It should restart from position 2 then 4 then 0 etc... every time its a new line.
#include <stdio.h>
int c, j, i, k,p=0;
char transposition[]={2,4,0,1,3}, input[256];
int main(){
FILE *file1=fopen("trans1.txt", "r");
FILE *file2=fopen("trans2.txt", "w");
while(fgets(input,sizeof(input),file1)!=NULL){
for(i=0;i<5;i++){
k=transposition[i];
fprintf(file2,"%c",input[k]);
}
}
return 0;
}
#include <stdio.h>
int main(){
char transposition[]={2,4,0,1,3};
char input[256] = "JacksJacksJacks\n";
int len = sizeof(transposition);
char ch, temp[len];
int i, j, k;
j=i=0;
do {
for( ; '\0'!=(ch = input[i]) && ch != '\n';++i){
temp[j++] = ch;
if(j == len){
j=0;
++i;
break;
}
}
while(j!=0){
temp[j++] = '.';//pading if(i % len != 0)
if(j == len)
j = 0;
}
for(k=0;i && k<len;++k){
printf("%c", temp[transposition[k]]);
}
}while(ch != '\n' && ch != '\0');
printf("\n");
return 0;
}
another way for the same thing
i=0;
do {
for(j=0;j<len;++j){
ch = input[i];
if(ch != '\n' && ch != '\0'){
temp[j] = ch;
++i;
} else {
temp[j] = '.';
}
}
if(temp[0] != '.')
for(k=0;k<len;++k){
printf("%c", temp[transposition[k]]);
}
}while(ch != '\n' && ch != '\0');
printf("\n");
Some hints:
What happens after the for-loop processes 5 characters?
Your problem seems character oriented. Why not use getc()?
For extra credit:
What happens if, just for example, there were fewer than 5 characters on the 2nd line?
Related
I am currently learning C and working on a problem that breaks input lines into lengths of n. Below is my current code where n is set to 30. When it reaches the n-th index it replaces that index with ' ' and then line breaks, but it will only do it for the first n characters and I'm unsure what isn't getting rest in order to it to continue making a new line at the nth index.
int getline2(void);
int c, len, cut, counter;
char line[MAXLINE];
main() {
while ((len = getline2()) > 0) {
if (len > BREAK) {
c = 0;
counter = 0;
while (c < len) {
if (line[c] == ' ') {
counter = c;
}
if (counter == BREAK) {
line[counter] = '\n';
counter = 0;
}
counter++;
c++;
}
}
printf("%s", line);
}
return 0;
}
int getline2(void) {
int c, i;
extern char line[];
for (i = 0; i < MAXLINE - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
line[i] = c; //i gets incremented at the end of the loop
if (c == '\n') {
line[i] = c;
++i;
}
line[i] = '\0';
return i;
}
Your code is a little too complicated:
you do not need to store the bytes read from the file into an array, just output them one at a time, keeping track of the line length
when the line would become too long, output a newline and reset the count before you output the byte.
also not that none of these global variables deserves to be global.
and the prototype for main should be either int main(), int main(void) or int main(int argc, char *argv[]) or equivalent. main()` is an obsolete syntax that should be avoided.
Here is a modified version:
#include <stdio.h>
#define BREAK 30
int main() {
int c;
int len = 0;
while ((c = getchar()) != EOF) {
if (c == '\n') {
putchar(c);
len = 0;
} else {
if (len >= BREAK) {
putchar('\n');
len = 0;
}
putchar(c);
len++;
}
}
return 0;
}
For my assignment I need to modify the following program. I can not use strings.h.
int main(void)
{
int c, countSpaces = 0;
printf("Type sentence:\n");
do
{
c = getchar();
if (c == ' ')
countSpaces = countSpaces + 1;
}
while (c != '\n');
printf("Sentence contains %d Spaces.\n", countSpaces);
return 0;
}
I tried using
if (c != EOF)
countSpaces = countSpaces + 1;
}
while (c != '\n');
printf("Sentence contains %d Spaces.\n", countSpaces - 1);
but that seems like a hacky and unelegant way to do this.
Can anyone help and/or explain to me how to do this better?
Thanks in advance
The code I posted counts the spaces in a sentence, I want to modify it to count all the characters in the input sentence. – fbN 21 secs ago
Have another counter outside the if condition.
#include <stdio.h>
int main(void)
{
int c;
int countSpaces = 0;
int countChars = 0;
puts("Type sentence:");
do {
c = getchar();
countChars += 1;
if (c == ' ') {
countSpaces += 1;
}
} while (c != '\n');
printf("Sentence contains %d spaces and %d characters.\n", countSpaces, countChars);
return 0;
}
Two notes. foo += 1 is shorthand for foo = foo + 1 without the precendence complexities of foo++.
Blockless if or while is playing with fire. Eventually you'll accidentally write this.
if( condition )
do something
whoops this is not in the condition but it sure looks like it is!
Always use the block form.
$ ./test
Type sentence:
foo bar baz
Sentence contains 2 spaces and 12 characters.
Note this says 12 because it's including the newline. That's because it's checking what c is after it's already been counted. You can fix this by checking c as its read. This is a fairly normal "read and check" C loop idiom.
// Note, the parenthesis around `c = getchar()` are important.
while( (c = getchar()) != '\n' ) {
countChars++;
if (c == ' ') {
countSpaces++;
}
}
$ ./test
Type sentence:
foo bar baz
Sentence contains 2 spaces and 11 characters.
I always prefer to use fgets() when reading a line from the console (stdin):
#include <stdio.h>
int main(void)
{
int i;
int length = 0;
char buffer[1024];
printf( "Enter some text> " );
fgets( buffer, sizeof(buffer), stdin );
// If the user inputs > 1024 letters, buffer will not be \n terminated
for ( i=0; buffer[i] != '\n' && buffer[i] != '\0'; i++ )
{
length += 1;
}
printf( "length: %d\n", length );
return 0;
}
I make this code that count length of the string given It's like strlen function.
and I used just scanf and it works perfectly even with spaces.
#include <stdio.h>
int main()
{
char *str = calloc(sizeof(char),50);
int i = 0, count = 0;
printf("Type sentence:\n");
scanf("%[^\n]",str);
while (str[i++] != '\0')
count++; //length of the string
printf("%d",count);
return 0;
}
and if you want just to count the characters in the string given use this code below:
#include <stdio.h>
int main()
{
char *str = calloc(sizeof(char),50);
int count = 0;
printf("Type sentence:\n");
scanf("%[^\n]",str);
for (int i = 0; str[i] != '\0'; i++)
if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
count++;
printf("Sentence contains %d characters.\n",count);
return 0;
}
the output :
Type sentence:
hello world
Sentence contains 10 characters.
you can just calculate the result like this
int main(void)
{
int c, countSpaces = 0;
printf("Type sentence:\n");
do
{
c = getchar();
if (c == ' ')
countSpaces++;
}
while (c != '\n');
int countChar = c - countSpaces - 1 ; // -1 new line
printf("Sentence contains %d Spaces.\n", countSpaces);
printf("Sentence contains %d chars.\n", countChar);
return 0;
}
So my program should get input from an user and store it in an array. After that if the input string includes three 'a's in a row it should be replaced with a single '*'. However I can't seem to get it right. It only replaces the first a with a *. I tried to replace the following 2 a with a blank but the output looks funny.
For this exercise I have to use putchar() and getchar().
Thank you in advance.
#include <stdio.h>
char c;
char buffer[256];
int counter= 0;
int i;
int main()
{
while ((c = getchar()) != '\n') {
buffer[counter] =c;
counter++;
if (counter >255) {
break;
}
}
for(i=0; i<256; i++) {
if(buffer[i]== 'a'&&buffer[i+1]=='a'&&buffer[i+2]=='a')
{
buffer[i]= '*';
buffer[i+1]=' ';
buffer[i+2]=' ';
}
putchar(buffer[i]);
}
putchar('\n');
return 0;
}
So my program should get input from an user and store it in an array.
After that if the input string includes three 'a's in a row it should
be replaced with a single '*'. However I can't seem to get it right.
You almost got it! Just move index by 2 to and continue.
#include <stdio.h>
char c;
char buffer[256];
int counter= 0;
int i;
int main(void)
{
while ((c = getchar()) != '\n') {
buffer[counter] =c;
counter++;
if (counter >= 255) {
break;
}
}
buffer[counter] ='\0';
for(i=0; i<256; i++) {
if(buffer[i]== 'a'&&buffer[i+1]=='a'&&buffer[i+2]=='a')
{
buffer[i]= '*';
putchar(buffer[i]);
i = i + 2;
continue;
}
putchar(buffer[i]);
}
putchar('\n');
return 0;
}
Test:
123aaa456aaa78
123*456*78
In string you must assign a end of character at the end and that is call null character \0 or just a numeric 0. Correct your code like below:-
while ((c = getchar()) != '\n') {
buffer[counter] =c;
counter++;
if (counter >=255) {
break;
}
}
buffer[counter] ='\0';// or buffer[counter] =0;
To avoid side effect in a string array always set all its value with 0 first:-
char buffer[256];
memset(buffer, 0, sizeof(buffer));
If you want to change the number of characters, you will need to create a different buffer to copy the output to.
If you really just want to output to the console, you could just write every character until you hit your matching string.
#include <stdio.h>
char c;
char buffer[256];
char output[256];
int counter= 0;
int i, j;
int main()
{
while ((c = getchar()) != '\n') {
buffer[counter] = c;
counter++;
if (counter >255) {
break;
}
}
buffer[counter] = 0;
for(i=0, j=0; i<256; i++, j++) {
if(buffer[i] == 'a' && buffer[i+1] == 'a'&& buffer[i+2] == 'a')
{
output[j]= '*';
i += 2;
}
else
output[j] = buffer[i];
putchar(output[j]);
}
putchar('\n');
return 0;
}
There are multiple problems in your code:
there is no reason to make all these variables global. Declare them locally in the body of the main function.
use int for the type of c as the return value of getchar() does not fit in a char.
you do not check for EOF.
your test for buffer overflow is off by one.
you do not null terminate the string in buffer. You probably make buffer global so it is initialized to all bits 0, but a better solution is to set the null terminator explicitly after the reading loop.
to replace a sequence of 3 characters with a single one, you need to copy the rest of the string.
You can use a simple method referred as the 2 finger approach: you use 2 different index variables into the same array, one for reading, one for writing.
Here is how it works:
#include <stdio.h>
int main() {
char buffer[256];
int c;
size_t i, j, counter;
for (counter = 0; counter < sizeof(buffer) - 1; counter++) {
if ((c = getchar()) == EOF || c == '\n')
break;
buffer[counter] = c;
}
buffer[counter] = '\0';
for (i = j = 0; i < counter; i++, j++) {
if (buffer[i] == 'a' && buffer[i + 1] == 'a' && buffer[i + 2] == 'a') {
buffer[j] = '*';
i += 2;
} else {
buffer[j] = buffer[i];
}
}
buffer[j] = '\0'; /* set the null terminator, the string may be shorter */
printf("modified string: %s\n", buffer);
return 0;
}
Trying to make a function that outputs the count of words a user enters into the program. I keep getting a value of 2 with this code and I am not sure why or where I am going wrong. If someone can point me in the right direction that would be great. Thanks!
Here is the code:
#include <stdio.h>
#include <string.h>
int wordcount(char string[101]){
int i, words = 0;
for(i=0;string[i] != '\0';i++)
{
if(string[i] != '\t' && string[i] != '\0'){
words++;
while(string[i] != ' ' && string[i] != '\t' && string[i] !=
'\0')
i++;
}
}
return words;
}
int main (void) {
char input[101];
printf("Word Counter\n");
printf("============\n");
printf("string to be analyzed: \n");
scanf("%100[^\n]", input);
wordcount(input);
printf(" %s - words = %i\n", input, wordcount(input));
}
The main problem now is there are two.
if(string[i] != '\t' && string[i] != '\0'){ deterioration, changing for the worse
i is +1 to twice in the for and while.
For example, it will change as follows.
int wordcount(char string[101]){
int i, words = 0;
for(i=0;string[i] != '\0'; i++){
if(string[i] != ' ' && string[i] != '\t'){// assuming not included the newline.
words++;
i += strcspn(&string[i], " \t") -1;// i point to last character of word
}
}
return words;
}
or i changes it will be in one place.
int wordcount(char string[]){
int i, words = 0;
char prev =' ';
for(i=0;string[i] != '\0'; i++){
if(isspace(prev) && !isspace(string[i])){//isspace in <ctype.h>
words++;//count front edge
}
prev = string[i];
}
return words;
}
So i was finally able to get my transposition cipher to work. But i needed to be able to take in variables from command line arguments. For example my transposition table given by transposition[] is subject to change based on the inputs in the command line arguments, also given an npos in the command line it determine how many characters i shift in total. For example if i put in the command line "a.out fileinput.txt fileoutput.txt 2 4 2 4 0 1" it should make it so that my program recognizes there is only 4 variables in the transposition array and the numbers in the transposition array are "2 4 0 1". Basically, i just want to know if there is a way to take in numbers from the command line and then store them into an array (specifically transposition array). I have tried using sscanf to take in the different arguments in the command line but it seems to not be working.
UPDATED Current Code:
#include <stdio.h>
int main(int argc, char *argv[]){
char transposition[]={};
char input[256];
char ch;
int i, j, k, npos;
FILE *file1=fopen(argv[1], "r");
FILE *file2=fopen(argv[2], "w");
sscanf(argv[3], "%d", &npos);
for(i=0;i<npos;++i){
sscanf(argv[4+i], "%d", &k);
transposition[i] = k;
}
int len= sizeof(transposition);
char temp[len];
while(fgets(input,sizeof(input),file1)!=NULL){
i=0;
do {
for(j=0;j<len;j++){
ch = input[i];
if(ch != '\n' && ch != '\0'){
temp[j] = ch;
++i;
} else {
temp[j] = ' ';
}
}
if(temp[0] != '.')
for(k=0;k<len;k++){
fprintf(file2,"%c", temp[transposition[k]]);
}
}
while(ch != '\n' && ch != '\0');
fprintf(file2,"\n");
}
return 0;
}
Original Working Code:
#include <stdio.h>
int main(int argc, char *argv[]){
char transposition[]={2,4,0,1,3};
char input[256];
int len= sizeof(transposition);
char ch, temp[len];
int i, j, k;
FILE *file1=fopen(argv[1], "r");
FILE *file2=fopen(argv[2], "w");
while(fgets(input,sizeof(input),file1)!=NULL){
i=0;
do {
for(j=0;j<len;j++){
ch = input[i];
if(ch != '\n' && ch != '\0'){
temp[j] = ch;
++i;
} else {
temp[j] = ' ';
}
}
if(temp[0] != '.')
for(k=0;k<len;k++){
fprintf(file2,"%c", temp[transposition[k]]);
}
}
while(ch != '\n' && ch != '\0');
fprintf(file2,"\n");
}
return 0;
}
...
sscanf(argv[4], "%d", &npos);
char transposition[npos];
...
for(i=0;i<npos;++i){
transposition[i] = atoi(argv[5+i]);//atoi : <stdlib.h> or sscanf(argv[5+i], "%d", &k);transposition[i] = k;
}
...