trying to write a letter counter in C - c

I am trying to make a program that counts the number of letters that the user has input. When I run my program, it says error: expected ';' after expression on the line count_letters(){ // writing the function to count number of letters? Does a function need a semicolon at the end? I also feel that I am not approaching this problem correctly, can someone please enlighten me :(
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int count_letters(); //function to count letters
int number_of_letters; //length of the string input
int letterCount; // counter for number of letters in the string input
int main(void) {
string text = get_string("Text: "); // getting string input
number_of_letters = strlen(text); //storing the length of string here
printf("Number of letters: %i\n", letterCount); //printing the number of letters in the string input
count_letters() { // writing the function to count number of letters
for (int i = 0; i < number_of_letters; i++) {
if (isalpha(text[i])) {
letterCount++;
}
}
}
}

While you are free to use strlen to get the initial number of characters in text, it isn't necessary. In C, the last character in a "string" is the nul-terminating character '\0' (which has the ASCII value of 0). This is what differentiates a normal array of characters, from a string. It is how all string functions know when to stop scanning for characters.
So you don't need to know beforehand how many characters there are in a string. For example, take the string "hello" entered at the "Text: " prompt where you have declared.
string text = get_string("Text: ");
When you enter "hello" at the prompt:
Text: hello
The string is stored in memory as:
+---+---+---+---+---+---+
| h | e | l | l | o |\0 |
+---+---+---+---+---+---+
^
|
text
where the pointer text points to the address of the first character of the string in memory. Using the fact that a string ends with a nul-terminating character, you can simply scan forward from the start until your reach '\0' (equivalent to plain old 0).
You can use a for loop and iterate using indexes:
for (int i = 0; text[i]; i++)
// do whatever with the character text[i]
Or you can use a pointer and simply increment the pointer so it points to the next character in the string until the '\0' is reached:
string p = text;
while (*p) {
// do whatever with *p (the character at that address)
p++;
}
Putting the last version into your int count_letters (string s) function (that passes a pointer to your string as a parameter to the function) and returns an int representing the number of letters (including only [a-zA-Z]), your function reduces to:
int count_letters (string s)
{
int n = 0;
while (*s) /* while not the nul-character */
if (isalpha (*s++)) /* check if current is letter, advance ptr */
n++; /* increment letter count */
return n; /* return letter count */
}
You main() function, not needing to call strlen() then reduces to:
int main (void) {
string text = get_string ("Text: ");
printf ("Number of letters: %d\n", count_letters(text));
}
Putting it altogether and including the needed headers, you would have:
#include <stdio.h>
#include <ctype.h>
#include "cs50.h"
int count_letters (string s)
{
int n = 0;
while (*s) /* while not the nul-character */
if (isalpha (*s++)) /* check if current is letter, advance ptr */
n++; /* increment letter count */
return n; /* return letter count */
}
int main (void) {
string text = get_string ("Text: ");
printf ("Number of letters: %d\n", count_letters(text));
}
Example Use/Output
Compile and link with the libcs50.so and then, for example you would have:
$ ./bin/ltrcountcs50
Text: hello world
Number of letters: 10
Understanding what a "string" in C is (outside of the unfortunate choice of the typedef char* string; used by CS50), allows you to handle scanning over the characters in your string without needing to know how many characters are included beforehand. (and also helps you understand why failing to provide a nul-terminated string to any of the C string function that expect a nul-terminated string as a parameter results in Undefined Behavior -- they have no way of knowing when to stop scanning for characters).
Look things over and let me know if you have further questions.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int count_letters(); //function to count letters
int number_of_letters; //length of the string input
//initialize letterCount to 0 so that doing letterCount++ does not add 1 to a garbage value
int letterCount = 0; // counter for number of letters in the string input
//define string as a global value if you don't want to refer to it again and again by pass it as parameter or reference
string text;
void count_letters() { //since it's not returning anything, it's void not integer
for(int i = 0; i < number_of_letters; i++) {
if(isalpha(text[i])
letterCount++;
}
}
int main(void){
text = get_string("Text: "); // getting string input
number_of_letters = strlen(text); //storing the length of string here
printf("Number of letters: %i\n", letterCount); //printing the number of letters in the string input
}
Hope this helps. All the comments are there where any modifications are made

I might take your knowledge for being too basic, please forgive me if it is the case.
I believe you have made a small mistake possibly led by the way another language works. The problem lies in the way you have declared count_letters().
To properly declare a function in C, first get out of any existing function, then enter any variable type as a return type for your function, the name of your function, then, in parenthesis, your function's parameters. And after that, your function's code can be inserted between brackets, like you did.
Note you can also declare the function without the code, then put the code for the function lower.
And you might also want to declare your string externally to avoid dealing with pointers.
Here's an example of function declaration:
int foo(int amount)
Hence your code should look a little like this:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
void count_letters(); //function to count letters
int number_of_letters; //length of the string input
int letterCount; // counter for number of letters in the string input
string text;
int main(void) {
text = get_string("Text: "); // getting string input
number_of_letters = strlen(text); //storing the length of string here
count_letters();
printf("Number of letters: %i\n", letterCount); //printing the number of letters in the string input
}
void count_letters() { // writing the function to count number of letters
for (int i = 0; i < number_of_letters; i++) {
if (isalpha(text[i])) {
letterCount++;
}
}
}
Now please excuse me if I did not understand or answer your question correctly, it would be with pleasure I'd improve myself if I could get more detail. Lacking the cs50.h library, I cannot know for sure my code works.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int count_letters(char *); //function to count letters
int main(void)
{
char* text = get_string("Text: "); // getting string input
printf("Number of letters: %i\n", count_letters(text)); //printing the number of letters in the string input
}
int count_letters(char *text)
{ // writing the function to count number of letters
int letterCount,number_of_letters; //length of the string input
number_of_letters = strlen(text); //storing the length of string here
for(int i = 0; i < number_of_letters; i++){
if(isalpha(text[i]))
{
letterCount++;
}
return letterCount;
}

Related

I mixed up two programs in the cs50 sandbox in c?

I mixed up two programs in the cs50 sandbox, one was to find the the number of characters in an array and other was the print these characters. I know the program is garbage but could anyone explain me what is the compiler doing here?
When I ran this, the output starts printing alphanumeric text and never stops Thanks
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Name: ");
int n = 0;
while (strlen(s) != '\0')
{
n++;
printf("%c", n);
}
}
You have multiple problems with the code you show, here's a couple of them:
strlen(s) will never be zero as you never modify or remove characters from the string, which means you have an infinite loop
n is an integer and not a character so should be printed with the %d format specifier
'\0' is (semantically) a character, representing the string terminator, it's not (semantically) the value 0
To fix the first problem I suspect you want to iterate over every character in the string? Then that could be done with e.g.
for (int i = 0; i < strlen(s); ++i)
{
printf("Current character is '%c'\n", s[i]);
}
But if all you want is to could the number of characters in the string, then that's what strlen is already gives you:
printf("The number of characters in the string is %zu\n", strlen(s));
If you want to count the length of the string without using strlen then you need to modify the loop to loop until you hit the terminator:
for (n = 0; s[n] != '\0'; ++n)
{
// Empty
}
// Here the value of n is the number of characters in the string s
All of this should be easy to figure out by reading any decent beginners book.
while (strlen(s) != '\0') is wrong. '\0' equals 0. There string length is never 0, so the loop keeps going on forever, printing integers interpreted as characters.
You can either use the indexes to go through the string characters by using the variable "n" or you can increment the pointer of the string that you have received from the standard input to go through all of its characters.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Name: ");
/* First way using n to iterate */
int n = 0;
for (n = 0; n < strlen(s); ++n)
{
printf("%c", s[n]);
}
printf("\n");
/* Second way increment the string pointer*/
while (strlen(s) != '\0')
{
printf("%c", *s); //print the value of s
s++; // go to the next character from s
}
printf("\n");
return 0;
}

program outputs exit phrase instead of words stored in C

So I was working on an assignment for school, and had written up a variation of this code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
// This program takes an input of strings and prints them out with a new line separating each one.
int main() {
char *WordArray[MAX]; //initializing variables
int i = 0;
int count = 0;
printf("enter up to 100 words, that are 20 characters maximum \n");
for (i = 0; i <100; i++){ //runs while there's less than 100 inputs
char Array[1];
scanf("%s",Array); //stores string in the array
if (strcmp(Array, "STOP") == 0) { //compares the string with stop, and if it is, it breaks out of the loop
break;
}
WordArray[i]=Array; //stores the string in the pointer array
}
printf("The output is\n");
for (count = 0; count<i; count++){ //counts up to the amount of words stored
printf("%s\n",WordArray[count]); //outputs each pointer string
}
}
and I noticed that the output was printing "STOP" instead of the values stored. Anyone have any answers to why and/or how to fix it? I know one of the methods is to switch to a 2D array instead of using pointers, but I'm still baffled as to why a program like this wouldn't work.
Your char Array[1]; isn't large enough to store any but an empty string. Also, when it works, every pointer will point to the same string, which will be the last entry you made. This makes some corrections where commented.
#include <stdio.h>
#include <stdlib.h> // instead of ctype.h
#include <string.h>
#define MAX 100
// This program takes an input of strings and prints them out with a new line separating each one.
int main() {
char *WordArray[MAX];
int i = 0;
int count = 0;
printf("enter up to 100 words, that are 20 characters maximum \n");
for (i = 0; i <100; i++){
char Array[21]; // increase size of array
scanf("%20s",Array); // limit entry length
if (strcmp(Array, "STOP") == 0) {
break;
}
WordArray[i] = strdup(Array); // allocate memory for and copy string
}
printf("The output is\n");
for (count = 0; count<i; count++){
printf("%s\n",WordArray[count]);
}
// free each string's memory
for (count = 0; count<i; count++){
free(WordArray[count]);
}
}
Program output:
enter up to 100 words, that are 20 characters maximum
one two three STOP
The output is
one
two
three
Edit: note that your code contains another undefined behaviour besides the too-short string char Array[1] which is that you dereference the pointer you stored in char *WordArray[MAX];. The scope of Array is inside the for loop, and theoretically ceases to exist after the loop completes, so the pointer you store is invalid. Here, the word entered is duplicated with strdup so that doesn't apply.

C programming. Function to generate a string of random letters using only arrays and then pointers

Im trying to code a program in C to generate a string containing random letters using only arrays first and then again using pointers. I've looked at many other questions but is not quite what I'm trying to accomplish. I can really use help please.
Function 1- Generates a string with random upper
case letter A-Z with 40 characters.
Function 2- Function to let user enter a string
with random upper case letter and a replacement character.
Function 3- Searches string1 from function 1 and replaces
occurences of any character from string 2 (user entered) with
replacement character.
OUTPUT EX.
String 1- "AABBCCDDEEFFGGHHABCDEFGH"
String 2- "BE"
Replacement char- "3"
Filtered string- AA33CCDD33FFGGHHA3CD3FGH.
This is what I have so far, Im not very good with arrays.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int s1 [41];
srand(time(NULL));
int i;
for (i = 0; i < 41; i++)
{
s1 [i] = rand();
}
return 0;
}
Any help will be appreciated.
Thanks alot.
#include <stdio.h>
#include <stdlib.h>
void rand_str(char* txt, size_t sz)
{
int i=sz-1;
while( i --> 0 )
{
txt[i] = 'A' + rand() % 26;
}
printf("Random Str: %.*s\n", sz+i, txt);
}
void fn2(char* tgt, size_t sz, char* repl )
{
puts("String 2: ");
fgets(tgt, sz, stdin);
puts("Replacement Char: ");
*repl = getchar();
}
void search_replace(char* txt, char* tgt, char repl)
{
while(*tgt != '\0')
{
while ((strchr(txt, *tgt) ? (tgt[strchr(txt, *tgt)-tgt] = repl) : 0) == repl);
tgt++;
}
}
int main(void)
{
char txt[41] = {0};
char tgt[40] = {0};
char repl;
rand_str(txt, sizeof(txt));
fn2(tgt, sizeof(tgt), &repl);
search_replace(txt, tgt, repl);
return !printf("Filtered String: %s\n", txt);
}
Please note that I did not compile any of this code. It might have some typo and/or runtime errors. The concept is correct though and you should understand the code first and not just copy it.
Function 1:
#include <stdlib.h> // Important! rand() function that generate random function is in that library!
//This function returns a pointer of an array (arr). In other words it returns the **address** of the first character of the array.
// Assuming arr is valid!
char* randomString(char* arr){
// This part does not REALLLYY matters it just makes sure the random will truly be random...
time_t t;
srand((unsigned) time(&t)); // Seeds the random function.
//------------------
//Looping the array assigning random letters:
int i = 0;
while(i<SIZE){
arr[i] = 'A'+(rand()%('Z'-'A'+1));// 'A' has a numerical value, we want the range from 'A' to 'Z' to be random. 'Z'-'A' is the range of letters (26) because its a modulu if the modulu was just 'Z'-'A' (26) it wouldnt print Z. 'Z' is the 26th letter, 26%26 is zero, it will not give 'Z' this is why I increased 'Z'-'A' by 1 so the modulu will include 'Z' as random latter.
i = i + 1;
}
arr[i] = 0;// String terminator also called NULL.
return "lol";
}
Function 2:
#include <string.h>
int replace(char* inputString, char* userInput,char replacement ){
/* e.g.
inputString = "ABSDSADASBBBAA";//Generate yourself... (Might want to user function 1)
userInput = "AB"; // You need to do the user input yourself...
replacement = 'D';
*/
int i = 0;
while(i<strlen(inputString)){
int j = 0;
while(j<strlen(userInput)){
if(inputString[i]==userInput[j]){
inputString[i] = replacement;
}
j = j+1;
}
i = i + 1;
}
}
Function 3:
int main(){
// Just use regular IO libraries to get user's input...
// Assuming you did that, I will hard code the values (you need to do the IO e.g. gets())
char str[SIZE];
randomString(str); // Requirement #1 reuse of function 1
char * userInput = "AB"; // You need to do the user input yourself...
char replacement = 'D';// You need to do the user input yourself...
replace(str, userInput, replacement)//Requirement #2
return 0;
}

