Calculating characters apperances in a row - c

Program i've wrote is calculating how many times each letter appears in a string. I want to change it that it will find the character that appears the great amount of times in a row i.e. for the string "aabbbcccca" i want to printf "c" (as there are four c in a row and only two a and three b).
How to change my program that it will do the things i want? I am looking for the solution that will be as simple as possible and I want to use the existing code as much as possible.
#include "stdafx.h"
#include "string.h"
#include "ctype.h"
int count_nonspace(const char* str)
{
int count = 0;
while (*str)
{
if (!isspace(*str++))
count++;
}
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[127];
int i = 0, j = 0, count[127] = { 0 };
char string[100] = "Hello world";
for (i = 0; i < strlen(string); i++)
{
for (j = 33; j<127; j++)
{
if (string[i] == (j))
{
count[j]++;
}
}
}
for (j = 0; j< 127; j++)
{
if (count[j] > 0)
if (j < ' ' + 1)
printf("\n%d -> %d", count[j], j);
else
printf("\n%d -> %c", count[j], char(j));
}
}
My idea for changing the code is the following (posted only the changed part):
but still results are not as anticipated, why is that?
for (i = 0; i < strlen(string); i++)
{
for (j = 33; j<127; j++)
{
if (string[i] == (j))
{
count[j]++;
if (string[i] == string[i + 1])
count[j]++;
else
best[j] = count[j];
}
}
}

#include "stdafx.h"
#include "string.h"
#include "ctype.h"
int count_nonspace(const char* str)
{
int count = 0;
while (*str)
{
if (!isspace(*str++))
count++;
}
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[127];
int i = 0, j = 0, count[127] = { 0 };
int cur_count = 1; /* Gets compared with value in count[] */
char cur_char = '\0';
char string[100] = "Hello world";
for (i = 0; i < strlen(string); i++)
{
if(cur_char == string[i])
{
cur_count++;
}
else
{
if(32 < cur_char && cur_char < 127)
{
if(cur_count > count[cur_char])
{
count[cur_char] = cur_count;
}
}
cur_char = string[i];
cur_count = 1;
if(32 < cur_char && cur_char < 127)
{
if(!(count[cur_char]))
{
count[cur_char] = cur_count;
}
}
}
}
/* Find the most consecutive char and print it. */
char max_char = '\0';
int max_count = 0;
for(j = 0; j < 127; j++)
{
if(max_count < count[j])
{
max_count = count[j];
max_char = j;
}
}
printf("%c\n", max_char);
}

Related

Is there a way to combine two different strings alphabetically using stack in C?

Here is the program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *stack;
int stackindex = 0;
int push(char in){
if (stack==NULL) {
stack = (char *) malloc (sizeof(char));
}
else {
stack = (char *) realloc(stack, sizeof(char)* ++stackindex);
}
stack[stackindex] = in;
return 1;
}
char pop(){
return stack[stackindex--];
}
void show() {
for (int i=stackindex; i>=0;i--) {
printf("%c", pop());
}
}
int main(int argc, char**argv) {
for (int i = 1; i < argc; i++) {
int k = strlen(argv[i]);
int j;
for (int j = 0; j < k ; j++) {
char in = argv [i] [j];
push( in );
}
for (j = i; j < k; j++){
char temp;
if (stack[i] > stack[j]) {
temp = stack[j];
stack[j] = stack[i];
stack[i] = temp;
}
if (i, argc-1) push(' ');
}
show();
return 0;
}
}
I've been trying to make an output from the string "im loving" alphabetically in descending order (ASCII based) using command line arguments like this: vonmliig, but the output was "m i" instead.
Looks like you are trying to sort as you build the stack. I would build the stack first, then sort.
int main(int argc, char**argv)
{
if (argc >= 3) {
// Build stack
for (int i = 1; i < argc; i++) {
int k = strlen(argv[i]);
for (int j = 0; j < k ; j++) {
push(argv[i][j]);
}
}
// Sort
for (int i = 0; i < stackindex; i++) {
for (int j = i; j < stackindex; j++){
if (stack[i] > stack[j]) {
char temp = stack[j];
stack[j] = stack[i];
stack[i] = temp;
}
}
}
show();
}
return 0;
}
Also, there is an error in the handling of stackindex in push().
int push(char in)
{
char *temp = realloc(stack, stackindex + 1);
if (!temp) {
// Handle error
return -1;
}
stack = temp;
stack[stackindex++] = in;
return 1;
}
char pop()
{
if (stackindex) return stack[--stackindex];
else return 0;
}
void show()
{
do {
printf("%c", pop());
} while (stackindex);
}

