I have to C program called contains.c that takes two text strings as arguments and prints "true" followed by a newline if the second string is entirely contained within the first, or "false" followed by a newline otherwise.
For instance(in the command prompt)
$ ./contains 'I have a really bad feeling about this' 'bad feeling'
true
$ ./contains 'To be or not to be' 'That is the question'
false
$ ./contains 'I am the walrus' 'I am the walrus'
true
$ ./contains 'the walrus' 'I am the walrus'
false
$ ./contains 'kmjnhbvc45&^$bn' '.'
false
Here is my code so far.
#include <stdio.h>
#include <string.h>
int main( int argc, char* argv[])
{
int i;
int j;
int lenst1;
int lenst2;
int pos1;
int pos2;
if (lenst2>lenst1)
{
printf("false");
return 0;
}
for (j=0; j<lenst1;j++)
{
for (i=0; i<lenst2; i++)
{
if (st2[i]==st1[j]) //NOT SURE HOW TO DEFINE "st2" and "st1"
{
pos1=j;
pos2=i;
while (pos2<lenst2)
{
pos2++;
pos1++;
if (st2[i]==st1[j])
{
}
else
{
printf("false\n");
return 0;
}
printf("true\n");
return 0;
}
}
}
}
}
I am not entirely sure how many flaws I have in my code but Xcode claimed that st2 and st1 needs to be defined.
It would be greatly appreciated if someone could help me.
You forgot to read in your arguments.
char* st1 = argv[1];
char* st2 = argv[2];
lenst1 = strlen(st1);
lenst2 = strlen(st2);
You've had most of it right, but here are a few of the things you missed:
test the number of arguments:
if (argc != 3) {
printf("wrong number of arguments provided\n");
return -1;
}
capture your required variables:
int i, j, pos1, pos2;
char* st1 = argv[1];
char* st2 = argv[2];
int lenst1 = strlen(argv[1]);
int lenst2 = strlen(argv[2]);
Replace your if-else block with if st2[i] != st1[j]
Declare your main function to be named main
End your function with return 0;
Write printf("false\n"); to pass your last test case. (do you see why?)
build gcc contains.c -o contains
Related
I need to verify a String(string:89dree01) with regular expression ([a-zA-Z0-9]*) using if condition in C like so:
if(string=regex) {}
Could someone help me with this?
Below is the code snippet:
#include <regex.h>
#include <stdio.h>
int main()
{
regex_t * regex = "[a-zA-Z0-9]*";
int value;
value = regcomp(regex,"89dree01", 0);
if (value == 0) {
LOG("RegEx compiled successfully.");
}
else {
LOG("Compilation error.");
}
return 0;
}
You're not using the POSIX regexp library quite correctly.
Here's an example that checks whether arguments given on the command line match that regexp (slightly modified).
#include <regex.h>
#include <stdio.h>
int main(int argc, char **argv) {
regex_t regex;
if (regcomp(®ex, "^[a-zA-Z0-9]+$", REG_NOSUB | REG_EXTENDED)) {
return 1;
}
for (int i = 1; i < argc; i++) {
int status = regexec(®ex, argv[i], 0, NULL, 0);
printf("%s: %s\n", argv[i], status == REG_NOMATCH ? "no match" : "matched");
}
return 0;
}
~/Desktop $ gcc -o s s.c
~/Desktop $ ./s aaa bb0 00a11 ..--
aaa: matched
bb0: matched
00a11: matched
..--: no match
Edit:
Simply (if inefficiently) put, you can wrap this in a function:
int does_regexp_match(const char *string, const char *regexp) {
regex_t r;
if (regcomp(&r, regexp, REG_NOSUB | REG_EXTENDED)) {
return -1;
}
return regexec(&r, string, 0, NULL, 0) == 0 ? 1 : 0;
}
if(does_regexp_match("89dree01", "^[a-zA-Z0-9]+$") == 1) {
// it's a match
}
I got my code working to an extent, but I need some more help. If I needed to remove the word "an", from sentence: "I ate an apple whilst looking at an ape.", it only removes the first "an" and not the second, how do I repeat the loop so it deletes all "an"s? I need the final sentence, after the code has been ran, to be: "I ate apple whilst looking at ape.". That is the goal im trying to achieve
Sorry for not including the code.
Here it is:
#include "RemoveFromText.h"
#include <stdlib.h>
#include <string.h>
int findFirstSubstring(char textToChange[], char removeThis[])
{
int size = strlen(textToChange);
int subStringLength = strlen(removeThis);
for(int i=0; i<size; i++)
{
if(textToChange[i] == removeThis[0])
{
int j = 0;
while(textToChange[i+j] == removeThis[j])
{
j++;
if(j==subStringLength)
{
return i;
}
}
}
}
return -1;
}
void removeFromText( char textToChange[], char removeThis[])
{
int textLength = strlen(textToChange);
if(findFirstSubstring(textToChange, removeThis) >= 0)
{
int subStringIdx = findFirstSubstring(textToChange, removeThis);
int loopVariabele = 0;
for(loopVariabele = subStringIdx; loopVariabele<textLength; loopVariabele++)
{
textToChange[loopVariabele] = textToChange[loopVariabele + strlen(removeThis)];
}
}
}
Leveraging 'strstr', and 'memmove' standard "C" library functions
// Remove all occurences of 'source' from 'message'.
void removeAll(char *message, char *source)
{
int len = strlen(source) ;
for (char *x = message ; x=strstr(x, source) ; ) {
// Copy everything after 'source', including terminating nul.
memmove(x, x+len, strlen(x+len)+1) ;
} ;
}
Notes:
that solution that not properly address the trailing space(s) after a word. This can be addressed by chaning the 'memmove'.
Probably make sense to make the function return the number of substitutions, or some other meaningful result
I am new to C, and I have been going through the CS50 course to learn some basics. I have been trying to solve the challenge which requires you to make a simple password cracker, but I ran into a problem which prevents me from writing a function program: every time I call the crypt function in my for loop, it somehow breaks my password string that I am iterating through.
I have tried making a copy of the password string, and passing that as an argument to crypt; I have also tried moving the crypt call into a separate function and calling that from the loop (as well as the combination of the two)
#define _XOPEN_SOURCE
#include <unistd.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
string buildLetterDictionary();
int main(int argc, string argv[])
{
if (argc == 2)
{
printf("Two arguments, starting test...\n");
char password[2];
string letters = buildLetterDictionary();
for(int i = 0; i < 5; i++)
{
password[0] = letters[i];
password[1] = '\0';
printf("Password: %s\n", password);
string hashed = crypt(password, "50");
printf("\n%i\nOriginal: %s\nHashed: %s\n", i, password, hashed);
}
return 0;
}
else
{
printf("Usage: ./crack hash");
return 1;
}
}
string buildLetterDictionary()
{
char letters[27];
for(int i = 65; i < 91; i++)
{
letters[i-65] = i;
}
letters[26] = '\0';
string letter = letters;
return letter;
}
if I comment out the lines:
string hashed = crypt(password, "50");
printf("\n%i\nOriginal: %s\nHashed: %s\n", i, password, hashed);
The code works as expected, and produces the output:
A
B
C
D
E
But if I leave those lines in, the password is printed out as 'A' with the hash "50pe4e2XTIS/g" the first time, but every subsequent time is printed out as "" with the hash "50sXZPq5euCxs"
Please let me know what the underlying problem is, so that I may work towards resolving it! Thanks for any help in advance!
I am guessing here that cs50.h contains some definitions like a type alias from char * to string that the professor is giving you for simplicity.
If that is true, then buildLetterDictionary() cannot work, because you are doing:
char letters[27];
...
char * letter = letters;
return letter;
This means you are returning the address of a local variable, which will be destroyed as soon as you leave the function.
I'm building a program that takes input as if it is a bare MAC address and turn it into a binary string. I'm doing this on a embedded system so there is no STD. I have been trying something similar to this question but after 2 days I haven't achieved anything, I'm really bad with these kind of things.
What I wanted is output to be equal to goal, take this into consideration:
#include <stdio.h>
int main() {
const char* goal = "\xaa\xbb\xcc\xdd\xee\xff";
printf("Goal: %s\n", goal);
char* input = "aabbccddeeff";
printf("Input: %s\n", input);
char* output = NULL;
// Magic code here
if (output == goal) {
printf("Did work! Yay!");
} else {
printf("Did not work, keep trying");
}
}
Thanks, this is for a personal project and I really want to finish it
First, your comparison should use strcmp else it'll be always wrong.
Then, I would read the string 2-char by 2-char and convert each "digit" to its value (0-15), then compose the result with shifting
#include <stdio.h>
#include <string.h>
// helper function to convert a char 0-9 or a-f to its decimal value (0-16)
// if something else is passed returns 0...
int a2v(char c)
{
if ((c>='0')&&(c<='9'))
{
return c-'0';
}
if ((c>='a')&&(c<='f'))
{
return c-'a'+10;
}
else return 0;
}
int main() {
const char* goal = "\xaa\xbb\xcc\xdd\xee\xff";
printf("Goal: %s\n", goal);
const char* input = "aabbccddeeff";
int i;
char output[strlen(input)/2 + 1];
char *ptr = output;
for (i=0;i<strlen(input);i+=2)
{
*ptr++ = (a2v(input[i])<<4) + a2v(input[i]);
}
*ptr = '\0';
printf("Goal: %s\n", output);
if (strcmp(output,goal)==0) {
printf("Did work! Yay!");
} else {
printf("Did not work, keep trying");
}
}
Guys so I'm working on the web service assignment and I have the server dishing out random stuff and reading the uri but now i want to have the server run a different function depending on what it reads in the uri. I understand that we can do this with function pointers but i'm not exactly sure how to read char* and assign it to a function pointer and have it invoke that function.
Example of what I'm trying to do: http://pastebin.com/FadCVH0h
I could use a switch statement i believe but wondering if there's a better way.
For such a thing, you will need a table that maps char * strings to function pointers. The program segfaults when you assign a function pointer to string because technically, a function pointer is not a string.
Note: the following program is for demonstration purpose only. No bounds checking is involved, and it contains hard-coded values and magic numbers
Now:
void print1()
{
printf("here");
}
void print2()
{
printf("Hello world");
}
struct Table {
char ptr[100];
void (*funcptr)(void)
}table[100] = {
{"here", print1},
{"hw", helloWorld}
};
int main(int argc, char *argv[])
{
int i = 0;
for(i = 0; i < 2; i++){
if(!strcmp(argv[1],table[i].ptr) { table[i].funcptr(); return 0;}
}
return 0;
}
I'm gonna give you a quite simple example, that I think, is useful to understand how good can be functions pointers in C. (If for example you would like to make a shell)
For example if you had a struct like this:
typedef struct s_function_pointer
{
char* cmp_string;
int (*function)(char* line);
} t_function_pointer;
Then, you could set up a t_function_pointer array which you'll browse:
int ls_function(char* line)
{
// do whatever you want with your ls function to parse line
return 0;
}
int echo_function(char* line)
{
// do whatever you want with your echo function to parse line
return 0;
}
void treat_input(t_function_pointer* functions, char* line)
{
int counter;
int builtin_size;
builtin_size = 0;
counter = 0;
while (functions[counter].cmp_string != NULL)
{
builtin_size = strlen(functions[counter].cmp_string);
if (strncmp(functions[counter].cmp_string, line, builtin_size) == 0)
{
if (functions[counter].function(line + builtin_size) < 0)
printf("An error has occured\n");
}
counter = counter + 1;
}
}
int main(void)
{
t_function_pointer functions[] = {{"ls", &ls_function},
{"echo", &echo_function},
{NULL, NULL}};
// Of course i'm not gonna do the input treatment part, but just guess it was here, and you'd call treat_input with each line you receive.
treat_input(functions, "ls -laR");
treat_input(functions, "echo helloworld");
return 0;
}
Hope this helps !