Below is the code template and under /* write your code here */ is my own code.
The template should be correct but there is sth wrong with my code.
My algorithm is to iterate through str until finding the null character.
Then compare each character, if they are the same then iterate through both str and sub, otherwise set continue to iterate through str and reset to the first character of substr.
#include <stdio.h>
int findSubstring(char *str, char *substring);
int main()
{
char str[40], substr[40];
printf("Enter the string: ");
gets(str);
printf("Enter the substring: ");
gets(substr);
printf("findSubstring(): %d\n", findSubstring(str, substr));
return 0;
}
int findSubstring(char *str, char *substr)
{
/* write your code here */
int i = 0, j = 0;
while ((str[j] != '\0')||(substr[i] != '\0')) {
if (substr[i] != str[j]) {
j++;
i = 0;
}
else {
i++;
j++;
}
}
if (substr[i] == '\0')
return 1;
else
return -1;
}
Do not use gets(), which has unavoidable risk of buffer overrun.
The condition of the loop is wrong. The loop should exited if one of *(str + j) or *(substr + i) is a (terminating) null character.
Fixed code:
#include <stdio.h>
int findSubstring(char *str, char *substring);
void safer_gets(char *str, size_t max);
int main(void)
{
char str[40], substr[40];
printf("Enter the string: ");
safer_gets(str, sizeof(str));
printf("Enter the substring: ");
safer_gets(substr, sizeof(str));
printf("findSubstring(): %d\n", findSubstring(str, substr));
return 0;
}
int findSubstring(char *str, char *substr)
{
int i = 0, j = 0;
while ((*(str + j) != '\0')&&(*(substr + i) != '\0')) {
if (*(substr + i) != *(str + j)) {
j++;
i = 0;
}
else {
i++;
j++;
}
}
if (*(substr + i) == '\0')
return 1;
else
return -1;
}
void safer_gets(char *str, size_t max)
{
int i;
fgets(str, max, stdin);
for (i = 0; *(str + i) != '\0'; i++) {
if (*(str + i) == '\n') {
*(str + i) = '\0';
break;
}
}
}
/*--------------------------One more simple example-----------------------------
Find the words from a set of words containing a given substring?
Input: Set of Words: [blackcat, blackdog, blackrat, whitetiger, blueelephant],
Substring: black
Output:[blackcat, blackdog, blackrat]
-----------------------------------------------------------------------------------------*/
#include <iostream>
#include <cstring>
int substring(char* sub,char* string);
int main()
{
const char* Names[] { "blackcat", "blackdog", "blackrat", "whitetiger", "blueelephant" };
char substr[]{ "black" };
int found{ -1 };
for (auto strings: Names)
{
found = substring(substr, const_cast<char*>(strings));
if (found != -1) {
std::cout << strings << " ";
}
}
std::cout << std::endl;
return 0;
}
int substring(char* sub, char* string)
{
int i{};
int j{};
while ((string[i] != '\0') && (sub[j] != '\0'))
{
if (string[i] != sub[j]) {
j++;
i = 0;
}
else {
i++;
j++;
}
}
if (sub[j] == '\0' && i != 0) {
return 1;
}
else {
return -1;
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
char s[100],sub[50];
int i,j,c=0;
clrscr();
printf("enter string and substring\n");
gets(s);
printf("\n");
gets(sub);
printf("\n");
i=0;
j=0;
while(s[i]!='\0')
{
if(s[i]!=sub[j])
i++;
else if(s[i]==sub[j])
{
while(sub[j]!='\0')
{
if(s[i]==sub[j])
{
i++;
j++;
c++;
}
else
{
c=0;
break;
}
}
}
}
if(c!=0)
printf("\nsubstring is present \n ");
else
printf("\nsubstring is absent \n ");
getch();
}
Related
Why do I get an error when I insert something like ';' or '&' in a Palindrome program?
I'd like to create a Palindrome program that automatically excludes special symbols.
eg:
mad!am -> The Palindrome is correct.
But it doesn't work. It only works when I put madam, but when I put mad!am, it gives an error.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAX_STACK_SIZE 100
typedef char element;
typedef struct {
element data[MAX_STACK_SIZE];
int top;
} StackType;
void init_stack(StackType* s)
{
s->top = -1;
}
int is_empty(StackType* s)
{
return (s->top == -1);
}
int is_full(StackType* s)
{
return (s->top == (MAX_STACK_SIZE - 1));
}
void push(StackType* s, element item)
{
if (is_full(s)) {
fprintf(stderr, "error\n");
return;
}
else s->data[++(s->top)] = item;
}
element pop(StackType* s)
{
if (is_empty(s)) {
fprintf(stderr, "error\n");
exit(1);
}
else return s->data[(s->top)--];
}
element peek(StackType* s)
{
if (is_empty(s)) {
fprintf(stderr, "error\n");
exit(1);
}
else return s->data[s->top];
}
int main(void)
{
StackType s, a, k;
init_stack(&s);
init_stack(&a);
init_stack(&k);
int i=0;
char input[MAX_STACK_SIZE];
printf("문자열을 입력하세요: ");
scanf("%s", input);
int length = strlen(input);
for (int i = 0; i < length; i++) {
if ('a' <= input[i] && input[i] <= 'z') {
push(&s, input[i]);
push(&a, input[i]);
}
else if ('A' <= input[i] && input[i] <= 'Z') {
push(&s, input[i] - ('A' - 'a'));
push(&a, input[i] - ('A' - 'a'));
}
}
for (int i = 0; i < length; i++)
{
push(&k, pop(&s));
}
while (k.data[MAX_STACK_SIZE]!=NULL) {
if (peek(&a) != peek(&k))
{
printf("not palindrome \n");
break;
return 0;
}
pop(&k);
pop(&a);
printf("palindrome ok!\n");
}
return 0;
}
Avoid constructs like if ('a' <= input[i] && input[i] <= 'z'). That's just makes a mess. In your case, just use isalpha().
But the problem lies in the for loop after you have read all the characters. Since you're skipping characters you cannot use length again. Do these changes:
int length = strlen(input);
int no_letters=0;
for (int i = 0; i < length; i++) {
if (isalpha(input[i])) {
push(&s, input[i]);
push(&a, input[i]);
no_letters++;
}
}
for (int i = 0; i < no_letters; i++)
{
push(&k, pop(&s));
}
There was a small bug with length.. You should ignore counting string length for those element which should not be counted.. Here I have corrected the code with comment , compiled and run.. It is working correct now...
Please try below code... I have added comment what I added..
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAX_STACK_SIZE 100
typedef char element;
typedef struct {
element data[MAX_STACK_SIZE];
int top;
} StackType;
void init_stack(StackType* s)
{
s->top = -1;
}
int is_empty(StackType* s)
{
return (s->top == -1);
}
int is_full(StackType* s)
{
return (s->top == (MAX_STACK_SIZE - 1));
}
void push(StackType* s, element item)
{
if (is_full(s)) {
fprintf(stderr, "error\n");
return;
}
else s->data[++(s->top)] = item;
}
element pop(StackType* s)
{
if (is_empty(s)) {
fprintf(stderr, "error\n");
exit(1);
}
else return s->data[(s->top)--];
}
element peek(StackType* s)
{
if (is_empty(s)) {
fprintf(stderr, "error\n");
exit(1);
}
else return s->data[s->top];
}
int main(void)
{
StackType s, a, k;
init_stack(&s);
init_stack(&a);
init_stack(&k);
int i=0;
char input[MAX_STACK_SIZE];
printf("문자열을 입력하세요: ");
scanf("%s", input);
int length = strlen(input);
int len=0; // added
for (int i = 0; i < length; i++) {
if ('a' <= input[i] && input[i] <= 'z') {
push(&s, input[i]);
push(&a, input[i]);
len++; //counting length for valid characters
}
else if ('A' <= input[i] && input[i] <= 'Z') {
push(&s, input[i] - ('A' - 'a'));
push(&a, input[i] - ('A' - 'a'));
len++; //counting length for valid characters only
}
}
for (int i = 0; i < len; i++)
{
push(&k, pop(&s));
}
while (k.data[MAX_STACK_SIZE]!=NULL) {
if (peek(&a) != peek(&k))
{
printf("not palindrome \n");
break;
return 0;
}
pop(&k);
pop(&a);
printf("palindrome ok!\n");
}
return 0;
}
i want to make the programm loop again and again until i type the endword ****TELOS
/* 1 */ int text_input();
int main() {
int number;
text_input();
return 0;
}
int text_input(char words[M][N]){
int l=0; /*lines, also how many words the text has */
char a;
int i=0;
printf("Enter the text. (****TELOS for stoping)");
char endword[10];
strcpy(endword, "****TELOS");
char temp[N];
while(1){
while(1) {
a = getchar();
if (a =='\n'){
if(strcmp(temp, "") == 0){
continue;
}
else{
break;
}
}
else if (a == ' '){
if(strcmp(temp, "") == 0){
continue;
}
else{
break;
}
}
else {
temp[i++] = a;
}
}
if (strcmp(temp, endword) == 0){
break;
}
else{
strcpy(words[l++],temp);
memset(temp, ' ', strlen(temp));
}
}
return 0;
}
I think your code doesn't work because you don't have set each item of endword to 0
so your code should be like that
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10
#define M 10
int text_input(char words[M][N]);
int main() {
char a[M][N];
text_input(a);
return 0;
}
int text_input(char words[M][N]){
int l=0; /*lines, also how many words the text has */
char a;
int i=0;
char temp[N];
char endword[10] = {0,0,0,0,0,0,0,0,0,0};
printf("Enter the text. (****TELOS for stoping)");
strcpy(endword, "****TELOS");
while(1){
while(1) {
a = getchar();
if (a =='\n'){
if(strcmp(temp, "") == 0){
continue;
}
else{
break;
}
}
else if (a == ' '){
if(strcmp(temp, "") == 0){
continue;
}
else{
break;
}
}
else {
temp[i++] = a;
}
}
if (strcmp(temp, endword) == 0){
break;
}
else{
strcpy(words[l++],temp);
memset(temp, ' ', strlen(temp));
}
}
return 0;
}
I hope you can help me I worked on this code. The code works like this
user inputs a string for example "hey john, how are you john?
the program erases signs like "'?' , ',' '!' " etc.
the program writes a string after erasing the signs : "hey john how are you john?"
and the code outputs the frequency of each word:
hey : 1
john: 2
how : 1
are : 1
you : 1
but my code counts sometimes wrong. For example when I type "bye bye bye hello hello hello"
the output is :
bye : 3
hello : 1
My code does the john example right, but the bye bye... example wrong.
How do I have to change my code? Thank you
#include <stdio.h>
#include <string.h>
char words[80][80];
void clear_string(char *text);
int extract_and_count(char *source, int *count);
void clearArray(char array[]);
int indexInWords(char string[]);
void print(int countOfWords, int count[]);
int equals(char *string1, char *string2);
int main() {
char string[80];
int count[80];
printf("please enter your text: ");
scanf("%[^\n]s", string);
clear_string(string);
printf("%s\n", string);
int countOfWords = extract_and_count(string, count);
print(countOfWords, count);
return 0;
}
void clear_string(char *text){
int i = 0;
for(;i < strlen(text);++i){
if( text[i] == '.' || text[i] == ',' || text[i] == '!' || text[i] == '?'){
int k = i + 1;
for(; k < strlen(text);++k){
text[k-1] = text[k];
}
k = strlen(text) - 1;
text[k] = ' ';
}
}
}
int extract_and_count(char *source, int *count){
int wordCounter = 0;
char string[80];
int i = 0, k = 0;
clearArray(string);
for(; i < strlen(source);++i, ++k){
if(source[i] != ' '){
string[k] = source[i];
}else{
if(string[0] == '\0'){
break;
}
int index = indexInWords(string);
if(index == -1){
strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
}else{
count[index] += 1;
}
clearArray(string);
k = -1;
}
}
return wordCounter;
}
void clearArray(char array[]){
memset(array,0,strlen(array));
//array[0] = '\0';
}
int indexInWords(char string[]){
int i = 0;
for(;i < 80;++i){
if(equals(words[i], string) == 0){
return i;
}
}
return -1;
}
void print(int countOfWords, int count[]){
for(int i = 0;i < countOfWords; ++i){
printf("%s : %d\n",words[i], count[i]);
}
}
int equals(char string1[], char string2[]){
return strcmp(string1, string2);
}
The most significant problem I found was in extract_and_count() -- it doesn't count the last word as it only counts words followed by space. The bandaid is to check if string has anything in it after the loop, and if so, process it. Below is my rework for that fix and general style:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
void clear_string(char *text);
int extract_and_count(char *source, int count[]);
void clearArray(char array[]);
int indexInWords(char string[]);
void print(int countOfWords, int count[]);
bool equals(char *string1, char *string2);
#define BUFFER_SIZE (512)
#define MAX_WORD_COUNT (80)
#define MAX_WORD_SIZE (64)
char words[MAX_WORD_COUNT][MAX_WORD_SIZE];
int main() {
char string[BUFFER_SIZE];
int count[MAX_WORD_COUNT];
printf("Please enter your text: ");
while (fgets(string, BUFFER_SIZE, stdin) == NULL) {
printf("Please (re)enter your text: ");
}
clear_string(string);
int countOfWords = extract_and_count(string, count);
print(countOfWords, count);
return 0;
}
void clear_string(char *text) {
for (int i = 0; i < strlen(text); i++) {
if (text[i] == '.' || text[i] == ',' || text[i] == '!' || text[i] == '?' || text[i] == '\n') {
int length = strlen(text);
for (int k = i + 1; k < length; k++) {
text[k - 1] = text[k];
}
text[length - 1] = '\0';
i--;
}
}
}
int extract_and_count(char *source, int count[]) {
int wordCounter = 0;
char string[MAX_WORD_SIZE] = {'\0'};
for (int i = 0, k = 0; i < strlen(source); i++, k++) {
if (source[i] != ' ') {
string[k] = source[i];
} else {
if (string[0] == '\0') {
break;
}
int index = indexInWords(string);
if (index == -1) {
strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
} else {
count[index] += 1;
}
clearArray(string);
k = -1;
}
}
if (string[0] != '\0') {
int index = indexInWords(string);
if (index == -1) {
strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
} else {
count[index] += 1;
}
}
return wordCounter;
}
void clearArray(char array[]) {
memset(array, 0, strlen(array));
}
int indexInWords(char string[]) {
for (int i = 0; i < MAX_WORD_COUNT; i++) {
if (equals(words[i], string)) {
return i;
}
}
return -1;
}
void print(int countOfWords, int count[]) {
for (int i = 0; i < countOfWords; i++) {
printf("%s : %d\n", words[i], count[i]);
}
}
bool equals(char string1[], char string2[]) {
return strcmp(string1, string2) == 0;
}
The next most significant issue I see is you don't keep track of how many entries in words[][] are used, so indexInWords() could easily wander off making comparisons against uninitialized memory.
In extract_and_count you break out of the for-loop when you find 2 spaces. Also you did not check for the last word of source. Changed it to:
int extract_and_count(char *source, int *count){
int wordCounter = 0;
char string[80];
int i = 0, k = 0;
clearArray(string);
for(; i < strlen(source)+1;++i, ++k){
if(source[i] != ' ' && source[i] != 0){
string[k] = source[i];
}else{
if(string[0] != '\0'){
int index = indexInWords(string);
if(index == -1){
strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
}else{
count[index] += 1;
} }
clearArray(string);
k = -1;
}
}
return wordCounter;
}
I now need to output the Morse code equivalent of alphanumeric input. My condition for check this is an if loop: I try to look at each element of input array with each element of the alpha array but a match never seems to be found. I am not sure if I am using the correct method. I try to de-reference the point to input and compare the value with each element of alpha until a match is found. If no match is found then an error occurs.
Not working:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
int main(void)
{
char *morse[] = {"/",
".-","-...","-.-.","-..",".","..-.","--","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--..",
"-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};
char *alpha[]= {" ",
"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y", "Z",
"0", "1","2","3","4","5","6","7","8","9"};
char *print_array[50];
int print_array_index = 0;
char hold[50];
int hold_index = 0;
char input[200];
int i = 0;
printf("welcome to the Morse translator.\n");
printf("Enter input: ");
fgets(input, sizeof(input), stdin);
char *p;
for (p=input; *p !='\0';++p)
{
*p = toupper(*p);
}
if (input[0]=='-' || input[0]=='.')
{
while (input[i] !='\0') {
if (input[i] ==' ' || input[i] == '\n')
{
hold[hold_index] = '\0';
bool found = false;
for (int x = 0; x < sizeof(morse) / sizeof(char *); x++)
{
if (strcmp(morse[x], hold) == 0)
{
print_array[print_array_index++] = alpha[x];
found = true;
break;
}
}
if (!found)
{
fprintf(stderr, "invalid Morse code!\n");
}
hold_index = 0;
}
else
{
hold[hold_index++] = input[i];
}
i++;
}
for (int x = 0; x < print_array_index; x++)
{
printf("%s", print_array[x]);
}
printf("\n");
}
else if (isalnum(input[0]))
{
while (input[i]!='\0')
{
if (input[i] ==' ' || input[i] == '\n')
{
bool found = false;
for (int x=0; x < sizeof(alpha)/sizeof (char*);x++)
{
if (alpha[x]==input[i])
{
print_array [print_array_index++] = alpha[x];
found = true;
break;
}
}
if (!found)
{
fprintf(stderr, "Invalid input!\n");
}
hold_index = 0;
}
i++;
}
for (int x=0; x < print_array_index; x++)
{
printf("%s",print_array[x]);
}
printf("\n");
}
return 0;
}
part of else if (isalnum(input[0])){ should be something like
else if (isalnum(input[0])){
while (input[i]!='\0' && input[i]!='\n'){
bool found = false;
for (int x=0; x < sizeof(alpha)/sizeof(char*);x++){
if (*alpha[x]==input[i]){
print_array[print_array_index++] = morse[x];
found = true;
break;
}
}
if(!found){
fprintf(stderr, "Invalid input!\n");
}
i++;
}
for (int x=0; x < print_array_index; x++){
printf("%s", print_array[x]);
}
printf("\n");
}
I wrote a function in C that search if a substring is in a string and it's OK, but when I use it in a array of strings I face an error. Please see this code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int substring(char*, char*);
main()
{
char student[100][7];
int counter = 0;
int finish =1;
char s1[4];
while(finish){
printf("please Enter Student Number: \n");
scanf("%s", student[counter]);
counter++;
printf("Do you want to exit? 1/0");
scanf("%d", &finish);
}
printf("Now, You can search in Student numbers\n");
printf("Enter a number to search: ");
scanf("%s", s1);
for(int i = 0; i < counter; i++){
printf("%d : %s\n", i, student[i]);
if(substring(student[i],s1) == 1)
printf("%s", student[i]);
}
getch();
}
int substring(char *s1,char *s2)
{
int f=0;
for(; *s1 !='\0';)
{
if(*s2=='\0')
break;
for(;*s2 !='\0';)
{
if(*s1==*s2)
{
f=1;
s1 ++;
s2 ++;
}
else
{
f=0;
s1++;
break;
}
}
}
if(f==0)
return 0;
else
return 1;
getch();
}
This is how you can test for substring using simple state machine:
int substring(const char *s1, const char *s2)
{
enum
{
search, start_match
} state = search;
const char *m;
while(*s1 != '\0')
{
switch(state)
{
case search:
if(*s2 == '\0')
return 0;
else if(*s2 == *s1)
{
state = start_match;
m = s2 + 1;
}
break;
case start_match:
if(*m == '\0')
return 1;
else if(*m != *s1)
state = search;
else
++m;
}
++s1;
}
return 0;
}
Also you may use the standard strstr function.