Only main() function executing in C - c

Starting to learn C. The main function is executing fine, but the program finishes running without ever executing the second function. I feel like I'm making a mistake here in the for loop in main.
int check_key_length(int count);
int main(void)
{
char key[20];
int count = 0;
printf("Enter key: ");
scanf("%s", key);
for(int i = 0; i < strlen(key); i++)
{
if (key[i] != ' ')
count++;
}
printf("Total number of characters in a string: %d", count);
}
int check_key_length(int count)
{
int set_amount = 26;
if (count < set_amount)
printf("Error: Your key is too short! Please input 26 chars\n");
else if (count > set_amount)
printf("Error: Your key is too long! Please input 26 chars\n");
else
string message = get_string("Enter string to encrypt: ");
return 0;
}

You forward declared your function, provided a definition for it, but you need to call the function in your main for your machine to execute it, something like this calls your function as expected
#include <stdio.h>
#include <string.h>
int check_key_length(int count);
int main(void)
{
char key[27];
int count = 0;
int strLength;
do {
printf("Enter key: ");
scanf("%s", key);
strLength = strlen(key);
} while(check_key_length(strLength) != 0);
for(int i = 0; i < strLength; i++)
{
if (key[i] != ' ')
{
count++;
}
}
printf("Total number of characters in a string: %d\n", count);
return 0;
}
int check_key_length(int count)
{
int set_amount = 26;
if (count < set_amount)
{
printf("Error: Your key is too short! Please input 26 chars\n");
return -1;
}
else if (count > set_amount)
{
printf("Error: Your key is too long! Please input 26 chars\n");
return -2;
}
else
{
return 0;
}
}
Note I had to modify the code a bit for it to build without any warning or error, I probably changed the behavior in a way you're not expecting so check my code before pasting it in

Related

How to use a code with arrays and strings?

Here are the instructions I was given:
If the command word is find, read an additional integer and search the data set for that integer.
If the command word is print, print the array
Any other command word is an error.
No command word will be longer than 20 characters.
After reading the n+1 values, there will be one more integer (k) read from the keyboard.
Search the array for the value k. If found, print the location where k was found. (1 = data value, n = last data value).
If k is not found, print not found. This is not an error.
If there are more than one value k in the data, only print the location of the first one.
#include <stdio.h>
int main (void) {
int n;
scanf ("%d", &n);
if (n < 1) {
printf ("Error: one or more values must be provided.\n");
return 1;
}
int x [n];
int a;
a = 0;
while (a < n) {
scanf ("%d", x [a]);
a = a + 1;
}
int k;
scanf ("%d", &k);
int i;
i = 0;
while (i <= n-1) {
if (x[i] == k) {
break;
}
i = i + 1;
}
if (i < n) {
printf ("%d\n", k+1);
} else {
printf ("not found\n");
}
printf ("Error: invalid command\n");
return 0;
}
Suggested Strategy:
After reading the array data, read a string.
If the string is find, read integer k and perform a search.
If the string is print, do not read k, just print the data in the array.
If the string is not find or print, handle the error.
Shai'Tavia, I hope my answer will help you see how you may make your code work. You've got the first part down, but you will need to compare the command string given by the user to then make a decision on what to do next.
#include <stdio.h>
#include <string.h>
#define ARRAYLENGTH 8
void printArray(int *array, int length)
{
for (int i = 0; i < length; i++)
printf("%d ", array[i]);
printf("\n");
}
void search(int *array, int key)
{
int flag = 0;
for (int i = 0; i < ARRAYLENGTH; i++)
{
if (array[i] == key && flag == 0)
{
printf("found %d at index: %d\n", key, i);
flag = 1;
}
}
if (flag == 0)
printf("not found\n");
}
int main(void)
{
char command[20];
int indx = 0;
int array[] = {1, 4, 6, 8, 43, 61, 34, 2};
int n, flag = 0;
printf("How many times will we run?");
scanf("%d", &n);
if (n < 1)
{
printf("Error: one or more values must be provided.\n");
return 1;
}
do
{
printf("Enter the command word:");
scanf("%s", command);
if (strcmp(command, "find") == 0)
{
scanf("%d", &n);
search(array, n);
}
else if (strcmp(command, "print") == 0)
printArray(array, ARRAYLENGTH);
else
printf("Command not found\n");
} while (--n > 0);
printf("What is your final interger?");
scanf("%d", &n);
search(array, n);
return 0;
}

Trying to remove substring from string in C, keep failing