Segmentation fault in substitution ciphertext

I am implementing a substitution ciphertext scheme. It's an assignment
from the CS50 course.
The input are the substitution scheme and the plaintext and the
program should write back the ciphertext.
$ ./encrypt QWERTYUIOPASDFGHJKLZXCVBNM HE
IT
I keep getting segmentation fault and I am not sure why. This is the source of my solution.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int result (string words);
int main (int argc, string argv[])
{
if (argc == 2)
{
if (strlen (argv[1]) == 26)
{
int k = 0;
for (int i = 0; i<strlen(argv[1]); i++)
{
if (((int)argv[1][i] > 64 && (int)argv[1][i] < 91) || ((int)argv[1][i] > 96 && (int)argv[1][i] < 123))
{
k +=1;
}
}
if (k==26)
{
char new_var[strlen(argv[1])];
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
new_var[j] = toupper((char)argv[1][j]) ;
}
string key = new_var;
printf ("%i", result(key));
if (result (key) == 3)
{
string word = get_string("plaintext: ") ;
char mutated_word [strlen(word)];
for (int i = 0; word[i] != '\0'; i++)
{
if ((int)word[i]>64 && (int)word[i]<91)
{
mutated_word[i] = key[(int)word[i] - 65] ;
}
else if ((int)word[i]>96 && (int)word[i]<123)
{
mutated_word[i] = (char) ((int)(key[(int)word[i]-97]) + 32) ;
}
else
{
mutated_word[i] = word[i] ;
}
}
printf("ciphertext: %s\n", mutated_word);
return 0;
}
else
{
printf ("Usage: ./substitution keya\n");
return 1;
}
}
else
{
printf ("Usage: ./substitution keyb\n");
return 1;
}
}
else
{
printf ("Usage: ./substitution keyc\n");
return 1;
}
}
else
{
printf ("Usage: ./substitution keyd\n");
return 1;
}
}
int result (string words)
{
int conclusion = 3;
for (int j = 0; j< strlen(words) - 1 ; j++)
{
for(int i =j+1; i<strlen(words) - 1; j++)
{
if ((int)words[i] == (int)words[j])
{
conclusion = 4;
}
}
}
return conclusion;
}
Check your 'Result' function
In it, check your 2nd for loop...
int result (string words)
{
int conclusion = 3;
for (int j = 0; j< strlen(words) - 1 ; j++)
{
for(int i =j+1; i<strlen(words) - 1; j++)
{
if ((int)words[i] == (int)words[j])
{
conclusion = 4;
}
}
}
return conclusion;
}
Don't think you have replaced 'i' with 'j'. j++
No Segmentation fault
Then it will not show segmentation fault, pls check your code thoroughly always.

Checking NULL pointer segmentation fault in C