Converting user input to an array of characters, and filtering letters from other characters?

#include "stdafx.h"
#include "stdlib.h"
#include <ctype.h>
int num = 0;
int i = 0;
int ch = 0;
int letter_index_in_alphabet(int ch) {
if (isalpha(ch) == true) {
char temp_str[2] = { ch };
num = strtol(temp_str, NULL, 36) - 9;
printf("%d is a letter, with %d as its location in the alphabet!", ch, num);
}
else {
return -1;
}
}
int main()
{
char input_str[10];
printf("Please enter a series of up to 10 letters and numbers: \n");
fgets(input_str, 10, stdin);
for (i == 0; i <= 10; i++) {
ch = input_str[i];
letter_index_in_alphabet(ch);
}
return 0;
}
Hello everyone, this is my first post on SOF! The goal of this program is to read characters from the standard input to EOF. For each character, report if it is a letter. If it is a letter, print out its respective index in the alphabet ('a' or 'A' = 1, 'b' or 'B' = 2..etc). I have been searching some other posts on stackoverflow and this has helped me get this far(using fgets and strtol functions). I have no visible syntax errors when I run this code, but after I enter a string of characters (ex: 567gh3fr) the program crashes.
Basically, I am trying to use 'fgets' to bring each character entered into a string with the appropriate index. Once I have that string, I check each index for a letter and if it is, I print the number assigned to that letter of the alphabet.
Any help or insight into why this isn't working as intended is greatly appreciated, Thanks!
You have a few problems.
First, char input_str[10] is only big enough for the user to enter 9 characters, not 10, because you need to allow one character for the null byte that ends a string.
Second, your loop goes too far. For a string with 10 characters, indexes go up to 9, not 10. It also should stop when it gets to the null byte, since the user might not have entered all 9 characters.
To get the position in the alphabet, you can simply subtract the value of A or a from the value of the character. Use tolower() or toupper() to convert the character to the case that you're going to use. Your method works, but it's overly complicated and confusing.
letter_index_in_alphabet() is declared to return int. But when the character is a letter, it doesn't execute a return statement. I'm not sure why it's supposed to return something, since you never use the return value, but I've changed it to return the position (maybe the caller should be the one that prints the message, so the function just does the calculation).
In the for loop, it should be i = 0 to perform an assignment, not i == 0 which is comparison.
You also shouldn't use global variables so much. And system header files should have <> around them, not "".
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
int letter_index_in_alphabet(int ch) {
if (isalpha(ch)) {
int num = tolower(ch) - 'a' + 1;
printf("%d is a letter, with %d as its location in the alphabet!\n", ch, num);
return num;
} else {
return -1;
}
}
int main()
{
char input_str[10];
printf("Please enter a series of up to 9 letters and numbers: \n");
fgets(input_str, sizeof(input_str), stdin);
for (int i = 0; input_str[i]; i++) {
letter_index_in_alphabet(input_str[i]);
}
return 0;
}

