Infinite loop in C counting spaces - c

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

Related

Garbage value encounter in -C

In this code I have got a string input- "WUBABCWUBNAWBWUB".
here i need to remove the word "WUB" and print the remaining.Example: "ABC NAWB";
i have almost solved it but the problem is when i give the input(given above) I get a garbage value at the end.It shows: "ABC NAWB~#".I cant understand whats wrong with the code,please help me to remove those garbage values.
#include <stdio.h>
#include <string.h>
int main() {
char str[201], original[201];
int cnt = 0, i, j = 0;
int len = strlen(str);
gets(str);
for(i = 0; i < len-3; i++) {
if(str[i] == 'W' && str[i+1] == 'U' && str[i+2] == 'B' && i != len) {
if(i != 0 && i != len-3) {
original[cnt] = ' '; //here i changed "WUB" into a blank line
j++;
cnt++;
}
i = i+2;
} else {
original[j] = str[i];
cnt++;
j++;
}
}
printf("%c %c\n", original[j], original[j+1]);
printf("%s", original);
printf("\n\nj=%d,i=%d,cnt=%d", j, i, cnt);
}
A few modifications:
I have added a '\0' the end at the new string
I have modified the code such that it works even if the end is not equal to WUB (maybe useless modification)
I have removed the redundant j index
I have replaced the gets function with the more secure fgets
#include<stdio.h>
#include<string.h>
int main()
{
char str[201],original[201];
fgets(str, 200, stdin);
int cnt=0,i, len=strlen(str);
for(i = 0; i <= len-3; i++) {
if(str[i]=='W' && str[i+1]=='U' && str[i+2]=='B' && i!=len) {
if(i!=0 && i!=len-3) {
original[cnt++] = ' '; //here i changed "WUB" into a blank line
}
i = i+2; // to jump over the "WUB" word
} else {
original[cnt++] = str[i];
}
}
for (int j = i; j < len; j++) { // to deal with the case the string doesn't terminate with "WUB"
original[cnt++] = str[j];
}
original[cnt++] = '\0';
//printf("%c %c\n",original[cnt],original[cnt+1]);
printf("%s",original);
//printf("\n\n,i=%d,cnt=%d",i,cnt);
}
Strings are actually a one-dimensional array of characters terminated by a null character '\0'. so in your program original is a character array and once the values are assigned to it, at last '\0' should be there so adding just one line of code may help you get correct output.
original[j]='\0';
thanks guys,i've solved the problem.The problem was solved after giving
original[j]='/0';
after the loop.I also used the cnt integer to avoid blanks more than once.Here is my new code.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str[201],original[201];
fgets(str,201,stdin);
int cnt=0,i,j=0,len=strlen(str);
for(i=0;str[i]!='\0';i++){
if(str[i]=='W' && str[i+1]=='U' && str[i+2]=='B')
{
if(i!=0 && i!=len-3&&cnt==0&&j!=0)
{
original[j] = ' ';
cnt++;
j++;
}
i=i+2;
}
else
{
original[j] = str[i];
cnt=0;
j++;
}
}
original[j]='\0';
printf("%s",original);
}

Way to call function Pig_Latin_Converter correctly?

#include <stdio.h>
#include <string.h>
#include <conio.h>
void Pig_Latin_Converter(char *a);
int main() {
char password[50];
printf("Please input password: ");
int passw = 0;
do {
password[passw] = getch();
if(password[passw] != '\r') {
printf("*");
}
passw++;
}
while(password[passw - 1] != '\r');
password[passw - 1] = '\0';
Pig_Latin_Converter(password);
return 0;
}
void Pig_Latin_Converter (char *a) {
int v = 0, c = 0, pos = 0,sv = 0, ch;
strlwr(a);
if(a[0]=='a'|| a[0]=='e'|| a[0]=='i'|| a[0]=='o'|| a[0]=='u'){
++sv;
printf("\n\nPig Latin Equivalent: %syay", *a);
}
else
{
++c;
for (int i = 0; a[i] != '\0'; ++i)
{
if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' ||
a[i] == 'o' || a[i] == 'u') {
++v;
pos=i;
break;}
}
}
if(c==1 && v==1)
{
printf("\n\n");
printf("Pig Latin Equivalent: ");
for(int i=pos; i<strlen(*a); i++) {
printf("%c", a[i]);
}
for(int i=0;i<pos;i++)
{
printf("%cay", a[i]);
}
}
else if(c==1 && sv==0)
{
printf("\n\n");
printf("Pig Latin Equivalent: %syay", *a);
}
}
Hello, I'm a beginner in using user-defined functions used in C programming. This program is tasked to input masked password then translate it to Pig Latin. But, I kept on coming up with errors on this. Is there a way to solve this? I have a huge gut feel that the problem is only inside the function Pig Latin.

