#include <stdio.h>
int main(){
int i = 0;
int count[26] = {0};
char str[100];
printf("Ord: ");
scanf("%s", &str);
printf("Frekvens: \n");
while (str[i] != '\0'){
if (str[i]>= 'a' && str[i] <= 'z'){
int x = str[i] -'a';
count[x]++;
}
i++;
}
for (i = 0; str[i] != 0; i++){
printf("'%c' : %d\n", i + 'a', count[i]);
}
return 0;
}
How to see all letters in the alphabet printed when running this? Something to do with the for loop?
The printing loop should iterate over the indexes of count.
for (i = 0; i < 26; i++){
printf("'%c' : %d\n", i + 'a', count[i]);
}
Related
It's supposed to do the following
Ask for input : Stack (e.g)
Ask for key : qwertyuioplkjhgfdsazxcvbnm (e.g)
Convert into cipher text : Azqel
Here's the new updated code:
//Encrypting user input by using 26 character long alphabetic key provided by user
#include <stdio.h>
#include <string.h>
int main(){
char key[26];
char text[1000];
//Prompt user for key
do{
printf("Provide a 26 character long key : ");
scanf("%s", key);
//If key is empty, exit code
if(strlen(key) == 0){
printf("Error: Empty input");
return 1;
}
//If key is incomplete, prompt user again for complete key
if(strlen(key)!= 26){
printf("Error: Incomplete key\n");
}
}while(strlen(key) != 26);
//If key has values other than alphabets, exit code
for(int i= 0, n= strlen(key); i<n; i++){
if((key[i]< 'a' || key[i]> 'z') && (key[i]< 'A' || key[i]> 'Z')){
printf("Error: Invalid key");
return 2;
}
}
//If key has repeated values, exit code
for(int i= 0; i< strlen(key); i++){
for(int j= i+ 1; key[j]!= '\0'; j++){
int x, y;
if(islower(key[i])){
x = key[i] - 'a';
}
else {
x = key[i] - 'A';
}
if(islower(key[j])){
y = key[j] - 'a';
}
else {
y = key[j] - 'A';
}
if(x == y){
printf("Error: Repeated characters in key");
return 3;
}
}
}
//Prompt user for input
printf("Plaintext : ");
scanf("%s", text);
//If input is empty, exit code
if(strlen(text) == 0){
printf("Error: Empty input");
return 4;
}
printf("Ciphertext: ");
for(int i= 0, n= strlen(text); i< n; i++){
//Encrypting small letters
if(text[i] >= 'a' && text[i] <= 'z'){
printf("%c", key[text[i]-'a']);
}
//Encrypting capital letters
else if(text[i] >= 'A' && text[i] <= 'Z'){
printf("%c", key[text[i]-'A']-('a'-'A'));
}
//Printing characters other than alphabets
else{
printf("%c", text[i]);
}
}
return 0;
}
The bug where code was treating lower case characters and upper case characters differently even if they were same has been fixed now.
If the key is for example : qwertyuioplkjhgfdsazxcvbmM (it has the letters 'm' and 'M' but they were being treated as different characters)
you can try this:
#include <stdio.h>
#include <string.h>
int main(){
char s[1000];
char t[26];
printf("Text: ");
scanf("%s", s);
printf("Key: ");
scanf("%s", t);
for(int i= 0, n= strlen(s); i< n; i++){
if(s[i] >= 'a' && s[i] <= 'z') printf("%c", t[s[i]-'a']);
else printf("%c", t[s[i]-'A'] + ('A'-'a'));
}
return 0;
}
and as said in the comments, try not to use numbers like 97 and 122, use 'a' and 'z' instead.
and for the bug mentioned in the updated code, instead of a simple check like key[j] == key[i] do this
int x, y;
if(islower(key[i])) x = key[i] - 'a';
else x = key[i] - 'A';
if(islower(key[j])) y = key[j] - 'a';
else y = key[j] - 'A';
if(x == y){
printf("Error: Repeated characters in key");
return 3;
}
or you can use a bool check[26], traverse the key, make the value of a character true if(islower(key[i])) check[key[i]-'a'] = true; else check[key[i]-'A'] = true; and lastly check whether the entire bool array is true.
I want to count letters,numbers,symbols using function password Seems like it doesn't count out numbers correctly because i get 0 for every printf in the end
#include <stdio.h>
void password(char * str[],int together,int numbers,int symbols,int i,int uppercase,int lowercase);
int main()
{
char str[100];
int together, numbers, symbols, i,uppercase,lowercase;
together = numbers = symbols = i = uppercase = lowercase = 0;
printf("password : ");
gets(&str);
printf(" %s", str);
password(&str,together,numbers,symbols,i,uppercase,lowercase);
printf("Number of letters = %d\n", together);
printf("number uppercase = %d\n", uppercase);
printf("Number lowecase = %d\n", lowercase);
printf("numbers = %d\n", numbers);
printf("Simboli = %d", symbols);
if (together >= 8 && numbers >= 2){
printf("\nPassword is correct");
}else{
printf("\nPassword isn't correct");
}
return 0;
}
void password(char * str[],int together,int numbers,int symbols,int i,int uppercase,int lowercase){
while(*str[i]!='\0')
{
if((*str[i]>='a' && *str[i]<='z') || (*str[i]>='A' && *str[i]<='Z'))
{
together++;
}
else if(*str[i]>='0' && *str[i]<='9')
{
numbers++;
}
else if(*str[i] >= 'A' && *str[i] <= 'Z') {
uppercase++;
}
else if(*str[i] >= 'a' && *str[i] <= 'z'){
lowercase++;
}
else
{
symbols++;
}
i++;
}
}
I get zeroes for every number,letter & symbol program runs without any error I tried changing some stuff but I am currently learning C so it is hard for me!
Few points : 1) There is no need to pass i as argument since you can get the length of your string with strlen from string.h (which returns a size_t type and not an int). 2) You passed your arguments as value, which means that they'll only change locally to your function. 3) Don't use the gets function that is not safe, you may want to use fgets or scanf.
#include <string.h>
#include <stdio.h>
void password(char *str, int *together, int *numbers, int *symbols, int *uppercase, int *lowercase){
for(size_t i=0; i<strlen(str); i++){
if(str[i] >= '0' && str[i] <= '9') (*numbers)++;
if(str[i] >= 'A' && str[i] <= 'Z') (*uppercase)++;
if(str[i] >= 'a' && str[i] <= 'z') (*lowercase)++;
if(str[i] >= '!' && str[i] <= '/') (*symbols)++;
}
*together = (*uppercase) + (*lowercase);
}
int main(void){
char str[100] = "Pa$$w0/2d";
int together = 0;
int numbers = 0;
int symbols = 0;
int uppercase = 0;
int lowercase = 0;
password(str, &together, &numbers, &symbols, &uppercase, &lowercase);
printf("Letters = %d\n", together);
printf("Uppercase = %d\n", uppercase);
printf("Lowercase = %d\n", lowercase);
printf("Numbers = %d\n", numbers);
printf("Symbols = %d\n", symbols)
return 0;
}
Maybe some property is passing unnoticed to me, but when 'i' is 1 it just freeze. When i input whatever string, 'j' variable goes to 700 or 2000 in different executions. The code goal is to output repetitive letters if you input "cheese" the output should be "eee". What am i doing wrong?
#include <stdio.h>
char * repeticoes(char *s) {
int index = 0;
for (int i = 0;( s[i] != '\0'); i++) //problem starts when i is > 0
{
for (int j = 0; ( s[j] != '\0'); j++)
{
if (s[i] == s[j])
{
printf("%c == %c\ni %d j %d\n", s[i], s[j],i,j);
s[index++] = s[i];
}
else
{
printf("not happening %c != %c\ni %d j %d\n", s[i],s[j],i,j);
}
}
}
s[++index] = '\0';
return s;
}
main() {
char input[21];
printf("str 1\n");
fgets(input, 20, stdin);
repeticoes(input);
printf("duplicated letters %s\n", input);
}
You need to start the inner loop at the next character after the one being processed in the outer loop, otherwise you'll process the same pair of characters twice, as well as testing a character against itself when i == j.
You should also break out of the inner loop as soon as you find a match. You'll find later matches in a future iteration of the outer loop. Otherwise you'll process the same pair twice again.
And you shouldn't increment index before assigning the null character after the loop. It was already incremented when adding the repetition.
#include <stdio.h>
char * repeticoes(char *s) {
int index = 0;
for (int i = 0;( s[i] != '\0'); i++) //problem starts when i is > 0
{
for (int j = i+1; ( s[j] != '\0'); j++)
{
if (s[i] == s[j])
{
printf("%c == %c\ni %d j %d\n", s[i], s[j],i,j);
s[index++] = s[i];
break;
}
else
{
printf("not happening %c != %c\ni %d j %d\n", s[i],s[j],i,j);
}
}
}
s[index] = '\0';
return s;
}
int main() {
char input[21];
printf("str 1\n");
fgets(input, 20, stdin);
repeticoes(input);
printf("duplicated letters %s\n", input);
}
With an array of letters to count and an array of counts. Once the letters have been counted, loop through the input again and set the letters that have a count greater than one.
#include <stdio.h>
void repeticoes ( char *s) {
char tocount[] = "abcdefghijklmnopqrstuvwxyz";
size_t len = sizeof tocount;
size_t index = 0;
int count[len];
for (int j = 0; j < len; j++) {
count[j] = 0;
}
for (int i = 0;( s[i] != '\0'); i++) {
for (int j = 0; j < len; j++) {
if ( s[i] == tocount[j]) {
count[j]++;
}
}
}
for (int i = 0;( s[i] != '\0'); i++) {
for (int j = 0; j < len; j++) {
if ( s[i] == tocount[j] && 1 < count[j]) {
s[index] = s[i];
index++;
}
}
}
s[index] = '\0';
}
int main ( void) {
char input[21];
printf("str 1\n");
fgets(input, 20, stdin);
repeticoes(input);
printf("duplicated letters %s\n", input);
return 0;
}
I'm fairly new to coding and am currently enrolled in a course at school teaching C. We have been given an assignment that requires as follows:
Write a program that inputs three lines of text and uses the function strchr to determine the number of occurrences of each letter of the alphabet (uppercase and lowercase should be counted as the same). Store the totals for each letter in an array and print the result.
Here's the code I've written so far:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 100
int main(void) {
int alphabet[26] = { 0 };
char string[3][SIZE];
int i, j;
int c;
printf("Enter three lines of text:\n");
for (i = 0; i <= 2; i++) {
fgets(string[i], SIZE, stdin);
}
for (i = 0; i <= 2; i++) {
for (j = 0; &string[i][j] != '\0'; j++) {
string[i][j] = tolower(string[i][j]);
}
for (j = 0; &string[i][j] != '\0'; j++) {
if (strchr(&string[i][j], ('a' + j)) != NULL) {
alphabet[j]++;
}
}
}
printf("\n");
for (i = 0; i < 26; i++) {
printf("%c: %d\n", ('a' + i), alphabet[i]);
}
printf("\n");
return 0;
}
The problem with the current program I have is that the counter for the array alphabet[] doesn't seem to be incrementing the count properly.
The output should count, for example, increment by 1 for each occurrence of every letter, but the results are all 0.
Any other tips or words of advice are greatly appreciated!
Your test for end of line is wrong,
no need to loop several time just parse the string and count the letters
You want letter between 'a'and 'z', check if the char is in the range and count it
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define SIZE 100
int main(void) {
int alphabet[26] = { 0 };
char string[3][SIZE];
int i, j;
printf("Enter three lines of text:\n");
for (i = 0; i <= 2; i++) {
fgets(string[i], SIZE, stdin);
}
for (i = 0; i <= 2; i++) {
for (j = 0; string[i][j] != '\0'; j++) {
char c = tolower(string[i][j]);
if (c >= 'a' && c <= 'z') {
alphabet[c - 'a']++;
}
}
}
printf("\n");
for (i = 0; i < 26; i++) {
printf("%c: %d\n", ('a' + i), alphabet[i]);
}
printf("\n");
return 0;
}
You need to change loop terminating condition from &string[i][j] != '\0'; to string[i][j] != '\0';
strchr will return the address from the string where match was found, so you should save the non null address to pass it to next call to strchr
Something like this:
tmpPtr = &string[i][j];
while ((tmpPtr=strchr(tmpPtr, string[i][j])) != NULL) {
alphabet[string[i][j] - 'a']++;
}
I'm writing a program to count spaces and vowels and it didnĀ“t work, I think I did an infinite loop.I'll show you my code:
int contar_p(char a[100]) {
int i = 0, spaces = 1;
while (a[i] != '\0' && i < 100) {
if (a[i] == ' ') {
spaces += 1;
i++;
}
}
return spaces;
}
int contar_v(char b[100]) {
int i = 0, counter = 0;
while (b[i] != '\0' && i < 100) {
if (b[i] == 'a' || b[i] == 'e' || b[i] == 'i' || b[i] == 'o' || b[i] == 'u') {
counter += 1;
}
i++;
}
return counter;
}
int main(void){
char phrase[100];
int words = 0, vowels = 0;
printf("write a phrase ");
gets(phrase);
palabras = contar_p(phrase);
vocales = contar_v(phrase);
printf("%d\n", words);
printf("%d", vowels);
return 0;
}
The loop
while (a[i]!='\0'&&i<100){
if(a[i]==' '){
spaces+=1;
i++;
}
}
is an infinite loop. Place i++ outside the if. Change it to
while (a[i]!='\0'){ // No need of condition i < 100
if(a[i]==' '){
spaces+=1;
}
i++;
}
Maybe another approach will help you to understand things easier, i'm mean you do know that there are A,E,I,O,U also and not only a,e,i,o,u. You should never use gets instead use fgets, anyway take a look at the following program:
#include <stdlib.h>
#include <stdio.h>
void countVowels(char* array){
int i,j,v;
i=0;
int count = 0;
char vowel[]={'a','e','i','o','u','A','E','I','O','U'};
while(array[i]!='\0'){
for(v=0;v<10;v++){
if (array[i]==vowel[v]){
j=i;
while(array[j]!='\0'){
array[j]=array[j+1];
j++;
}
count++;
i--;
break;
}
}
i++;
}
printf("Found %d Vowels\n",count);
}
void contar_p(char a[100]) {
int i = 0, spaces = 0;
for(i=0;a[i]!='\0';i++){
if(a[i]==' ')
spaces++;
}
printf("Found %d Spaces\n",spaces);
}
int main(void){
char a[]="aa bb EOU cc ii";
countVowels(a);
contar_p(a);
return 0;
}
Output:
Found 7 Vowels
Found 4 Spaces