String Editor (not functioning correctly)

I'm new to C and have been set the following problem. I am to write a program where a string can be entered and stored, I should then enter two integer values which will then be used to remove characters from the string, afterwards the result should be printed. Once the program works it should be converted into a function.
I have created a program that will split the entered string into two strings which store the chars I want to keep in two buffers, afterwards the two strings are concatenated to give the resultant edited string. The problem I am having is that when I print the edited string I get random characters at the end and sometimes in between the two strings and I think it's because the strings are not being null terminated correctly. I hope that someone is able to help, Thanks :)
#include <stdio.h>
#include <string.h>
int main ()
{
char string [25];
char buffer1 [25];
char buffer2 [25];
int start;
int remove;
int i;
int finish;
int size;
int numbercopy;
int A, B, C;
printf("Enter a string: ");
gets(string);
printf("\nEnter a starting character position: ");
scanf("%d", &start);
printf("\nHow many characters would you like to remove? ");
scanf("%d", &remove);
finish = (start+remove);
size = strlen(string);
numbercopy = (size-finish);
strncpy(&buffer1[0], &string[0], start);
buffer1[start] = '\0';
strncpy(&buffer2[0], &string[finish], numbercopy);
buffer2[numbercopy] = '\0';
A = strlen(buffer1);
B = strlen(buffer2);
C = (A+B);
strcat(buffer1, buffer2);buffer1[C] = '\0';
for (i=0; i<25; i++)
{
printf("%c", buffer1[i]);
}
return 0;
}
Since it is a string, you do not need to print it character by character. Also, the loop indicates that only 25 char strings will be printed. If a string (buffer1) is shorter in length(<25), garbage values will be printed, if a string is is larger (>25), some chars will not be printed.
Change this:
for (i=0; i<25; i++)
{
printf("%c", buffer1[i]);
}
to this:
printf("%s", buffer1);

Resources