I know this question has been asked many times before but I simply cannot get my head around what I am doing wrong. Everytime I make some progress I get a new error. The code I am using is really basic because I am a newbie and our professor requires the usage of scanf and gets. This is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
int identify(char[], char[]);
int remove(char[], char[], int);
int scan(choice)
{
while(choice < 0 || choice > 7)
{
printf("Invalid input, choose again\n");
scanf("%d", &choice);
}
return choice;
}
int main()
{
char sentence[MAX_SIZE], word[MAX_SIZE];
int choice, i, j, k, deikths;
printf("Choose one of the following:\n");
printf("1. Give sentence\n");
printf("2. Subtract a word\n");
printf("3. Add a word\n");
printf("4. Count the words\n");
printf("5. Count the sentences\n");
printf("6. Count the characters\n");
printf("7. Is the phrase a palindrome?\n");
printf("0. Exit\n");
scanf("%d", &choice);
if(scan(choice) == 1)
{
printf("Give sentence:\n");
gets(sentence);
gets(sentence);
printf("%s\n", sentence);
}
else(scan(choice) == 2);
{
printf("Give word you want to subtract\n");
gets(word);
printf("%s", word);
deikths = identify(sentence, word);
if(deikths != -1)
{
remove(sentence, word, deikths);
printf("Sentence without word: %s\n", sentence);
}
else
{
printf("Word not found in sentence.\n");
}
}
}
int identify(char sentence[], char word[])
{
int i, j, k;
for(k = 0; word[k] != '\0'; k++);
{
for(i = 0, j = 0; sentence[i] != '\0'; i++)
{
if(sentence[i] == word[j])
{
j++;
}
else
{
j = 0;
}
}
}
if(j == 1)
{
return(i - j);
}
else
{
return -1;
}
}
int remove(char sentence[], char word[], int deikths)
{
int i, k;
for(k = 0; word[k] != '\0'; k++)
{
for(i = deikths; sentence[i] != '\0'; i++)
{
sentence[i] = sentence[i + k + 1];
}
}
}
The error I am getting, is that the remove function has conflicting types. Any help with fixing my code will be greatly appreciated, or even an alternative solution to my problem would bre great.
As established in the comments, the compiler error is generated because remove is already defined in the stdio.h. After changing, the name the code compiles successfully, but still doesn't work as expected.
identify is the function which is meant to find whether a substring exists in a string and return its position. This is very similar to how strstr from the standard library works - I'd suggest having a look at an implementation of that function, to better understand how this is done.
The function you implemented only correctly finds substrings of length 1, at the end of the string. I have highlighted errors in the code below which cause this.
int identify(char sentence[], char word[])
{
int i, j, k;
for(k = 0; word[k] != '\0'; k++); // <- this loops is never actually ran because of the trailing semicolon - this is however a good thing as it is redundant
{
for(i = 0, j = 0; sentence[i] != '\0'; i++)
{
if(sentence[i] == word[j])
{
j++;
}
else
{
j = 0; // <- this makes it so only matches at the end can be found - otherwise, j is just reset back to 0
}
}
}
if(j == 1) // <- this makes it so only matches of length 1 can be found
{
return(i - j); // <- this is only correct if the match is at the end of the sentence
}
else
{
return -1;
}
}
strremove is inefficient due to the nested loops and the range of characters copied needs to be shortened - right now data is access beyond the end of the array.
int strremove(char sentence[], char word[], int deikths)
{
int i, k;
for(k = 0; word[k] != '\0'; k++) // <- this loop is redundant
{
for(i = deikths; sentence[i] != '\0'; i++) // <- you need to add range checking to make sure sentence[i+k+1] doesn't go beyond the end of the string
{
sentence[i] = sentence[i + k + 1];
}
}
}
I will leave the problems in main as an exercise to you - this is an assignment after all.

Find missing lower-case letters that are not in a series of words

As stated in the title I am trying to find all lower-case letters that are not in a series of words. There are no upper-case letters, digits, punctuation, or special symbols.
I need help fixing my code. I am stuck and do not know where to go from here.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
int letters[26];
char words[50];
int i = 0, b = 0;
printf("Enter your input : ");
scanf("%s", words);
for(i = 0; i < 26; i++){
letters[i] = 0;
}
while(!feof(stdin)){
for(b = 0; b < strlen(words) - 1; b++){
letters[ words[b] - 'a']++;
scanf("%s", words);
}
}
printf("\nMissing letters : %c ", b + 97);
return 0;
}
My output is giving me some random letter that I do not know where it is coming from.
Here is a working first implementation.
As well as the comments that have already been made, you should use functions wherever possible to separate out the functionality of the program into logical steps. Your main function should then just call the appropriate functions in order to solve the problem. Each function should be something that is self contained and testable.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_INPUT 20 /* Max input to read from user. */
char *readinput(void);
void find_missing_lower_case(char *, int);
int main()
{
char *user_input = readinput();
int len_input = strlen(user_input);
printf("user input: %s\n", user_input);
printf("len input: %d\n", len_input);
find_missing_lower_case(user_input, len_input);
/* Free the memory allocated for 'user_input'. */
free(user_input);
return 0;
}
char *readinput()
{
char a;
char *result = (char *) malloc(MAX_INPUT);
int i;
for(i = 0; i < MAX_INPUT; ++i)
{
scanf("%c", &a);
if( a == '\n')
{
break;
}
*(result + i) = a;
}
*(result + i) = '\0';
return result;
}
void find_missing_lower_case(char *input, int len_input)
{
int a = 97; /* ASCII value of 'a' */
int z = 122; /* ASCII value of 'z' */
int lower_case_chars[26] = {0}; /* Initialise all to value of 0 */
/* Scan through input and if a lower case char is found, set the
* corresponding index of lower_case_chars to 1
*/
for(int i = 0; i < len_input; i++)
{
char c = *(input + i);
if(c >= a && c <= z)
{
lower_case_chars[c - a] = 1;
}
}
/* Iterate through lower_case_chars and print any values that were not set
* to 1 in the above for loop.
*/
printf("Missing lower case characters:\n");
for(int i = 0; i < 26; i++)
{
if(!lower_case_chars[i])
{
printf("%c ", i + a);
}
}
printf("\n");
}
I figured it out and this is the code I used.
int main(void)
{
int array[26];
char w;
int i=0;
for(i=0; i<26; i++) {
array[i]=0; }
printf("Enter your input: ");
scanf("%c", &w);
while(!feof(stdin)) {
array[w-97] = 1;
scanf("%c", &w); }
printf("Missing letters: ");
for(i=0; i<26; i++) {
if(array[i] == 0) {
printf("%c ", i+97); }
}
printf("\n");
return 0;
}