Compare code in reverse each word in sentence

I have school task. To reverse each word in sentence, so example :
Input: Fried chicken, fried duck.
Output: deirF nekcihc, deirf kcud.
So except dot and comma it's not reversed.
The first code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int i, n, titik = 0, coma = 0;
char s[5001];
char c[5001];
char *tok;
scanf("%[^\n]s", s);
if (s[0] == ' ')
printf(" ");
tok = strtok(s, " ");
while (tok != NULL) {
strcpy(c, tok);
n = strlen(c);
for (i = n; i >= 0; i--) {
if (c[i] == ',') {
coma = 1;
} else
if (c[i] == '.') {
titik = 1;
} else
printf("%c", c[i]);
}
if (coma) {
printf(",");
coma = 0;
} else
if (titik){
printf(".");
titik = 0;
}
tok = strtok(NULL," ");
if (tok == NULL)
printf("\n");
else
printf(" ");
}
}
Second code is
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
int i, j, n, prana = 0, titik = 0, coma = 0, end = 0;
char s[5001];
scanf("%[^\n]s", s);
n = strlen(s);
for (i = 0; i <= n; i++) {
if (isspace(s[i]) || iscntrl(s[i])) {
if (iscntrl(s[i]))
end = 1;
for (j = i - 1; j >= prana; j--) {
if (s[j] == '.') {
titik = 1;
} else
if (s[j] == ',') {
coma = 1;
} else
printf("%c", s[j]);
}
prana = i + 1;
if (titik) {
titik = 0;
if (end)
printf(".");
else
printf(". ");
} else
if (coma) {
coma = 0;
if (end)
printf(",");
else
printf(", ");
} else {
if (end)
printf("");
else
printf(" ");
}
}
}
printf("\n");
return 0;
}
Why the second code is accepted in test case?, but first code is not.
I tested the result it's same. Really identical in md5 hash.
The output of the two codes id different, because you print the terminating null character for each token in the first code. This loop:
for (i = n; i >=0 ; i--) ...
will have i == n in its first iteration. For a C string of length n, s[n] is the terminating null. This character may not show in the console, but it is part of the output.
To fix the loop, you could start with i = n - 1, but C uses inclusive lower bounds and exclusive upper bounds, and a more idomatic loop syntax is:
i = n;
while (i-- > 0) ...
Not related to your question at hand, but your codes are rather complicated, because they rely on many assumptions: words separated by spaces; only punctuation is comma or stop; repeated punctuation marks are ignored, special case for last word.
Here's a solution that treats all chunks of alphabetic characters plus the apostrophe as words and reverses them in place:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
void reverse(char *str, int i, int j)
{
while (i < j) {
int c = str[--j];
str[j] = str[i];
str[i++] = c;
}
}
int main()
{
char str[512];
int begin = -1;
int i;
if (fgets(str, sizeof(str), stdin) == NULL) return -1;
for (i = 0; str[i]; i++) {
if (isalpha((unsigned char) str[i]) || str[i] == '\'') {
if (begin == -1) begin = i;
} else {
if (begin != -1) {
reverse(str, begin, i);
begin = -1;
}
}
}
printf("%s", str);
return 0;
}

K&R Exe. 1-23 : Some Complications

