I have to make a program where it reads from a file and then enter a string with letter, symbols and numbers and then it have to print only the letters in reverse way, now i have to use recursive function for the letters and I must use prints in the main function not in the recursive function
for example if the file the program reads from has :
input :#$f^t&^$q&%Y &^q~!$n!#n&#g%$#J*/&^(%
output : Hello Word
NOTE : ALL that wouldn't happen to me if I haven't been asked to use print only in the main function,
and also when there is a symbol or number it avoids the if statements and the function
The main question is : how I can use printf in the main function and be used properly where it should print reverse way and the symbols shouldn't be shown, in the function itself those thing works btw
here is my code :
int decode(char x[])
{
if (!x[0])
{
return 1;
}
decode(x+1);
if ( x[0]== ' ' || x[0]== ',' ||((x[0]>='A' && x[0]<='Z') || (x[0]>='a' && x[0]<='z' ) ) )
{ // here each string i enter at makes it backward by -2 for example c-->a
switch(x[0])
{
case 'A':
x[0]='Y';
break;
case 'B':
x[0]='Z';
break;
case 'a':
x[0]='y';
break;
case 'b':
x[0]='z';
break;
case ',':
x[0]=',';
break;
case ' ':
x[0]=' ';
break;
default:
x[0]-=2;
break;
}
else
return ;
}```
here is the main function
```int main()
{
FILE *fp;
char str[1000];
char filename[100];
scanf("%s",filename);// here i have to enter the file name to get all the strings from another file //
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Could not open file %s",filename);
return 1;
}
while (fgets(str,1000, fp) != NULL)// the problem start here it get the strings input
{
decode(str);
printf("%s",str);// and in the printf it prints everything where it should only print the letters in revrse way
fclose(fp);
}```
Just create a long enough char array, initialize all members with 0 and use it as a string variable (or use calloc). Use sprintf or sputc within your loop to "print" into that string variable and just use one regular printf at the end of your program.
Related
I want to use the scanf function to get inputs that are either two numbers, or one number from the user and put them in an array. However, I'm not sure how I can use this same function to get inputs for both one element of the array, and also two elements of the array.
i.e. if the user enters, 9 0, it should be able to successfully store that in an array and move onto new code, or if the user enters something like 1, it should also be able to successfully store that in the array, and move onto new code.
I've tried putting the scanf into a while loop:
int scanned_array[2] = {};
int element = 0;
while(scanf("%d", &scanned_array[element]) {
//... more code here which will have different functions depending on the input...
element++;
}
How can I successfully do this while only using the scanf function, while loops, arrays and if statements?
**Edit: Just wondering, if I changed my code according to what was suggested below by #nmgari, how could I go to if(num == 1) { ... without having to press ctrl+d?
Thanks for reading!
If i understand correctly you want to call scanf() once and read either one number or two.
Something like this should work:
int scanned_array[2];
int num_input = 0;
num_input = scanf("%d %d", &scanned_array[0], &scanned_array[1]);
if(num_input == 1)
{
//Do something
}
else if (num_input == 2)
{
//Do somethine else
}
Anyway, you need to consider that the second element in the array may never get a value.
You should read the input line with fgets() and parse it with sscanf():
char buf[120];
if (fgets(buf, sizeof buf, stdin)) {
int i1, i2;
switch (sscanf(buf, "%d%d", &i1, &i2)) {
case 2:
/* the user entered 2 integers */
handle_2_integers(i1, i2);
break;
case 1:
/* the user entered a single integer */
handle_1_integer(i1);
break;
default:
/* the user did not enter a number */
handle_invalid_input();
break;
}
} else {
/* the input stream is at end of file or encountered an error */
handle_end_of_file();
}
If you run scanf("%d%d", &i1, &i2), scanf() will keep reading the input stream until either end of file, or 2 integers have been read or an character has been input that cannot be converted to an integer, possibly reading multiples lines of input for one of these cases to occur. If you want to handle a single line of input, you cannot use scanf() directly.
A separate scan for whitespace could be used.
The scanset %1[ \t\r\n] will capture whitespace character by character. The loop will exit on non-whitespace or a newline.
Then try to scan an integer.
#include <stdio.h>
#include <stdlib.h>
int main ( void) {
char space[2] = "";
int scanned = 0;
int scanned_array[2] = { 0};
while ( 1) {
while ( 1 == scanf ( "%1[ \t\r\n]", space)) {
if ( '\n' == space[0]) {
break;
}
}
if ( '\n' == space[0]) {
break;
}
if ( 1 != scanf ( "%d", &scanned_array[scanned])) {
fprintf ( stderr, "bad input\n");
return 1;
}
++scanned;
if ( 2 <= scanned) {
break;
}
}
for ( int each = 0; each < scanned; ++each) {
printf ( "%d\n", scanned_array[each]);
}
return 0;
}
I want the problem to recognize words in that order: XYZ1111*** no matter how many '1' or '*' but it must have at least one '1' and XYZ must be in that exact order and always be included for the string to be valid. It must read from a file that I have written a lot of these words such as XYZ1, XYZ1111*, 1111* and print ok if the word meets the restrictions. When I run my program it just takes the name of the file and then does nothing. Here's my code:
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[]) {
FILE *input;
char c;
if(argc >2) {
printf("Too much arguments");
exit(1);
} else if(argc==2) {
input=fopen(argv[1],"r");
if(input==NULL) {
printf("Unable to find file ");
exit(1);
}
} else {
input=stdin;
c=fgetc(input);
while (!feof(input)) {
if (c=='x') {
int state=1;
while(1) {
switch(state) {
case 1:
c=fgetc(input);
if (c=='Y')
state=2;
break;
case 2:
c=fgetc(input);
if(c=='Z')
state=3;
break;
case 3:
c=fgetc(input);
if (c==1)
state=4;
break;
case 4:
if (c=='1')
state=4;
else if(c=='*')
state=5;
else if(c=='\n' || c=='\t' || c==' ')
printf("ok");
break;
case 5:
if (c=='*')
state=5;
else if(c=='\n' || c=='\t' || c==' ')
printf("ok");
break;
} // end of switch
} // end of while(1)
} // end of if(c=='x')
} // end of while(!feof(input))
} // end of else
printf("bgika");
} // end of main
I think your approach is wrong. As I have understood from your explanation, the string must start with "XYZ1" and after this you may have no matter how many 1 (just 1, not other characters). So, it would be very simple to check for the first part using strncmp, and then check if remaining characters are all 1.
c=fgetc(input);
if(strncmp(c, "XYZ1", 4) == 0){
//check if remaining characters are 1
}else{
//the string does not match
}
Also, while(!feof(input)) is discouraged: Why is “while ( !feof (file) )” always wrong?
while(1)
This is your problem.
Within this while loop, you have a switch statement full of break;s. These break;s will only get you out of the switch and will not get you out of the while loop. You have no way of getting out of this loop.
I have no earthly idea of what exactly the program is supposed to do (and I just spent 5 minutes fixing the formatting), so I can't currently make a recommended fix, but this is the problem.
Per #woolstar's comment, the inner while loop is unnecessary. The while(!feof(input)) can take care of repeatedly calling the switch statement. You will probably need to move the int state = 1; outside of this outer while loop however.
I'd like to be able to compare a character on stdin with a characters of my specification. The purpose of this is to filter out every other input as wrong, while maintaining only the specified single chars as commands. Like on stdin "nn" or "qddaw" -> wrong go again but "n" make something useful.
Here is what I have in mind "code-wise":
if (input does not contain 'c' or 's' or 'q' or 'n') {
printf("some kind of error");
}
Well I tried to create an array with specified characters like array[] = {'a', 'b', 'c'} so I could be able to compare it with a string on the stdin with function strncmp.. like
char c[256];
scanf("%s", c)
if (strncmp(array, c, 1) != 0) printf("error");
but it doesn't seem to work. Any suggestions?
Edit1: Here is actual piece of code:
char c[256];
char* s = "nsrld";
char* quiter = "q";
do
{
printf(">");
scanf("%s", c);
if (only when there is no 'n' or 's' or other char from char* s on input)
{
errorHandle(ERROR_WRONG_CMD);
}
scanf("%*[^\n]"); scanf("%*c");
} while (strcmp(c,quiter) != 0);
as you can see I handled the 'q' thing quite well, but multiple chars are pain in the ass. Thanks for any advice.
Edit 2: or in other words I need a function which will compare input with a set of given characters and only if there is one OR another (like 'q' or 's' the function will pass (but not if there are characters together like 'qs')
I didn't make myself clear enough. What I need is input "type what ever you want" like "wwqwqe" and do the error unless the input is just 'c' or just 's' (and a few more).
char usersInput[200] = ""; /* A buffer to hold the input values */
char *result = gets(usersInput); /* Fill the buffer from stdin */
if (result != NULL) /* If we got something */
{
if (strlen(usersInput) == 1) /* the input must be exactly 1 character */
{
char ch = usersInput[0];
if (strchr(ch, "csqn") == NULL) /* It must be a valid values */
{
printf("Evil Bad Character <%c>\n", ch);
}
else
{
/* Do stuff with the known valid input value ch */
}
}
else
{
puts("The input value must be exactly 1 character\n");
puts("and must be 'c', 's', 'q' or 'n'");
}
}
else
{
puts("EOF or ERROR while reading stdin\n");
}
This should do the job.
One warning. gets is not smart enough to know that usersInput is 200 characters long.
It will gleefully let you type in 201 characters or more, which overwrites other characters in memory. That sort of thing can lead to hard-to-find bugs.
int ch = getchar();
if (ch != EOF)
{
if (strchr("csqn", ch) == NULL)
{
printf("Evil Bad Character <%c> in Hex %02X\n", ch, ch);
}
else
{
/* Do stuff with ch */
}
}
else
{
printf("EOF on input\n");
}
char c = getchar();
switch (c) {
case 'c':
case 's':
case 'q':
case 'n':
do_something();
break;
default:
print_error();
};
The above code should work. I don't know why your if statement wasn't working. Generally a switch works well in this type of scenario too.
Your first solution should work. If that's the exact same code you posted - then your problem might because the printf needs a newline at the end to flush to console.
I have thought the string as sets... So if the intersection of them is the void set then we will fail -> printf("Error")... otherwise the output is none...
#include <stdio.h>
#include <string.h>
int intersection(char* source, char* search)
{
int i,j;
for(i = 0; i < strlen(search); i++)
if(strchr(source,search[i]))j++;
if(j != strlen(search))return 0;
else return 1;
}
int main()
{
char *letters = "eo";
char *p = "hello";
int e = intersection(p,letters);
if(e==1)puts("Non Void");
else puts("Void");
}
While it looks as if you've got a solution, it might be worth mentioning that what you're asking for doesn't sound as if it's all that far away from the standard 'getopt' functionality... See http://www.gnu.org/software/libc/manual/html_node/Getopt.html for example.
This worked for me:
char c[256];
char* s = "nqsrld";
char* quiter = "q";
do
{
printf(">");
scanf("%s", c);
if ((strpbrk(s, c) == 0) || (strlen(c) >= 2))
{
errorHandle(ERROR_WRONG_CMD);
}
scanf("%*[^\n]"); scanf("%*c");
} while (strcmp(c,quiter) != 0);
Thanks to everyone for their help.
Write a function
int IsGood(int c)
{
if(c=='a'||c=='b'||c=='c')
return 1;
else
return 0;
}
I want the put a sub-option in a string so that I can use it as a file name for reading a file:
char *nvalue = NULL;
char *dvalue = NULL;
char *input = NULL;
char inputfilename[] = "\"";
int ar;
int main(int argc, char *argv[])
{
while ((ar = getopt(argc, argv, "hn:d:i:")) != -1)
switch (ar)
{
case 'h':
printf("something");
break; /* added */
case 'n':
nvalue = optarg;
if (isdigit(nvalue))
stop = atoi(nvalue);
else
printf("something\n");
break; /* added */
case 'd':
dvalue = optarg;
if (!strcmp(dvalue, "FCFS") || !strcmp(dvalue, "SSTF") ||
!strcmp(dvalue, "C-SCAN") || !strcmp(dvalue, "LOOK"))
;
else
printf("Invalid type of disk scheduling policy entered.\n");
break; /* added */
case 'i':
input = optarg;
strcpy(inputfilename, optarg);
printf("Filename :%s\n", inputfilename);
break;
}
/* ... */
}
So on the command line if I input:
./foobar -i hello
then I should be able to read the file with:
FILE *file = fopen(inputfilename, "r" );
any suggestions? answers?
thanks!
There are a number of problems with your code. I'm ignoring the absence of header files (assuming your code uses the correct ones so all functions have a prototype in scope before use). I'm also reformatting your code ruthlessly but without further comment.
char *nvalue = NULL;
char *dvalue = NULL;
char *input = NULL;
char inputfilename[] = "\"";
This allocated an array of two bytes as inputfilename. I hate to think what's going to happen when you use it.
int ar;
There is every reason why this variable should be local to the main function and no reason visible for it to be a global variable. Unless you have a header declaring them, the other variables should also be static - assuming that you need to access their values outside of main() without a convenient way to pass them as locals. Avoid global variables whenever possible.
int main(int argc, char *argv[])
{
while ((ar = getopt(argc, argv, "hn:d:i:")) != -1)
{
switch (ar)
{
case 'h':
printf("something");
Oops; no break, so the code drops through to the case 'n': code. C is not Pascal.
case 'n':
nvalue = optarg;
if (isdigit(nvalue))
stop = atoi(nvalue);
You haven't shown a declaration for stop. Unless you really need the string, you can do without nvalue, avoiding a global variable, which is always desirable.
else
printf("something\n");
Another missing break; I'm not going to point it out again.
case 'd':
dvalue = optarg;
if (strcmp(dvalue, "FCFS") == 0 ||
strcmp(dvalue, "SSTF") == 0 ||
strcmp(dvalue, "C-SCAN") == 0 ||
strcmp(dvalue, "LOOK" == 0)
{
I'd suggest a comment such as /* Nothing - dvalue is OK */. Or inverting the condition using De Morgan's theorem:
if (strcmp(dvalue, "FCFS") != 0 &&
strcmp(dvalue, "SSTF") != 0 &&
strcmp(dvalue, "C-SCAN") != 0 &&
strcmp(dvalue, "LOOK" != 0)
You might even decide to encapsulate that test into a function which tests the value against each element of an array of codes.
}
else
printf("Invalid type of disk scheduling policy entered.\n");
It would be courteous to provide a list of the acceptable values - which suddenly becomes another reason for having an array of valid values which you can use to generate the list. Very often, error messages should be reported on stderr (using fprintf()) rather than stdout.
case 'i':
input = optarg;
This assignment is sufficient.
strcpy(inputfilename, optarg);
Unless the user types a single-character file name, you've just overflowed the inputfilename array. You really didn't need to make a copy of the argument unless you're going to modify the name (for example, to add or change the extension on the name).
//strcat(inputfilename,"\"");
printf("Filename :%s\n", inputfilename);
}
You didn't include a default clause. Since ar will be assigned the value ? when the user-provided option is not recognized, this is normally your cue to provide a simple usage message and exit.
I'm writing a program that compares two files character by character. The function to compare each file returns a value dependant on the condition of the files.
the function returns 0 when both files are the same, -1 if both files match but the first file ends before the second, -2 if both files match but the second file ends before the first, and a positive int indicating which character the files differ at.
#include <stdio.h>
#include <string.h>
#define CMP_EQUAL 0
#define CMP_EOF_FIRST -1
#define CMP_EOF_SECOND -2
int char_cmp(FILE *fp1, FILE *fp2);
int main(void)
{
FILE *fp1;
FILE *fp2;
fp1 = fopen("input1.txt", "rb+");
fp2 = fopen("input2.txt", "rb+");
switch(char_cmp(fp1, fp2))
{
case CMP_EQUAL:
printf("The Files are equal");
break;
case CMP_EOF_FIRST:
printf("EOF on a.txt");
break;
case CMP_EOF_SECOND:
printf("EOF on t.txt");
break;
default:
printf("files differ: char %d\n", char_cmp(fp1, fp2));
break;
}
if(fclose(fp1) != 0)
{
perror("fclose");
/*other error handling*/
}
if(fclose(fp2) != 0)
{
perror("fclose");
/*other error handling*/
}
return 0;
}
int char_cmp(FILE *fp1, FILE *fp2)
{
int c, d;
size_t byte = 0;
int same = 1;
do
{
byte++;
}while((c = fgetc(fp1)) == (d = fgetc(fp2)));
if(c == EOF && d != EOF)
{
return CMP_EOF_FIRST;
}
if(d == EOF && c != EOF)
{
return CMP_EOF_SECOND;
}
if(c != d)
{
return byte;
}
return CMP_EQUAL;
}
I was wondering how i would break out of the do loop after checking if all the characters match in each file. Because when i have tried, It breaks the moment it finds a character that is the same and does not check the rest of it.
Also i've encourtered this weird bug where if one file contains:
dee
and the second one contains
ae
it gives me a weird return value and was wondering why is that so?
thanks for any help in advance
You call char_cmp(fp1, fp2)) twice - once in the switch statement, and the second time in the default condition. The second time it returns you the second char position in which they differ (or something another, really unexpected :)
Change it to
int k = char_cmp(fp1, fp2));
and use k in these both places:
switch( k )
...
printf("files differ: char %d\n", k);
EDIT: The infinite loop in case of equal files happens because in this condition:
(c = fgetc(fp1)) == (d = fgetc(fp2))
c and d become forever equal to EOF from some moment. Change it to
(c = fgetc(fp1)) == (d = fgetc(fp2) && c != EOF
and everything is ok.
You are calling char_cmp() multiple times. The second time round, in the printf() call, returns a different value from the first call because the file pointers have been used.
Call char_cmp() once and store the returned value in a local.
cmp = char_cmp(fp1, fp2);
switch(cmp)
{
case CMP_EQUAL:
printf("The Files are equal");
break;
case CMP_EOF_FIRST:
printf("EOF on a.txt");
break;
case CMP_EOF_SECOND:
printf("EOF on t.txt");
break;
default:
printf("files differ: char %d\n", cmp);
break;
}
I don't know whether the rest of your logical is correct or not.
Actually, your logic is not correct. It enters an infinite loop when presented with identical files. I'm sure you'll be able to track down the problem!
When both reach EOF at the same time, the while condition is true and you start looping over and over, since EOF == EOF.
I suggest you to try to be less "short" at the beginning.