Replicating the Strlen function in C [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have been working on my C assignment where I try to replicate strlen() function without actually using it. This is the code I have been trying to get working. However, somehow the main function does not reflect what's happenning in mystrlen() function. Can you please tell me why it does not work as the strlen() function?
#include <stdio.h>
int mystrlen(char *input_string) {
/* This function returns the length of the input string */
/* WRITE FUNCTION CODE HERE! */
char str1[50];
int abcd = 0;
scanf("%s", str1);
int m;
for(m=0; str1[m]; m++){
abcd ++;
}
return 0;
}
int main(int argc, char **argv) {
int length;
if (argc!=2) {
printf("Usage: strlen <input_string_with_no_space_inside_it>\n\n");
return 1;
}
length = mystrlen(argv[1]);
printf("The length is: %d characters.\n",length);
return 0;
}

This:
return 0;
should be:
return abcd;
That said, abcd is a terrible name for this variable, and the function makes little sense as a strlen() replacement. It doesn't touch its argument, and calls scanf() to read input from the user, which you really don't want a strlen() replacement to do.
Here's one way of writing it:
size_t mystrlen(const char *s)
{
size_t len = 0;
if(s != NULL)
{
while(*s != '\0')
{
++len;
++s;
}
}
return len;
}
Improvements include:
Proper size_t-typed return value (lengths are sizes, and cannot be negative) so size_t is proper and what the real strlen() uses.
Doesn't do any input-reading.
Uses the input argument.
Computes length and returns it.
It also handles being given NULL, as a bonus.
A matching main() that does what yours did could be:
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Usage: strlen <input_string_with_no_space_inside_it>\n\n");
return 1;
}
const size_t length = mystrlen(argv[1]);
printf("The length is: %zu characters.\n", length);
return 0;
}
This basically centers around the int-to-size_t change. Also note that in shells supporting quoting, you can run your program like this:
$ ./strlen "hello this is a string with spaces in it"
and it will pass that entire quoted string (sans quotes, of course) in argv[1].

First of all, you try to pass a string, and then you invoke scanf function in mystrlen. Delete this scanf.
Secondly, your function always returns zero instead of value of variable abcd.

Related

