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;
}
...
Related
I was trying to complete the task requested by this exercise from the K&R book:
Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.
I still have to figure out how to implement the first task, but I have this idea for the second one:
#include <stdio.h>
#define MAXLINE 1000
#define IN 1
#define OUT 1
int blank1(char s[], int len){
int i;
int c=0;
for (i=0; i<len;i++){
if(s[i] == ' ' | s[i] == '\t')
c++;
}
if (c==(len-1))
return 1;
else
return 0;
}
int get_line1(char s[], int lim){
int c,i;
for (i=0; i<lim-1 && ((c=getchar())!= EOF) && c!='\n'; i++){
s[i]=c;
}
if (c=='\n'){
s[i]=c;
i++;
}
s[i] = '\0';
return i;
}
int main() {
char line[MAXLINE];
int len;
int j=1;
int i;
while(j){
len=get_line1(line,MAXLINE);
if ((blank1(line,len)) == 0) {
printf("%s", line);
}
if (getchar() == EOF){
j=0;
}
}
}
The function blank1 takes as input a string s[] and the string's length len.
Then it cycles for all the string and increases the counter i every time it hits a blank or tab. If the string is completely made of blank or tab, such as \t\t\t\n\0, then the value of i will be the same value of the length of the string - 1 (so to speak, the length of the string excluding the character \n). If the string is completely blank, then 1 will be returned, otherwise 0. So in this way if blank(string,string's length) returns 1, we know it is a wholly blank string and we can avoid printing it, as the exercise requests.
The problem is that with some outputs this program cuts the first letter. For instance with:
Once upon a time\n
There was a little monkey\n
That\n
What is printed is:
Once upon a time
here was a little monke
hat
I can't manage to get why this truncation occurs.
EDIT:
#include <stdio.h>
#define MAXLINE 1000
#define IN 1
#define OUT 1
int blank1(char s[], int len){
int i;
int c=0;
for (i=0; i<len;i++){
if(s[i] == ' ' | s[i] == '\t')
c++;
}
if (c==(len-1))
return 1;
else
return 0;
}
int get_line1(char s[], int lim){
int c,i;
for (i=0; i<lim-1 && ((c=getchar())!= EOF) && c!='\n'; i++){
s[i]=c;
}
if (c==EOF)
return -1; /////
if (c=='\n'){
s[i]=c;
i++;
}
s[i] = '\0';
return i;
}
int main() {
char line[MAXLINE];
char verso[MAXLINE];
char longest[MAXLINE];
int len;
int j=1;
int i;
while(j){
len=get_line1(line,MAXLINE);
if (len==-1){ ///////////
j=0;
}
else if ((blank1(line,len)) == 0) {
printf("%s", line);
}
}
}
getchar() == EOF in main is swallowing your first character from each line (after the first). You are better off calling getchar in only one place.
One possibility that causes minimal churn to your current implementation is to keep the only getchar call inside get_line1 and return -1 from get_line1 if a EOF is read there, to then be handled inside main, instead of the call to getchar inside main.
Do not program in main use functions for similar tasks.
char *skipLeadingBlanks(char *str)
{
char *wrk = str;
while((*wrk && *wrk != '\n') && (*wrk == ' ' || *wrk == '\t')) *wrk++;
memmove(str, wrk, strlen(wrk) + 1);
return str;
}
int main(void)
{
char str[1024];
while(fgets(str, sizeof(str), stdin))
{
skipLeadingBlanks(str);
if(!*str || *str == '\n') continue; //empty line
printf("%s",str);
}
}
https://godbolt.org/z/hxW5zP5Mx
int count0=0,count1=0,cnt=0;
char str[200];
char ch;
ch= getchar();
while(ch!='\0')
{
str[cnt]=ch;
cnt++;
}
str[cnt]='\0';
printf("%s",str);
output expected :
shubham
output:
Your code didn't print anything.
input:
shubham
You are just accepting one character, you should replace while() with do-while().
int ch;
do
{
ch= getchar();
if(ch == EOF)
{
str[cnt] = '\0';
break;
}
else
str[cnt] = ch;
cnt++;
}while(ch != '\0');
The above loop should fix the issue that you are facing. You need to enter NULL terminator using ctrl+# at the end of the string.
A while loop is fine. Simply put the getchar() in it.
Use int ch rather than char ch as getchar() typically returns 1 of 257 different values: 0 - 255 and EOF(a negative value). EOF indicates end-of-file (or rare input error).
size_t count = 0;
int ch;
// test to insure not too many read, ch == EOF? ch == end-of-line?
// | | |
while (count < sizeof str - 1 && (ch == getchar()) != EOF && ch != '\n') {
str[count++] = ch;
}
str[count] = '\0';
puts(str);
Or perhaps one prefers a for() loop?
size_t count;
for (count = 0; count < sizeof str - 1; count++) {
int ch == getchar();
if (ch == EOF || ch == '\n') {
break;
}
buf[count] = ch;
}
buf[count] = '\0';
puts(str);
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int i=0, j=0,T;
T=getchar();
int ch;
for(i=0; i<T ;i++)
{
int cnt=0;
char str[200];
while ((ch = getchar()) != '\n' && ch != EOF)
{
str[cnt] = ch;
cnt++;
}
str[cnt]='\0';
printf("\n%s",str);
}
return 0;
}
**
here str is storing almost everything
space, \n,
**
expected output:
shubham
shubh
output:
shubham
shubh
shubham
shubh
input:
shubham
shubh
*/
I use pointers to open a .txt file that consists of several paragraphs. I used a for loop to store the .txt file into a char word[i] array and then printf it.
Everything worked well except I don't want to store white spaces and special characters. I only want to store alphabetical characters such as ABCD......Z into my char word[i] array.
I know I have to put if functions into my for loops, but I don't know the exact syntax. Please help.
here is the for loop of my code :
for (i=0; i<1730; i++ )
{
fscanf(fptr,"%c", &word[i]);
printf("%c", word[i]);
};
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fptr;
char chr;
char *file_name="IdentifiedParts.txt";
fptr = fopen(file_name, "r");
while ( (fscanf(fptr,"%c",&chr)) != EOF ) {
//ASCII values : 65-90 => A-Z , 97-122 => a-z
if( (chr >= 97 && chr <= 122 ) || (chr >= 65 && chr <= 90 ) ) {
printf("%c", chr);
}
}
fclose(fptr);
return 0;
}
OR
while ( (fscanf(fptr,"%c",&chr)) != EOF ) {
if(isalpha(chr)) {
printf("%c", chr);
}
}
something like this:
#include <stdio.h>
#include <ctype.h>
int main(void){
char word[1730];
FILE* fptr = fopen("input.c", "r");
int i, ch;
for (i = 0; i<1730 && (ch = fgetc(fptr)) != EOF; i++ ){
if(isalpha(ch))
printf("%c", word[i]=ch);
}
fclose(fptr);
return 0;
}
#include <stdio.h>
int main(void){
char word[1730];
FILE* fptr = fopen("input.txt", "r");
while(!feof(fptr)){
while(1 == fscanf(fptr, "%1729[A-Za-z]", word)){
printf("%s", word);
}
fscanf(fptr, "%*[^A-Za-z]");
}
fclose(fptr);
return 0;
}
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?
So I'm writing a practice program in C which has the purpose of taking user input and then after EOF is reached, it reads back the input but only lines that were longer than 10 characters.
I am on Linux, so EOF is Ctrl + D, but, if an input line is longer than 10, it prints when I push enter, rather than waiting until EOF is reached.
here is my code:
#define MAXSIZE 1000
#define SIZE 10
int checklen(char line[], int index);
int main()
{
char currentline[MAXSIZE];
int i = 0;
while ((currentline[i] = getchar()) != EOF){
if (currentline[i] == '\n'){
if (checklen(currentline, i) > SIZE){
printf("%s", currentline);
}
}
++i;
}
return 0;
}
int checklen(char line[], int index)
{
int i;
for (i=index; line[i] != '\n'; ++i){
;
}
return i;
}
EDIT: I have been trying to figure it out for quite a while now with no luck. I'm not really understanding what you guys are saying and everything but we'll get there eventually :)
I have since rewritten the code but it is still not working.
#include <stdio.h>
#define MAX 1000
#define SIZE 10
void examine(char input[], int index);
int main()
{
int i=0;
char input[MAX];
char output[MAX];
//take user input and store it in our input string
while ((input[i] = getchar()) != EOF){
++i;
}
//put a null byte at the end of input[]
input[i+1] = '\0';
//examine line by line until end of string (null byte)
for (i=0; input[i] != '\0'; ++i){
if (input[i] == '\n'){
examine(input, i);
}
}
return 0;
}
void examine(char input[], int index)
{
//decrement through input[] until \n or start [0] is reached
int i=0;
for (i=0; ((input[index] != '\n') || (index > 0)); ++i){
--index;
}
//if line is bigger than 10 chars, print it
if (i>SIZE){
for (; input[index+1] != '\n'; ++index){
putchar(input[index]);
}
}
//otherwise, return
return;
}
rewrote it. was actually really easy. here is the code:
/*this program takes keyboard input from the user until EOF
and prints out their input excluding lines less than or equal to LINESIZE*/
#include <stdio.h>
#define MAX 2000
#define LINESIZE 10
void checkprint(char line[]);
int main()
{
char input[MAX];
char line[MAX];
int i, i2;
i2 = 0;
//take input until EOF (Ctrl + D)
for (i=0; (input[i]=getchar()) != EOF; ++i){
;
}
//add null byte to end of string
input[i+1] = '\0';
//basic formatting for aesthetics
putchar('\n');
//copy a line into line[] from input[] until NULL byte reached
for (i=0; input[i] != '\0'; ++i){
line[i2] = input[i];
++i2;
//if end of line, call checkprint
if (input[i] == '\n'){
checkprint(line);
i2=0;
}
}
return 0;
}
void checkprint(char line[])
{
int i;
//find the length of the line
for (i=0; line[i] != '\n'; ++i){
;
}
//if longer than LINESIZE, print it
if (i > LINESIZE){
putchar('\n');
for (i=0; line[i] != '\n'; ++i){
putchar(line[i]);
}
}
}
#include <stdio.h>
#define MAX 1000
#define SIZE 10
void examine(char input[], int index);
int main(void){
char input[MAX];
// char output[MAX];
int i, ch;
for(i=0; i< MAX - 1 && (ch = getchar()) != EOF; ++i)
input[i] = ch;
input[i] = '\0';
for (i=0; input[i] != '\0'; ++i){
if (input[i] == '\n'){
examine(input, i);
}
}
examine(input, i);//for '\0'
return 0;
}
void examine(char input[], int index){
int i;
if(index == 0) return ;
for (i=1; index - i >= 0 && input[index-i] != '\n'; ++i)
;
--i;
if (i > SIZE){
while(i>0)
putchar(input[index - i--]);
putchar('\n');
}
return;
}
buffer's size 11 version.
#include <stdio.h>
#define SIZE 10
void print(char ch){
static char buf[SIZE+1];
static index = 0, over = 0;
int i;
if(over){
putchar(ch);
if(ch == '\n')
over = 0;
return ;
}
if(ch == '\n'){
index = 0;
} else {
buf[index++] = ch;
if(index == SIZE + 1){
for(i=0;i<index;++i){
putchar(buf[i]);
}
index = 0;
over = 1;
}
}
}
int main(void){
int ch;
while((ch = getchar()) != EOF){
print(ch);
}
return 0;
}
//simple is to use the fgets