I have to split an initial char and create a list of said char which has to end with a NULL so i can iterate over in the main without knowing list size. The problem is that i get a seg fault whenever i try to check if last element is NULL. i am sorry i am still trying to learn both C and english. Thank you all
#include <stdlib.h>
#include <stdio.h>
char **split(const char *s) {
char **split;
unsigned m_size = 0, c_size, i, j, k;
// get s size
for (i = 0; s[i] != '\0'; i++) {
if (s[i] == ' ') {
m_size++;
}
}
m_size++;
split = (char**) malloc(sizeof(char) * (m_size + 1));
int sizes[m_size];
c_size = 0;
// get s words size
for (i = 0, j = 0; s[i] != '\0'; i++) {
if (s[i] == ' ') {
c_size++;
sizes[j] = c_size;
c_size = 0;
j++;
} else {
c_size++;
}
}
sizes[j] = c_size;
for (i = 0; i < m_size; i++) {
split[i] = (char *) malloc(sizeof(char) * sizes[i]);
}
split[i] = NULL;
for (i = 0, j = 0, k = 0; s[i] != '\0'; i++) {
if (s[i] != ' ') {
split[j][k] = s[i];
k++;
} else {
split[j][k] = '\0';
j++;
k = 0;
}
}
return split;
}
int main() {
char s[19] = "hello how are you?";
char **splitted;
unsigned i;
splitted = split(s);
if (splitted == NULL) {
return 1;
}
for (i = 0; splitted[i]!=NULL; i++) {
printf("%s\n", splitted[i]);
}
return 0;
}
EDIT
#include <stdlib.h>
#include <stdio.h>
char **split(const char *s) {
char **r;
unsigned word_size = 0;
unsigned list_size = 0, i, j, k;
// get s size
for (i = 0; s[i] != '\0'; i++) {
if (s[i] != ' ') {
word_size++;
} else {
if (word_size > 0) {
list_size++;
word_size = 0;
}
}
}
list_size++;
r = malloc(sizeof(*r) * (list_size + 1));
int char_sizes[list_size];
for (i = 0; i < list_size; char_sizes[i] = 0, i++);
// get s words size
for (i = 0, j = 0; s[i] != '\0'; i++) {
if (s[i] != ' ') {
char_sizes[j]++;
} else {
if (char_sizes[j] > 0) {
j++;
}
}
}
for (i = 0; i < list_size; i++) {
r[i] = malloc(sizeof(char) * char_sizes[i]);
}
r[i] = NULL;
for (i = 0, j = 0, k = 0; s[i] != '\0'; i++) {
if (s[i] != ' ') {
r[j][k] = s[i];
k++;
} else {
if (k > 0) {
r[j][k] = '\0';
j++;
k = 0;
}
}
}
return r;
}
void destroy(char **list) {
unsigned i;
for (i = 0; list[i] != NULL; i++) {
free(list[i]);
}
free(list);
}
int main() {
char s[100] = " hello guys how are? you,d 31 3 ";
char **splitted;
unsigned i;
splitted = split(s);
if (splitted == NULL) {
return 1;
}
for (i = 0; splitted[i]!=NULL; i++) {
printf("%s", splitted[i]);
}
destroy(splitted);
return 0;
}
ok guys i followed your tips and i edited my code. leaving this here if someone wants to point out other errors i will appreciate. now it should work even with multiple spaces. thanks to all
Your are requesting an "array" of pointers to char, but you are allocating an "array" of chars:
split = (char**) malloc(sizeof(char) * (m_size + 1));
should become
split = malloc(sizeof(char*) * (m_size + 1));
Note the sizeof(char*). BTW: Note that in C, you should not cast the result of malloc as explained in this SO post.

CS50 Vigenere's Cipher: Can't fix the problem with the spaces in the plaintext