"Exercise 1-23. Write a program to remove all comments from a C program.
Don't forget to handle quoted strings and character constants
properly. C comments do not nest." K&R pg.34
Basically, I have two questions:
1)I'm completely new coding and I wanted to know if I'm at least thinking the problem the right way.
2)The code was built to ignore // till \n or /* till */. But whit the /* comment it always leaves one /.
Input: abc/*comment*/123
Output: abc/123
Input: abc/*123
Output: abc/
#include <stdio.h>
char s[1000]; //Principal array
int countS; //Number of char in array
int deletSingleLineComments(void);
int deletMultiLineComments(void);
int main(void){
int c;
while((c=getchar())!=EOF){
s[countS]=c;
++countS;
}
deletMultiLineComments(); //Function 1
deletSingleLineComments(); //Function 2
printf("\ns[]=\n%s\n\ncountS[]=%d\n",s,countS);
}
//Functions 1
int deletMultiLineComments(void){
char t[1000];
int i=0;
int inComment=0;
int diff=0;
int a,b,c;
while(i<=countS){
t[i]=s[i];
++i;
}
i=0;
while(i<=countS){
if(t[i]=='/' && t[i+1]=='*'){
inComment=1;
}
if(inComment==1){
++diff; //to equilibrate the number
}
if(inComment==0){
s[i-diff]=t[i];
}
if(t[i]=='*' && t[i+1]=='/'){
inComment=0;
}
++i;
}
s[i-diff+1]='\0';
countS=i-diff;
printf("\nt[]=\n%s\n",t);
}
//Function 2
int deletSingleLineComments(void){
int i=0;
char t[1000];
int inComment=0;
int diff=0;
while(i<=countS){
t[i]=s[i];
++i;
}
i=0;
while(i<=countS){
if(t[i] == '/' && t[i+1] == '/'){
inComment=1;
}
if(t[i]=='\n'){
inComment=0;
}
if(inComment==1){
++diff;
}
if(inComment==0){
s[i-diff]=t[i];
}
s[i-diff+1]='\0';
++i;
}
countS=i-diff;
}
Thank you.
while(i<=countS){ t[i]=s[i];... }
Note that character strings are zero based. For example "ABC" has length 3, it starts at zero index, and the last valid index is 2 (not 3). Therefore you should change the condition to i < string_length
while(i < countS){ t[i]=s[i];... }
Also be careful when accessing t[i+1], because while i is valid, i+1 can be out of bound.
if (i < (countS - 1))
if(t[i]=='/' && t[i+1]=='*')
In order to assign one string to another, you can introduce a second variable k, and increment k after each assignment. This method is easier (in my opinion) than using the diff variable and doing additions and subtractions.
In addition, rather than char t[1000];, you can use char *t = malloc(countS); to declare a temporary variable of length countS, then it has to be freed at the end with free(t). If your compiler supports variable length array, you can just put char t[countS].
Example:
char s[1000]; //Principal array
int countS; //Number of char in array
//Functions 1
void deletMultiLineComments(void)
{
char *t = malloc(countS);
int i = 0;
int k = 0;
int inComment = 0;
while (i < countS)
{
t[i] = s[i];
++i;
}
i = 0;
while (i < countS)
{
if (i < countS - 1)
if (t[i] == '/' && t[i + 1] == '*')
{
inComment = 1;
i+=2;
continue;
}
if (inComment == 1)
{
if (i < countS - 1)
if (t[i] == '*' && t[i + 1] == '/')
{
inComment = 0;
i+=2;
continue;
}
}
if (inComment == 0)
{
s[k] = t[i];
k++;
}
++i;
}
free(t);
s[k] = '\0';
countS = k;
printf("mulitline comment removed %s\n", s);
}
//Function 2
void deletSingleLineComments(void)
{
char *t = malloc(countS);
int i = 0;
int k = 0;
int inComment = 0;
while (i < countS)
{
t[i] = s[i];
++i;
}
i = 0;
while (i < countS)
{
if (i < countS - 1)
if (t[i] == '/' && t[i + 1] == '/')
{
inComment = 1;
i += 2;
continue;
}
if (t[i] == '\n')
{
inComment = 0;
}
if (inComment == 0)
{
s[k] = t[i];
k++;
}
i++;
}
free(t);
s[k] = '\0';
countS = k;
printf("single comment removed %s\n", s);
}
int main(void)
{
//get input
scanf("%s", s);
countS = 0;
while (s[countS]) countS++;
deletMultiLineComments(); //Function 1
deletSingleLineComments(); //Function 2
}

C-printing a histogram

This is a question from K&R:-
Write a program to print a histogram of the lengths of words in its input.It is easy to draw the histogram with bars horizontal; but a vertical orientation is more challenging.
I am not supposed to use any library functions because this is only a tutorial introduction!
I have written the following program to do so but it have some bugs:-
1)If there is more than one white-space character between words, the program doesn't work as expected.
2)How do I know the maximum value of 'k' I mean how to know how many words are there in the input?
Here is the code:-
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define MAX_WORDS 100
int main(void)
{
int c, i=0, k=1, ch[MAX_WORDS] = {0};
printf("enter the words:-\n");
do
{
while((c=getchar())!=EOF)
{
if(c=='\n' || c==' ' || c=='\t')
break;
else
ch[i]++;
}
i++;
}
while(i<MAX_WORDS);
do
{
printf("%3d|",k);
for(int j=1;j<=ch[k];j++)
printf("%c",'*');
printf("\n");
k++;
}
while(k<10);
}
This program will work fine even if there are more than one newline characters in between the two words and numWords will give you the numbers of words.
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int ch, cha[100] = {0}, k = 1;
int numWords = 0;
int numLetters = 0;
bool prevWasANewline = true; //Newlines at beginning are ignored
printf("Enter the words:-\n");
while ((ch = getchar()) != EOF && ch != '\n')
{
if (ch == ' ' || ch == '\t')
prevWasANewline = true; //Newlines at the end are ignored
else
{
if (prevWasANewline) //Extra nelines between two words ignored
{
numWords++;
numLetters = 0;
}
prevWasANewline = false;
cha[numWords] = ++numLetters;
}
}
do
{
printf("%3d|",k);
for(int j=0;j<cha[k];j++)
printf("%c",'*');
printf("\n");
k++;
} while(k <= numWords);
return 0;
}
Its kind of an old thread, but i had the same problem. The code above works like charm, but its kind of an overkill for the knowledge we have with K&R in the first 20 pages.
I had no idea what they meant by histogram printing and that code helped me with that.
Anyway i wrote a code myself with the knowledge i gained through the book, i hope it will be helpful to someone.
Forgive me if its a bit messy, i am just a beginner myself :D
#include <stdio.h>
#define YES 1
#define NO 0
int main(void)
{
int wnumb [100];
int i, inword, c, n, k;
n = (-1);
for (i = 0; i <=100; ++i) {
wnumb [i] = 0;
}
inword = NO;
while ((c = getchar()) != EOF) {
if ( c == ' ' || c == '\n' || c == '\t' ) {
inword = NO;
}
else if (inword == NO) {
++n;
++wnumb [n];
inword = YES;
}
else {
++wnumb [n];
}
}
for (i = 0; i <= 100; ++i) {
if (wnumb [i] > 0) {
printf ("\n%3d. | ", (i+1));
for (k = 1; k <= wnumb[i]; ++k) {
printf("*");
}
}
}
printf("\n");
}
Here is example of simple Histogram(Vertically)
#include <stdio.h>
int main()
{
int c, i, j, max;
int ndigit[10];
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
max = ndigit[0];
for (i = 1; i < 10; ++i) /* for Y-axis */
if (max < ndigit[i])
max = ndigit[i];
printf("--------------------------------------------------\n");
for (i = max; i > 0; --i) {
printf("%.3d|", i);
for (j = 0; j < 10; ++j)
(ndigit[j] >= i) ? printf(" X ") : printf(" ");
printf("\n");
}
printf(" ");
for (i = 0; i < 10; ++i) /* for X-axis */
printf("%3d", i);
printf("\n--------------------------------------------------\n");
return 0;
}

Resources