Pthread program printing different values on Codeblocks and Linux Kernel [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm working on the following pthread program that finds the number of substrings in string2 that are in string1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define NUM_THREADS 4
#define MAX 1024
int n1,n2,i;
char *s1,*s2;
FILE *fp;
char *substring(char *string, int position, int length);
void *occurrence();
int readf(FILE *fp)
{
if((fp=fopen("strings.txt", "r"))==NULL){
printf("ERROR: can't open strings.txt!\n");
return 0;
}
s1=(char *)malloc(sizeof(char)*MAX);
if(s1==NULL){
printf("ERROR: Out of memory!\n");
return -1;
}
s2=(char *)malloc(sizeof(char)*MAX);
if(s1==NULL){
printf("ERROR: Out of memory\n");
return -1;
}
/*read s1 s2 from the file*/
s1=fgets(s1, MAX, fp);
s2=fgets(s2, MAX, fp);
n1=strlen(s1); /*length of s1*/
n2=strlen(s2)-1; /*length of s2*/
if(s1==NULL || s2==NULL || n1<n2) /*when error exit*/
return -1;
return 0;
}
int main(void)
{
pthread_t tid;
pthread_create(&tid, NULL, occurrence, NULL);
pthread_join(tid, NULL);
exit(0);
}
char *substring(char *string, int position, int length)
{
char *pointer;
int c;
pointer = malloc(length+1);
if (pointer == NULL)
{
printf("Unable to allocate memory.\n");
exit(1);
}
for (c = 0 ; c < length ; c++)
{
*(pointer+c) = *(string+position-1);
string++;
}
*(pointer+c) = '\0';
return pointer;
}
void* occurrence()
{
char *string1;
char *string2;
char *new_str;
int counter=0;
readf(fp);
string1 = s1;
string2 = s2;
new_str = malloc(200);
for(i=1;i<=strlen(string1);i++)
{
new_str = substring(string1,i,strlen(string2));
if(strcmp(new_str, string2)==0)
{
counter++;
}
}
printf("The number of substrings is: %d \n",counter);
return 0;
}
When i compile it on codeblocks, it prints the correct number of substrings. However, when I compile it on the Linux kernel, it always prints 1 as the number of substrings, even though there's more than one. For example, a strings.txt file that has abdeabjhab in the first line and ab in the 2nd line should print 3, since there's 3 instances of ab in the first line. My Linux kernel prints 1. Is there a specific way I'm supposed to compile it for it to print the right value? I'm current using gcc -pthread substring.c -o substrings to compile and ./substrings to execute it.
There are several issues here.
First the signature for occurrence is wrong; the thread function should take a void* as argument and return a void* as required by the pthread_create API. So it should be:
void *occurrence(void*);
and
void *occurrence(void arg*) {
...
return NULL;
}
There are several other questionable things in your code.
You are passing the global variable fp to readf() function. Why do you need to pass a global variable?
You are reading only the first two lines. What if the file contains many more lines?
Why do you have s1 and s2 as global? You could easily rewrite it to pass from readf back to occurrence. Or occurrence could pass varibles that readf writes into.
You don't close the file with fclose.
You create just one thread to do the work and the main thread just waits for it. So there's no real need for threads here. You might as well let the main thread do the work and not bother with threads.
Why do you subtract here?: n2=strlen(s2)-1; /*length of s2*/.
What if the length of s1 and s2 are the same? Is "abc" not a substring of "abc"?
Your actual of problem of getting incorrect substring count is probably because fgets() reads in the newline character and thus the substring match fails.
To remove newlines, you can do in your readf function after reading s1 and s2:
char *p = strchr(s1, '\n');
if (p) *p = 0;
p = strchr(s2, '\n');
if (p) *p = 0;
This should fix the immediate problem. But there's a lot you could improve.

How to check if command line parameters are integers

First of all, I am talking about old-fashioned ANSI-C (I mean the ANSI standard and no C99 or newer) compiled with gcc. I am only allowed to use the libraries that can be seen below in the code.
My problem is I have a program that is called in the following way on the Terminal:
program < integer_1 integer_2
While I have been able to figure out how to check for the number of arguments, I'm stuck on checking if those are integers.
If the program is called like this:
program < 1 -13
it should run without complaining but if it is run like this:
program < s 7
it should throw out an error.
Whatever I have tried so far has been utter rubbish. The best thing I have managed so far has been an error message if the second number has been a character. None of my tries has been able to deal with more than one digit but I have figured out why that is.
The problem is that I haven't used command line / terminal arguments with any programming language i now (C++, Java). I would really appreciate it if someone could show me how check for correct input as frankly I am out of ideas.
Am I correct that if I want to deal with numbers bigger than 9, I have to iterate through argv starting from index 2 until I find a space?
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(int arc, char *argv[])
{
if(arc != 3)
{
printf("Error: You have entered %d arguments, but two were expected!", arc - 1);
return -1;
}
return 0;
}
The easiest way out is to iterate over the argv[n]s and pass one by one to them to strtol() or similar. Then, check for the error and make the decision. To quote the man page, (emphasis mine)
long int strtol(const char *nptr, char **endptr, int base);
[...]
If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.
That said, program < integer_1 integer_2 is not exactly the way to pass the command-line arguments. If you want to pass the values arguments as command-line arguments, you shall lose the redirection operator and work with argc and argv[n]s directly..
Best way is to create a function for checking whether it is number or not.if the below function returns true then use atoi(argv[]) to convert them to integers to use it further.
bool isNumber(char number[])
{
int i = 0;
//checking for negative numbers
if (number[0] == '-')
i = 1;
for (; number[i] != 0; i++)
{
//if (number[i] > '9' || number[i] < '0')
if (!isdigit(number[i]))
return false;
}
return true;
}
Just a comment: not an answer
If you are going to use
program < arg1 arg2
You will not see arg1 or arg2 in the main parameters. arg1 is typically a filename or device which contain data which will be read by the program. I don't know if the program will even be able to access arg2. If you wish to pick up arg1 arg2 etc, lose the <
program arg1 arg2
You can try something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int check_cmd_args(char const *str[], int numargs);
int
main(int argc, char const *argv[]) {
if (argc < 2) {
printf("Not enough command line arguements entered\n");
exit(EXIT_FAILURE);
}
if (check_cmd_args(argv, argc)) {
printf("All Command line arguements are integers\n");
} else {
printf("Error, non-integer command line arguement entered\n");
}
return 0;
}
int
check_cmd_args(char const *str[], int numargs) {
int n, i = 0;
for (n = 1; n < numargs; n++) {
if (str[n][0] == '-') {
i = 1;
}
for (; str[n][i]; i++) {
if (!isdigit(str[n][i])) {
return 0;
}
}
}
return 1;
}

I have an issue printing the length of a string from the command line without using strlen? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Hello there basically what my program is meant to do is when the user enters a string from the command line it will print the word and the size.
E.g
./commandline hello world
Output:
hello
world
2
What I'm trying to do is add a method that will print its length without using strlen so output should be length 10 for the example above.
This is my code bare in mind i am new to c.
int main(int args, char *argv[]){
for(int i =1; i <args; i++){
printf("%s\n", argv[i]);
size_t(argv[i]);
}
printf("%d\n", args -1);
}
size_t string_length( char *argv[]){
int length = 0;
while(argv[length]!='\0')
{
length++;
printf("%i\n", length);
}
return 0;
}
My program does not print length only prints the string entered and the size.
for(int i =1; i <args; i++){
printf("%s\n", argv[i]);
size_t(argv[i]);
}
printf("%d\n", args -1);
Here you're not calling your function anywhere. Your program just prints the arguments and the number of them. size_t(argv[i]); merely casts argv[i] to a type called size_t. Certainly, that's not what you want. Replace it with string_length(argv[i]);. Note that you'd better change the type of the first argument of this function.
What's more, you should return length in your string_length function.
size_t string_length( char *arg){
size_t length = 0;
while(arg[length])
{
length++;
}
return length;
}
try this
size_t string_length( char *argv[]);
int main(int args, char *argv[]){
for(int i =1; i <args; i++){
printf("%s\n", argv[i]);
string_length(argv[i]);
}
printf("%d\n", args -1);
}
size_t string_length( char *argv[]){
int length = 0;
while(argv[length]!='\0')
{
length++;
}
printf("%i\n", length);
return length;
}

Returning a string [char pointer] from a function [duplicate]

This question already has answers here:
Return char[]/string from a function [duplicate]
(5 answers)
Closed 8 years ago.
I am writing a program that returns a string from stdin, but i am getting warning that it returns an adress of local wariable. How can i return the string?
thanks in advance
#include <stdio.h>
char* readLine()
{
int i;
char input[1024];
for(i=0;i<1024;i++)
{
input[i]=fgetc(stdin);
if(input[i]=='\n')
{
break;
}
}
return input;
}
int main()
{
printf("%s",readLine());
return 0;
}
This should work for you:
You can pass input from main as reference:
#include <stdio.h>
char * readLine(char * input, int length) {
int i;
for(i = 0; i < length; i++) {
input[i] = fgetc(stdin);
input[length] = '\0';
if(input[i] == '\n')
break;
}
return input;
}
int main() {
int length = 1024;
char input[length+1];
printf("%s", readLine(input, length));
return 0;
}
Try to do something like that instead:
#include <stdio.h>
char* readLine()
{
int i;
char *input;
if ((input = malloc(sizeof(char) * 1024)) == NULL)
return (NULL);
for(i=0;i<1024;i++)
{
input[i]=fgetc(stdin);
if(input[i]=='\n')
{
input[i] = '\0';
break;
}
}
return input;
}
int main()
{
char *str;
if (str = readLine()) != NULL) {
printf("%s\n", str);
free(str);
}
return 0;
}
}
There is nothing wrong here - that is just a WARNING because usually it is a common mistake of new programmers. I used to run into problems with this usage all the time.
The first thing... this "string" is not null-terminated. You'll want to put at the end of that function something like *(input + i) = '\0'; and make either the array size 1025 or the condition i < 1023 (so that the null character isn't assigned beyond the end of the buffer), because at the moment using this array in a function that expects null termination will cause it to possibly continue past the end of the array, resulting in a memory access violation. Alternately, you could use memset(input,0,1024);, just still make sure that the condition is something like i < 1023 so that the standard input you receive doesn't end up writing all the way to the last null character in the array.
The other problem is that this memory is local, as in it "belongs" to this function. And for the usage you have here, it is probably just fine to use the same memory... if you plan to call the function, do something with the result, and then call the function again, do something with the result... But if you want to keep what's given to you by it, you'll have to either (1) copy the string to another buffer that isn't going to be written to again when the function is called in the future, or (2) make the function allocate a new buffer each time it runs, and then be sure to delete that memory when you're done with it. For example, instead of char input [1024]; (which by the way would have the same pointer for the life of the program, so it's not really necessary to return it each time) you could write char* input = malloc(1024); and later, when the caller is done with the string, you should free(input);. (Of course, the name might not be input in this case since you would probably not want to free the memory in the function whose purpose is to allocate it.)
I will edit this later with code showing changes.

passing tokens from array to strcmp

What I am trying to do is to break the user input in parts with whitespace as a delimiter, copy the parts into the array (tokenAr) and compare the tokenAr[0] (the first part) if it is equal to sHistory. if they are equal, check the value of tokenAr[1] if it is "1", "2" etc, to execute the corresponding command that is entered in the history array. This is what i have tried to far and it crashes. I am using TCC on Windows x64.
EDIT: I forgot to mention that I began learning C, just two days ago.
EDIT2: I run the program in a debugger and it has raised an Acces Violation(Segmentation Fault) in line if(strcmp(tokenArPtr[0],sHistory)==0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i=1; int j=1; int k=0;
char history[100][100] = {0};
char sKey[] = "exit";
char sInput[100];
char sHistory[]="history";
do
{
//gather user input
printf ("hshell> ");
fgets (sInput, 100, stdin);
strcpy(history[i],sInput);
i++;
//END_gather user input
//Tokenizing
char delims[] = " ";
char *tokenArPtr[5];
char *result = NULL;
result = strtok(sInput, delims);
tokenArPtr[0] = result;
while (result!=NULL)
{
puts(result);
result= strtok(NULL, delims);
tokenArPtr[k+1] = result;
puts(tokenArPtr[k]);
puts("=====");
k++;
}
k=0;
/*
//END_Tokenizing
if(strcmp(tokenArPtr[0],sHistory)==0)
{
for(j=1;j<i;j++)
{
printf("%d. %s \n",j,history[j]);
}
}
else if (strcmp (sKey,tokenArPtr[0]) != 0)
{
printf("\nCommand not found \n");
}*/
}while (strcmp (sKey,sInput) != 0);
return 0;
}
EDIT 3: I used the result variable instead of the tokenArPtr directly, but when debugging, I noticed that the values of the array are not being updated.
Which type does strtok return? char *. What is the type of tokenAr[k]? char. What type does strcmp expect as input? char * and char *. What is the type of tokenAr[0]? char.
See a problem? You should. The * is pretty significant.
Assuming tokenAr is declared like char *tokenAr[2];, how many char * values can tokenAr store? What happens when k exceeds 2? You need to ensure you don't overflow your tokenAr array.
history is uninitialised. Using an uninitialised variable is undefined behaviour. I suggest initialising it, like this: char history[100][100] = { 0 };
Which book are you reading?
While tokenizing, the loop will never end because the test is on the variable "result" that will never change... So you're finally going to a buffer overflow with "tokenAr"... Modify your code to test "tokenAr".
Edit: And tokenAR should be an array... (I don't know how it can compile...)
There are many problems... First of all you should include string.h which will show you some errors in compilation.
I believe that the main problem is here:
char tokenAr[2];
result = strtok(sInput, delims);
while (result!=NULL)
{
tokenAr[k] = strtok(NULL, delims);
k++;
}
tokenAr should be an array of pointers, not chars. And are you sure that k will never exceed 2? An assertion would help debugging.

Resources