I'm having a problem with my code, which is supposed to be the
Vigenere's cipher from Harvard's CS50 program. I have met all the
requirements, except when the key is "baz" and the plaintext is "hello,
world!". Because there is a comma and a space, my code accepts that, and
shifts from z to a to b, etc. I want it to ignore the spaces and
punctuation so that the "b" "a" "z" only change for the characters.
You should only have to look at the first "else if" line, as that is
when the plaintext is longer than the key, and the key will have to
alternate.
Please help! I've been on this problem for hours, and I can't figure out
the solution.
https://gyazo.com/3a7b3e692d210262ae15f580b10f296d
https://gyazo.com/0b25bfc010d937840f09ff4294d0dd41
https://gyazo.com/c1b85208ecae1b9ad57d48d5b5af59b5
This is my code:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main(int argc, string argv[])
{
if (argc == 2)
{
string key = argv[1];
int key_length = strlen(key);
for (int i = 0; i < key_length; i++)
{
key[i] = toupper(key[i]);
}
for (int k = 0; k < key_length; k++)
{
if (key[k] < 'A' || key[k] > 'Z')
{
printf("Not a valid key!\n");
return 1;
}
}
for (int i = 0; i < key_length; i++)
{
key[i] = key[i] - 'A';
}
string plain = get_string("Plaintext: ");
int plain_length = strlen(plain);
if (key_length == plain_length)
{
for (int i = 0; i < key_length; i++)
{
if (islower(plain[i]))
{
for (int q = 0; q < key[i]; q++)
{
plain[i]++;
if (plain[i] > 'z')
{
plain[i] = 'a';
}
}
}
if (isupper(plain[i]))
{
for (int q = 0; q < key[i]; q++)
{
plain[i]++;
if (plain[i] > 'Z')
{
plain[i] = 'A';
}
}
}
}
printf("ciphertext: %s\n", plain);
}
else if (key_length < plain_length)
{
float truncate_not = plain_length / key_length;
int truncate = trunc(truncate_not);
int mod = strlen(plain) % key_length;
for (int i = 0; i < truncate; i++)
{
for (int k = 0; k < key_length; k++)
{
int pos = k + (i * key_length);
if (islower(plain[pos]))
{
for (int q = 0; q < key[k]; q++)
{
plain[pos]++;
if (plain[pos] > 'z')
{
plain[pos] = 'a';
}
}
printf("%c\n", plain[pos]);
}
else if (isupper(plain[pos]))
{
for (int q = 0; q < key[k]; q++)
{
plain[pos]++;
if (plain[pos] > 'Z')
{
plain[pos] = 'A';
}
}
printf("%c\n", plain[pos]);
}
else
{
printf("error\n");
}
}
}
for (int j = 0; j < mod; j++)
{
int pos = j + (truncate * key_length);
if (islower(plain[pos]))
{
for (int q = 0; q < key[j]; q++)
{
plain[pos]++;
if (plain[pos] > 'z')
{
plain[pos] = 'a';
}
}
}
else if (isupper(plain[pos]))
{
for (int q = 0; q < key[j]; q++)
{
plain[pos]++;
if (plain[pos] > 'Z')
{
plain[pos] = 'A';
}
}
}
else
{
plain[j] = plain[j];
}
}
printf("ciphertext: %s\n", plain);
}
else
{
int mod = plain_length % key_length;
for (int i = 0; i < mod; i++)
{
if (islower(plain[i]))
{
for (int q = 0; q < key[i]; q++)
{
plain[i]++;
if (plain[i] > 'z')
{
plain[i] = 'a';
}
}
}
if (isupper(plain[i]))
{
for (int q = 0; q < key[i]; q++)
{
plain[i]++;
if (plain[i] > 'Z')
{
plain[i] = 'A';
}
}
}
}
printf("ciphertext: %s\n", plain);
}
}
else
{
printf("Incorrect number of arguments!\n");
return 1;
}
}
Actual results: iekmo, wnslc!
Expected results: iekmo, vprke!
As you can see, my code shifted the "baz", when it shouldn't have in the
space and comma places.
The overriding problem in this program is tying key_length to plain_length. They are discrete and must be treated separately; they "move" at different rates and should use separate and distinct indices.
First I suggest you rewatch the walkthrough. You probably want to write out (yes, pencil and paper) the "baz" example using the same format that Zamyla does with the "panda" example. And then write pseudo-code.
You will probably have to abandon much of the code you have written; generally this pset can be done with one loop that cycles through the plaintext by index, and then manages the keyword index independently within that loop.

word frequency of string counter is sometimes wrong

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;
}

Resources