Counting how many letters are repeated in a string
My program should first read an entered string word
and then count the amount of repeated letters.
For example, if I enter apple
it should print 1
, but instead it prints 4
.
I suppose word[i] = word[i + 1]
isn't the right way to count?
#include<stdio.h>
int main()
int i, j, wordLength = 0, alphabeticalSort, counter = 0, swap;
char word[51];
scanf("%s", word);
while (word[wordLength] != '')
wordLength++;
...
for (i = 0; i < wordLength; i++)
if (word[i] = word[i + 1])
counter++;
printf("nNumber of repeated letters: %d", counter);
...
return 0;
c string
|
show 3 more comments
My program should first read an entered string word
and then count the amount of repeated letters.
For example, if I enter apple
it should print 1
, but instead it prints 4
.
I suppose word[i] = word[i + 1]
isn't the right way to count?
#include<stdio.h>
int main()
int i, j, wordLength = 0, alphabeticalSort, counter = 0, swap;
char word[51];
scanf("%s", word);
while (word[wordLength] != '')
wordLength++;
...
for (i = 0; i < wordLength; i++)
if (word[i] = word[i + 1])
counter++;
printf("nNumber of repeated letters: %d", counter);
...
return 0;
c string
4
if (word[i] = word[i + 1])
should beif (word[i] == word[i + 1])
. Also, consider includingstring.h
and usingstrlen()
instead of manually counting the length. These common functions exist for a reason.
– cdhowie
Nov 11 '18 at 21:20
Since it produces the wrong answer, what you've got clearly isn't what's needed. You need to count the number of occurrences of each letter (does 'Abracadabra' count 'A' and 'a' separately). Then you need to count the number of letters that have more than 1 occurrence.
– Jonathan Leffler
Nov 11 '18 at 21:21
Thanks guys. Also, is includingstring.h
necessary when usingstrlen
,strcmp
, etc? I've used them in the past, but never included thestring.h
library.
– alcatraz
Nov 11 '18 at 21:22
1
Are you counting repeated adjacent letters, or repeated occurrences of a letter anywhere in the word (string)? It matters because 'abracadabra' has no adjacent repeats, but it repeats a, b, and r non-adjacently. So, it's count might be 0 or 3, depending on this detail in the specification.
– Jonathan Leffler
Nov 11 '18 at 21:25
1
You really don't need thewordLength
loop; you could simply modify yourfor
loop to use:for (int i = 0; word[i] != ''; i++)
to control it. If you must havewordLength
, then usewordLength = strlen(word);
instead of your loop. Yes,strlen()
returns the length of a string.
– Jonathan Leffler
Nov 11 '18 at 21:32
|
show 3 more comments
My program should first read an entered string word
and then count the amount of repeated letters.
For example, if I enter apple
it should print 1
, but instead it prints 4
.
I suppose word[i] = word[i + 1]
isn't the right way to count?
#include<stdio.h>
int main()
int i, j, wordLength = 0, alphabeticalSort, counter = 0, swap;
char word[51];
scanf("%s", word);
while (word[wordLength] != '')
wordLength++;
...
for (i = 0; i < wordLength; i++)
if (word[i] = word[i + 1])
counter++;
printf("nNumber of repeated letters: %d", counter);
...
return 0;
c string
My program should first read an entered string word
and then count the amount of repeated letters.
For example, if I enter apple
it should print 1
, but instead it prints 4
.
I suppose word[i] = word[i + 1]
isn't the right way to count?
#include<stdio.h>
int main()
int i, j, wordLength = 0, alphabeticalSort, counter = 0, swap;
char word[51];
scanf("%s", word);
while (word[wordLength] != '')
wordLength++;
...
for (i = 0; i < wordLength; i++)
if (word[i] = word[i + 1])
counter++;
printf("nNumber of repeated letters: %d", counter);
...
return 0;
c string
c string
edited Nov 11 '18 at 21:43
asked Nov 11 '18 at 21:18
alcatraz
164
164
4
if (word[i] = word[i + 1])
should beif (word[i] == word[i + 1])
. Also, consider includingstring.h
and usingstrlen()
instead of manually counting the length. These common functions exist for a reason.
– cdhowie
Nov 11 '18 at 21:20
Since it produces the wrong answer, what you've got clearly isn't what's needed. You need to count the number of occurrences of each letter (does 'Abracadabra' count 'A' and 'a' separately). Then you need to count the number of letters that have more than 1 occurrence.
– Jonathan Leffler
Nov 11 '18 at 21:21
Thanks guys. Also, is includingstring.h
necessary when usingstrlen
,strcmp
, etc? I've used them in the past, but never included thestring.h
library.
– alcatraz
Nov 11 '18 at 21:22
1
Are you counting repeated adjacent letters, or repeated occurrences of a letter anywhere in the word (string)? It matters because 'abracadabra' has no adjacent repeats, but it repeats a, b, and r non-adjacently. So, it's count might be 0 or 3, depending on this detail in the specification.
– Jonathan Leffler
Nov 11 '18 at 21:25
1
You really don't need thewordLength
loop; you could simply modify yourfor
loop to use:for (int i = 0; word[i] != ''; i++)
to control it. If you must havewordLength
, then usewordLength = strlen(word);
instead of your loop. Yes,strlen()
returns the length of a string.
– Jonathan Leffler
Nov 11 '18 at 21:32
|
show 3 more comments
4
if (word[i] = word[i + 1])
should beif (word[i] == word[i + 1])
. Also, consider includingstring.h
and usingstrlen()
instead of manually counting the length. These common functions exist for a reason.
– cdhowie
Nov 11 '18 at 21:20
Since it produces the wrong answer, what you've got clearly isn't what's needed. You need to count the number of occurrences of each letter (does 'Abracadabra' count 'A' and 'a' separately). Then you need to count the number of letters that have more than 1 occurrence.
– Jonathan Leffler
Nov 11 '18 at 21:21
Thanks guys. Also, is includingstring.h
necessary when usingstrlen
,strcmp
, etc? I've used them in the past, but never included thestring.h
library.
– alcatraz
Nov 11 '18 at 21:22
1
Are you counting repeated adjacent letters, or repeated occurrences of a letter anywhere in the word (string)? It matters because 'abracadabra' has no adjacent repeats, but it repeats a, b, and r non-adjacently. So, it's count might be 0 or 3, depending on this detail in the specification.
– Jonathan Leffler
Nov 11 '18 at 21:25
1
You really don't need thewordLength
loop; you could simply modify yourfor
loop to use:for (int i = 0; word[i] != ''; i++)
to control it. If you must havewordLength
, then usewordLength = strlen(word);
instead of your loop. Yes,strlen()
returns the length of a string.
– Jonathan Leffler
Nov 11 '18 at 21:32
4
4
if (word[i] = word[i + 1])
should be if (word[i] == word[i + 1])
. Also, consider including string.h
and using strlen()
instead of manually counting the length. These common functions exist for a reason.– cdhowie
Nov 11 '18 at 21:20
if (word[i] = word[i + 1])
should be if (word[i] == word[i + 1])
. Also, consider including string.h
and using strlen()
instead of manually counting the length. These common functions exist for a reason.– cdhowie
Nov 11 '18 at 21:20
Since it produces the wrong answer, what you've got clearly isn't what's needed. You need to count the number of occurrences of each letter (does 'Abracadabra' count 'A' and 'a' separately). Then you need to count the number of letters that have more than 1 occurrence.
– Jonathan Leffler
Nov 11 '18 at 21:21
Since it produces the wrong answer, what you've got clearly isn't what's needed. You need to count the number of occurrences of each letter (does 'Abracadabra' count 'A' and 'a' separately). Then you need to count the number of letters that have more than 1 occurrence.
– Jonathan Leffler
Nov 11 '18 at 21:21
Thanks guys. Also, is including
string.h
necessary when using strlen
, strcmp
, etc? I've used them in the past, but never included the string.h
library.– alcatraz
Nov 11 '18 at 21:22
Thanks guys. Also, is including
string.h
necessary when using strlen
, strcmp
, etc? I've used them in the past, but never included the string.h
library.– alcatraz
Nov 11 '18 at 21:22
1
1
Are you counting repeated adjacent letters, or repeated occurrences of a letter anywhere in the word (string)? It matters because 'abracadabra' has no adjacent repeats, but it repeats a, b, and r non-adjacently. So, it's count might be 0 or 3, depending on this detail in the specification.
– Jonathan Leffler
Nov 11 '18 at 21:25
Are you counting repeated adjacent letters, or repeated occurrences of a letter anywhere in the word (string)? It matters because 'abracadabra' has no adjacent repeats, but it repeats a, b, and r non-adjacently. So, it's count might be 0 or 3, depending on this detail in the specification.
– Jonathan Leffler
Nov 11 '18 at 21:25
1
1
You really don't need the
wordLength
loop; you could simply modify your for
loop to use: for (int i = 0; word[i] != ''; i++)
to control it. If you must have wordLength
, then use wordLength = strlen(word);
instead of your loop. Yes, strlen()
returns the length of a string.– Jonathan Leffler
Nov 11 '18 at 21:32
You really don't need the
wordLength
loop; you could simply modify your for
loop to use: for (int i = 0; word[i] != ''; i++)
to control it. If you must have wordLength
, then use wordLength = strlen(word);
instead of your loop. Yes, strlen()
returns the length of a string.– Jonathan Leffler
Nov 11 '18 at 21:32
|
show 3 more comments
3 Answers
3
active
oldest
votes
It's best to split what you're doing into two functions, one of which will count the number of occurrences of each letter.
#define LETTERS 26
/* counts number of occurrences of each lowercase letter in str. Result is placed in outCounts,
assuming it has room */
void countletters(char *str, int outCounts)
memset(outCounts, 0, LETTERS * sizeof (int));
for (; *str; ++str)
if (isupper(*str))
*str = tolower(str);
if (islower(*str))
++outCounts[*str - 'a'];
Then you write another function will examines the outCounts
array, which is modified. If a letter is repeated, the corresponding member of this array will be greater than one. I leave this as an exercise to the reader.
add a comment |
You have:
for (i = 0; i < wordLength; i++)
if (word[i] = word[i + 1])
counter++;
Look at the 'comparison' inside the loop; you're giving the value of word[i+1]
to word[i+1]
. This condition will always be true unless the word[i+1]
's value is a 0 or a null char which is also a 0 in the ASCII table.
Remember: the equality operator in C is always ==
, so replace the =
with ==
.
add a comment |
int main()
char string[80];
int c = 0, count[26] = 0, x;
printf("Enter a stringn");
//gets(string);
scanf("%s", string);
while (string[c] != '')
if (string[c] >= 'a' && string[c] <= 'z')
x = string[c] - 'a';
count[x]++;
c++;
for (c = 0; c < 26; c++)
printf("%c occurs %d times in the string.n", c + 'a', count[c]);
return 0;
5
No! Never usegets
– stackptr
Nov 11 '18 at 21:24
2
See Why thegets()
function is too dangerous to be used — ever!
– Jonathan Leffler
Nov 11 '18 at 21:27
thank you for your suggestions :)
– Sari Masri
Nov 11 '18 at 21:28
1
You assume that all the characters in the string are lower-case alphabetic; I'm not convinced that's a safe assumption. I think you'd do better to ignore spaces and punctuation and digits (or non-alphabetic characters). Maybe map upper-case to lower-case and convert appropriately, or maybe count them separately; it isn't clear from the question which is expected.
– Jonathan Leffler
Nov 11 '18 at 21:29
Ah.scanf
. No better, is it?.
– Bob Jarvis
Nov 11 '18 at 23:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253339%2fcounting-how-many-letters-are-repeated-in-a-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's best to split what you're doing into two functions, one of which will count the number of occurrences of each letter.
#define LETTERS 26
/* counts number of occurrences of each lowercase letter in str. Result is placed in outCounts,
assuming it has room */
void countletters(char *str, int outCounts)
memset(outCounts, 0, LETTERS * sizeof (int));
for (; *str; ++str)
if (isupper(*str))
*str = tolower(str);
if (islower(*str))
++outCounts[*str - 'a'];
Then you write another function will examines the outCounts
array, which is modified. If a letter is repeated, the corresponding member of this array will be greater than one. I leave this as an exercise to the reader.
add a comment |
It's best to split what you're doing into two functions, one of which will count the number of occurrences of each letter.
#define LETTERS 26
/* counts number of occurrences of each lowercase letter in str. Result is placed in outCounts,
assuming it has room */
void countletters(char *str, int outCounts)
memset(outCounts, 0, LETTERS * sizeof (int));
for (; *str; ++str)
if (isupper(*str))
*str = tolower(str);
if (islower(*str))
++outCounts[*str - 'a'];
Then you write another function will examines the outCounts
array, which is modified. If a letter is repeated, the corresponding member of this array will be greater than one. I leave this as an exercise to the reader.
add a comment |
It's best to split what you're doing into two functions, one of which will count the number of occurrences of each letter.
#define LETTERS 26
/* counts number of occurrences of each lowercase letter in str. Result is placed in outCounts,
assuming it has room */
void countletters(char *str, int outCounts)
memset(outCounts, 0, LETTERS * sizeof (int));
for (; *str; ++str)
if (isupper(*str))
*str = tolower(str);
if (islower(*str))
++outCounts[*str - 'a'];
Then you write another function will examines the outCounts
array, which is modified. If a letter is repeated, the corresponding member of this array will be greater than one. I leave this as an exercise to the reader.
It's best to split what you're doing into two functions, one of which will count the number of occurrences of each letter.
#define LETTERS 26
/* counts number of occurrences of each lowercase letter in str. Result is placed in outCounts,
assuming it has room */
void countletters(char *str, int outCounts)
memset(outCounts, 0, LETTERS * sizeof (int));
for (; *str; ++str)
if (isupper(*str))
*str = tolower(str);
if (islower(*str))
++outCounts[*str - 'a'];
Then you write another function will examines the outCounts
array, which is modified. If a letter is repeated, the corresponding member of this array will be greater than one. I leave this as an exercise to the reader.
edited Nov 12 '18 at 1:34
answered Nov 11 '18 at 21:40
stackptr
8,28023356
8,28023356
add a comment |
add a comment |
You have:
for (i = 0; i < wordLength; i++)
if (word[i] = word[i + 1])
counter++;
Look at the 'comparison' inside the loop; you're giving the value of word[i+1]
to word[i+1]
. This condition will always be true unless the word[i+1]
's value is a 0 or a null char which is also a 0 in the ASCII table.
Remember: the equality operator in C is always ==
, so replace the =
with ==
.
add a comment |
You have:
for (i = 0; i < wordLength; i++)
if (word[i] = word[i + 1])
counter++;
Look at the 'comparison' inside the loop; you're giving the value of word[i+1]
to word[i+1]
. This condition will always be true unless the word[i+1]
's value is a 0 or a null char which is also a 0 in the ASCII table.
Remember: the equality operator in C is always ==
, so replace the =
with ==
.
add a comment |
You have:
for (i = 0; i < wordLength; i++)
if (word[i] = word[i + 1])
counter++;
Look at the 'comparison' inside the loop; you're giving the value of word[i+1]
to word[i+1]
. This condition will always be true unless the word[i+1]
's value is a 0 or a null char which is also a 0 in the ASCII table.
Remember: the equality operator in C is always ==
, so replace the =
with ==
.
You have:
for (i = 0; i < wordLength; i++)
if (word[i] = word[i + 1])
counter++;
Look at the 'comparison' inside the loop; you're giving the value of word[i+1]
to word[i+1]
. This condition will always be true unless the word[i+1]
's value is a 0 or a null char which is also a 0 in the ASCII table.
Remember: the equality operator in C is always ==
, so replace the =
with ==
.
edited Nov 11 '18 at 22:54
Jonathan Leffler
560k896651017
560k896651017
answered Nov 11 '18 at 22:35
Mehdi Fracso
163
163
add a comment |
add a comment |
int main()
char string[80];
int c = 0, count[26] = 0, x;
printf("Enter a stringn");
//gets(string);
scanf("%s", string);
while (string[c] != '')
if (string[c] >= 'a' && string[c] <= 'z')
x = string[c] - 'a';
count[x]++;
c++;
for (c = 0; c < 26; c++)
printf("%c occurs %d times in the string.n", c + 'a', count[c]);
return 0;
5
No! Never usegets
– stackptr
Nov 11 '18 at 21:24
2
See Why thegets()
function is too dangerous to be used — ever!
– Jonathan Leffler
Nov 11 '18 at 21:27
thank you for your suggestions :)
– Sari Masri
Nov 11 '18 at 21:28
1
You assume that all the characters in the string are lower-case alphabetic; I'm not convinced that's a safe assumption. I think you'd do better to ignore spaces and punctuation and digits (or non-alphabetic characters). Maybe map upper-case to lower-case and convert appropriately, or maybe count them separately; it isn't clear from the question which is expected.
– Jonathan Leffler
Nov 11 '18 at 21:29
Ah.scanf
. No better, is it?.
– Bob Jarvis
Nov 11 '18 at 23:07
add a comment |
int main()
char string[80];
int c = 0, count[26] = 0, x;
printf("Enter a stringn");
//gets(string);
scanf("%s", string);
while (string[c] != '')
if (string[c] >= 'a' && string[c] <= 'z')
x = string[c] - 'a';
count[x]++;
c++;
for (c = 0; c < 26; c++)
printf("%c occurs %d times in the string.n", c + 'a', count[c]);
return 0;
5
No! Never usegets
– stackptr
Nov 11 '18 at 21:24
2
See Why thegets()
function is too dangerous to be used — ever!
– Jonathan Leffler
Nov 11 '18 at 21:27
thank you for your suggestions :)
– Sari Masri
Nov 11 '18 at 21:28
1
You assume that all the characters in the string are lower-case alphabetic; I'm not convinced that's a safe assumption. I think you'd do better to ignore spaces and punctuation and digits (or non-alphabetic characters). Maybe map upper-case to lower-case and convert appropriately, or maybe count them separately; it isn't clear from the question which is expected.
– Jonathan Leffler
Nov 11 '18 at 21:29
Ah.scanf
. No better, is it?.
– Bob Jarvis
Nov 11 '18 at 23:07
add a comment |
int main()
char string[80];
int c = 0, count[26] = 0, x;
printf("Enter a stringn");
//gets(string);
scanf("%s", string);
while (string[c] != '')
if (string[c] >= 'a' && string[c] <= 'z')
x = string[c] - 'a';
count[x]++;
c++;
for (c = 0; c < 26; c++)
printf("%c occurs %d times in the string.n", c + 'a', count[c]);
return 0;
int main()
char string[80];
int c = 0, count[26] = 0, x;
printf("Enter a stringn");
//gets(string);
scanf("%s", string);
while (string[c] != '')
if (string[c] >= 'a' && string[c] <= 'z')
x = string[c] - 'a';
count[x]++;
c++;
for (c = 0; c < 26; c++)
printf("%c occurs %d times in the string.n", c + 'a', count[c]);
return 0;
edited Nov 11 '18 at 22:57
Jonathan Leffler
560k896651017
560k896651017
answered Nov 11 '18 at 21:24
Sari Masri
1297
1297
5
No! Never usegets
– stackptr
Nov 11 '18 at 21:24
2
See Why thegets()
function is too dangerous to be used — ever!
– Jonathan Leffler
Nov 11 '18 at 21:27
thank you for your suggestions :)
– Sari Masri
Nov 11 '18 at 21:28
1
You assume that all the characters in the string are lower-case alphabetic; I'm not convinced that's a safe assumption. I think you'd do better to ignore spaces and punctuation and digits (or non-alphabetic characters). Maybe map upper-case to lower-case and convert appropriately, or maybe count them separately; it isn't clear from the question which is expected.
– Jonathan Leffler
Nov 11 '18 at 21:29
Ah.scanf
. No better, is it?.
– Bob Jarvis
Nov 11 '18 at 23:07
add a comment |
5
No! Never usegets
– stackptr
Nov 11 '18 at 21:24
2
See Why thegets()
function is too dangerous to be used — ever!
– Jonathan Leffler
Nov 11 '18 at 21:27
thank you for your suggestions :)
– Sari Masri
Nov 11 '18 at 21:28
1
You assume that all the characters in the string are lower-case alphabetic; I'm not convinced that's a safe assumption. I think you'd do better to ignore spaces and punctuation and digits (or non-alphabetic characters). Maybe map upper-case to lower-case and convert appropriately, or maybe count them separately; it isn't clear from the question which is expected.
– Jonathan Leffler
Nov 11 '18 at 21:29
Ah.scanf
. No better, is it?.
– Bob Jarvis
Nov 11 '18 at 23:07
5
5
No! Never use
gets
– stackptr
Nov 11 '18 at 21:24
No! Never use
gets
– stackptr
Nov 11 '18 at 21:24
2
2
See Why the
gets()
function is too dangerous to be used — ever!– Jonathan Leffler
Nov 11 '18 at 21:27
See Why the
gets()
function is too dangerous to be used — ever!– Jonathan Leffler
Nov 11 '18 at 21:27
thank you for your suggestions :)
– Sari Masri
Nov 11 '18 at 21:28
thank you for your suggestions :)
– Sari Masri
Nov 11 '18 at 21:28
1
1
You assume that all the characters in the string are lower-case alphabetic; I'm not convinced that's a safe assumption. I think you'd do better to ignore spaces and punctuation and digits (or non-alphabetic characters). Maybe map upper-case to lower-case and convert appropriately, or maybe count them separately; it isn't clear from the question which is expected.
– Jonathan Leffler
Nov 11 '18 at 21:29
You assume that all the characters in the string are lower-case alphabetic; I'm not convinced that's a safe assumption. I think you'd do better to ignore spaces and punctuation and digits (or non-alphabetic characters). Maybe map upper-case to lower-case and convert appropriately, or maybe count them separately; it isn't clear from the question which is expected.
– Jonathan Leffler
Nov 11 '18 at 21:29
Ah.
scanf
. No better, is it?.– Bob Jarvis
Nov 11 '18 at 23:07
Ah.
scanf
. No better, is it?.– Bob Jarvis
Nov 11 '18 at 23:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253339%2fcounting-how-many-letters-are-repeated-in-a-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
if (word[i] = word[i + 1])
should beif (word[i] == word[i + 1])
. Also, consider includingstring.h
and usingstrlen()
instead of manually counting the length. These common functions exist for a reason.– cdhowie
Nov 11 '18 at 21:20
Since it produces the wrong answer, what you've got clearly isn't what's needed. You need to count the number of occurrences of each letter (does 'Abracadabra' count 'A' and 'a' separately). Then you need to count the number of letters that have more than 1 occurrence.
– Jonathan Leffler
Nov 11 '18 at 21:21
Thanks guys. Also, is including
string.h
necessary when usingstrlen
,strcmp
, etc? I've used them in the past, but never included thestring.h
library.– alcatraz
Nov 11 '18 at 21:22
1
Are you counting repeated adjacent letters, or repeated occurrences of a letter anywhere in the word (string)? It matters because 'abracadabra' has no adjacent repeats, but it repeats a, b, and r non-adjacently. So, it's count might be 0 or 3, depending on this detail in the specification.
– Jonathan Leffler
Nov 11 '18 at 21:25
1
You really don't need the
wordLength
loop; you could simply modify yourfor
loop to use:for (int i = 0; word[i] != ''; i++)
to control it. If you must havewordLength
, then usewordLength = strlen(word);
instead of your loop. Yes,strlen()
returns the length of a string.– Jonathan Leffler
Nov 11 '18 at 21:32