Error: Segmentation Fault 11 in Palindrome

So I have been trying to make sure that I can check whether an user-inputted word is a palindrome or not by using function prototypes. However, I am getting an error in the end saying "Segment Fault: 11". I am fairly new to using function prototypes so if anyone can help me with solving anything that can be going on in the body of the function definition, then please point it out to me.
#include <stdio.h>
void palindrome_check(int ch_length, char text)
int main(void)
{
int length;
printf("Enter how many characters are in the message: ");
scanf("%d", &length);
char m;
printf("Enter the message: ");
scanf("%c", &text);
palindrome_check(l, m);
return 0;
}
void palindrome_check(int ch_length, char text)
{
char msg[ch_length];
text = msg[ch_length];
int count = 0;
while (count < ch_length)
{
count++;
scanf("%c", &msg[count]);
}
int i, j;
for (i = 0; i < ch_length; i++)
{
msg[j] = msg[ch_length - i];
}
if (text[i] == text[j])
{
printf("The message you entered is a palindrome!\n");
}
else
{
printf("It's not a palindrome.\n");
}
}
I couldn't understand some of your code it seemed you were doing some unnecessary things. What was msg for? This should work if I understood your problem correctly:
#include <stdio.h>
#include <string.h>
void palindrome_check(int ch_length, char text []);
int main(void)
{
char text [100];
/*
int length;
printf("Enter how many characters are in the message: ");
scanf("%d", &length);
Not necessary if you use strlen()*/
printf("Enter the message: ");
fgets(text,100,stdin); /*Using fgets() to allow spaces input*/
/*Strip the newline*/
text [strlen(text)-1]='\0';
palindrome_check(strlen(text),text);
return 0;
}
void palindrome_check(int ch_length, char text [])
{
int i;
for (i = 0; i < ch_length; i++)
{
if(text [i] != text [ch_length-i-1])
{
printf("It's not a palindrome.\n");
return;
}
}
printf("It is a palindrome!\n");
}

making a word search puzzle?

I've made a program that allows you to choose the size of the grid and it allows you to enter up to 20 words. Now I have to insert the entered words horizontally into the original array using a function. The function must return a value for success and a value for failure to enter the word into the puzzle board. I need help getting started with what the actual function should look like along with the function prototype. Pseudocode would be helpful. I'm a fairly new programmer so any help is great. Thank you
#include<stdio.h>
#include<string.h>
void printmatrix(char matrix[][20],int);
void inserthor(char matrix[][20],int);
int main(void)
{
//declare variables
char matrix[20][20];
char words[20][100];
int x;
int a,b;
int i=0;
int n=0;
for (a=0;a<20;a++)
{
for (b=0;b<20;b++)
{
matrix[a][b] = '+';
}
}
while (x<10 || x>20)
{
printf("How large would you like the puzzle to be (between 10 and 20):\n");
scanf("%d",&x);
}
printmatrix(matrix,x);
//part 3
printf("Enter up to 20 words to hide in the puzzle.\n");
printf("Enter the word 'done' after your last word if entering less than 20 words.\n");
for (i = 0; i < 20; i++)
{
printf("Enter word %2d:\n", i+1);
if (scanf("%99s", words[i]) != 1 || strcmp(words[i], "done") == 0)
break;
}
n = i;
printf("%d words entered\n", n);
for (i = 0; i < n; i++)
printf("Word %2d = [%s]\n", i+1, words[i]);
return 0;
}
void printmatrix(char matrix[][20],int x)
{
int i,j;
printf("Empty Puzzle:\n");
for (i=0;i<x;i++)
{
for (j=0;j<x;j++)
{
printf(" %c ", matrix[i][j]);
}
printf("\n");
}
}
Your function prototype
void inserthor(char matrix[][20],int);
lacks the parameter with the word to be entered and the value to be returned. You could use
char *inserthor(char matrix[][20], int order, char *word)
{
int i, j, l = strlen(word);
for (i = 0; i < order; ++i)
for (j = 0; j <= order-l; ++j)
if (matrix[i][j] == '+') return memcpy(&matrix[i][j], word, l);
return NULL;
}
which returns the address of the inserted word for success and NULL for failure.

Resources