I have char* pointer and trying to count the length of each word. but I am not getting any result in debugger (just empty space). whatever I change , I dont get any result. the code :
void wordsLen(char* text, int* words, int n)
{
int i, count = 0, s = 0;
//words[countWords(text)]; // not important
for (i=0; i < n; i++)
{
if (text[i] != ' ')
{
count++;
}
else
{
printf("%d",count);
}
printf("%d",count);//if I add this it types the count from 1 to the end
}
}
I try to insert this array :
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define N 100
void main()
{
char t[] = "hello my name is.";
int cum[N];
wordsLen(t, cum, strlen(t));
getch();
}
Since am not getting any result , I would like to know why , and is there any problem with the code for counting the length of words? like is it good for counting the length of word or do I need to change something.
Here is a little modification of your function.
void wordsLen(char* text, int* words, int n)
{
int i, count = 0, s = 0;
for (i=0; i < n; i++)
{
if (text[i] != ' ')
{
count++;
}
else
{
printf("%d",count);
count = 0;
}
}
printf("%d", count);
}
Here some code for counting the words and there length:
void addWord(int* numberOfWords, int* count) {
*numberOfWords = *numberOfWords + 1;
*count = 0;
}
void print(int numberOfWords, int count) {
printf("number of words %d \n", numberOfWords);
printf("word length %d \n", count);
}
void wordsLen(char* text, int* words, int n)
{
int i = 0;
int count = 0;
int numberOfWords = 0;
//words[countWords(text)]; // dynamicly set the length
for (i = 0; i < n; i++)
{
char ch = text[i];
if (ch != ' ')
{
count++;
}
if (count > 0 && (ch == ' ' || (i == n - 1)))
{
print(numberOfWords + 1, count);
addWord(&numberOfWords, &count);
}
}
}
Try this:
void wordsLen(char* text, int* words, int n)
{
int i, count = 0, s = 0;
//words[countWords(text)]; // not important
for (i=0; i <= n; i++)
{
if (text[i] != ' ' && text[i] != '\0')
{
count++;
}
else
{
printf("%d ",count);
count = 0;
}
//printf("%d",count);
}
}
Related
I have made one program, where you enter a few characters (10 max). It makes you a list, count average length of surnames, tell about how much different names. But the problem is, when I enter the last number (10) - it sorts me it incorrectly (like 1, 10, 2, 3, 4, 5, 6, 7, 8, 9). Beneath I will present my code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct people {
char num[2];
char surname[20];
char name[10];
} peoples[10], c;
int main()
{
int i, j, k = 0, l = 0, m = 0, n = 0;
float s = 0;
char str[100];
system("chcp 1251 > nul");
for (i = 0, j = 0; i < 10; i++, j++)
{
printf("Enter number, surname, name %d of your human: ", i + 1);
gets(str);
while (str[n] != '\n')
{
if (str[n] != ' ')
{
peoples[j].num[k] = str[n];
}
else
break;
n++;
k++;
}
n++;
k = 0;
while (str[n] != '\n')
{
if (str[n] != ' ')
{
peoples[j].surname[k] = str[n];
}
else
break;
n++;
k++;
}
n++;
k = 0;
while (str[n] != '\n')
{
if (str[n] != '\0')
{
peoples[j].name[k] = str[n];
}
else
break;
n++;
k++;
}
n = 0;
k = 0;
}
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 10; j++)
{
if (!strcmp(peoples[i].name, peoples[j].name))
m = 1;
}
if (m == 0)
l++;
m = 0;
s = s + strlen(peoples[i].surname);
}
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
if (strcmp(peoples[j].num, peoples[j+1].num) > 0)
{
c = peoples[j+1];
peoples[j+1] = peoples[j];
peoples[j] = c;
}
for (i = 0; i < 10; i++)
{
printf("%s ", peoples[i].num);
printf("%s ", peoples[i].name);
printf("%s ", peoples[i].surname);
printf("\n");
}
printf("\nYou have %d different names\n", l);
printf("Avarege lenght of surname is = %f\n", s / 10);
}
If you want to give numeric input, then use actual numeric data.
Change the num field to become an int instead of a single-character string:
struct people {
int num;
char surname[20];
char name[10];
};
Use fgets to read the line:
fgets(str, sizeof str, stdin);
[Error checking left as exercise for reader]
Then use e.g. sscanf for parse your string:
sscanf(str, "%d %s %s", &peoples[j].num, &peoples[j].name, &peoples[j].name);
[Error checking left as exercise for reader]
And finally, instead of doing your own sorting use the standard qsort function:
qsort(peoples, 10, sizeof(struct people), &compare_people_num);
With the comparison function being something like this:
int compare_people_num(const void *a, const void *b)
{
const struct people *p1 = a;
const struct people *p2 = b:
return p1->num - p2->num; // Change order to reverse sort
}
Using sscanf and qsort will make your code much simpler and easier to understand.
I'm having some problem with saveing the vaule of the longest fence.
I tried this:
int longestFence(char input [], int size)
{
int i , max = 0, count = 0;
for(i = 0; i < size ; i++)
{
if(input[i] == '|' && input[i] == '-')
{
count = 1;
}
if(input[i] != input[i + 1])
{
count++ - 1;
}
}
return count;
}
In practice, to detect is the fence is still valid, you just have to check if the current symbol is different or not than the previous one.
You also have to check if the current count is longer or not than the previous longest one.
Besides, I modified the random string generator: the current one is rather inefficient.
In addition, the string generated is not terminated by the Null character. I also modified it.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10
int longestFence (char input[], int size) {
if (size == 0) return 0;
int max_count = 0;
int count = 1;
for (int i = 1; i < size; ++i) {
if (input[i] != input[i-1]) {
count++;
} else {
if (count > max_count) max_count = count;
count = 1;
}
}
if (count > max_count) max_count = count;
return max_count;
}
int main() {
char string[MAX+1];
char symbols[] = {'|', '-'};
srand (time(NULL));
int length = rand() % (MAX+1);
for (int i = 0; i < length; ++i) {
int val = (rand() / 16) % 2;
string[i] = symbols[val];
}
string[length] = '\0';
printf ("String is: %s\n", string);
printf ("Longest fence = %d\n", longestFence (string, length));
return 0;
}
I have an assignment to write code that printing all combinations of N char. For example, if the input is 3, the expected output must be "aaa aab aac aba ... ccc". But my code looping over and over again. Here's my code.
#include <stdio.h>
#ifndef MAX
#define MAX 5
#endif
void comb(char kar[], int size, int index) {
// string = aaa
// lim = 'd'
char lim = 'a' + size;
while (index != -1) {
if (kar[size-1] != lim) { // != c
for (int i = 0; i < size; i++) {
printf("%s ", kar);
kar[size-1]+=1;
}
return comb(kar, size, index);
} else {
while (kar[index-1] == lim && index != -1) {
kar[index-1]='a';
index--;
}
kar[index-1] += 1;
return comb(kar, size, size);
}
}
}
int main(int argc, char const *argv[]) {
int n;
char kar[MAX];
printf("Input N char : ");
scanf(" %d", &n);
for (int j = 0; j < n; j++) {
kar[j] = 'a';
}
comb(kar, n, n);
return 0;
}
I'm a little bit confused and I have no idea where is the mistake. Thank you.
The problem has been solved. I changed some elements in the comb() and added the pow() function to define the recursion limit.
int comb(char kar[], int size, int index, int limit) {
char lim = 97 + size;
int limit_value = pow(size,size);
if(limit == limit_value){
return 1;
} else {
if (index < size-1) {
printf("%s ", kar);
kar[size-1]+=1;
return comb(kar, size, index+1, limit+1);
} else {
int cek = index;
printf("%s ", kar);
while (kar[cek] == lim-1 ) {
kar[cek]=97;
cek-=1;
}
kar[cek] += 1;
return comb(kar, size, 0, limit+1);
}
}
}
I am creating my own string word length function for an assignment(well it's at least apart of the assignment), but for some reason I am getting a really weird error. It's supposed to give me the number of words within a string, but for some reason the value is not being saved. Even when I run it, it says that the length is 21 for the string, "Happy go lucky charms". Can someone tell me what's wrong here?
#include <stdio.h>
#include <ctype.h> // For the letter checking functions
int findLengthString( char *word){
int i = 0;
int length = 0;
for ( i = 0; word[i] != '\0'; i++){
length++;
}
printf("length is %d", length );
return length;
}
int totalWords(char *str) {
int i = 0;
int total = 0;
int hold = findLengthString(str);
for ( i = 0; i < hold; i++ ) {
if ( str[i] == ' ' || str[i+1] == '\0') {
printf("total is %d", total);
total++;
}
return total;
}
}
int main() {
int hold = 0;
char arr[] = "Happy go lucky charms";
hold = totalWords(arr);
printf("hold is %d", hold);
return 0;
}
The immediate problem is that the return is in a wrong position. If you format your code properly, you will clearly see what's wrong:
int totalWords(char *str) {
int i = 0;
int total = 0;
int hold = findLengthString(str);
for ( i = 0; i < hold; i++ ) {
if ( str[i] == ' ' || str[i+1] == '\0') {
printf("total is %d", total);
total++;
}
return total; // <<== You return in the loop
}
// You should return here
}
However, this is the smaller problem. A bigger problem is that the logic of your code is incorrect: rather than counting words, it counts spaces, so a string like this "Hello, world!" will produce 10 instead of 2.
To fix this problem you need to change your algorithm in such a way that you add 1 to total only if you have seen a non-space after the last increment:
int totalWords(char *str) {
int i = 0;
int total = 0;
int hold = findLengthString(str);
int inWord = 0;
for ( i = 0; i < hold; i++ ) {
if ( str[i] == ' ' || str[i+1] == '\0') {
total+= inWord;
inWord = 0;
} else {
inWord = 1;
}
}
printf("total is %d", total);
return total;
}
Demo.
#include <stdio.h>
include
include
int totalWords(char str[]) {
int i = 0;
int total = 0;
int hold = strlen(str);
for ( i = 0; i < hold; i++ ) {
if ( str[i] == ' ' || str[i+1] == '\0') {
printf("total is %d\n", total);
total++;
}
}
return total;
}
int main() {
int hold = 0;
char arr[] = "Happy go lucky charms";
hold = totalWords(arr);
printf("hold is %d", hold);
return 0;
}
I am trying to make permutations of strings with up to 8 characters. The problem is it must be done with recursion and it must be in lexicographical order. I found one solution with the recursion but it only works for 4 characters max. After that, it starts to mess up again.
void swap(char* a, char* b){
char temp = *a;
*a = *b;
*b = temp;
}
void recursion(char* arr, int start, int n){
if (start == (n-1)){
printf("%s\n", arr);
return;
}
for (int i = start; i < n; i++){
recursion(arr, start+1, n);
swap(arr+start+1, arr+n-1);
int j = start+1;
while (j < n && arr[start] > arr[j]){
j++;
}
if (j >= n){
continue;
}
swap(arr+start, arr+j);
}
swap(arr+start+1, arr+n-1);
}
int main(int argc, char *argv[]) {
char arr[9];
char charakter;
int m = 0;
while (scanf("%c", &charakter) != EOF){
if (charakter == '\n'){
break;
}
else if (isalpha(charakter) || isdigit(charakter)){
arr[m] = charakter;
m++;
}
else{
fprintf(stderr, "Error!\n");
return 100;
}
}
arr[m] = '\0';
int n = strlen(arr);
int start = 0;
recursion(arr, start, n);
return 0;
}
Any idea how to fix the recursion function?
Your solution is strange, have a look here and here a fix:
void recursion(char *arr, int start, int n) {
if (start == n) {
printf("%s\n", arr);
return;
}
for (int i = start; i < n; i++) {
swap(arr + start, arr + i);
recursion(arr, start + 1, n);
swap(arr + start, arr + i);
}
}
here a proper solution:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
static void recursion(char *str, size_t n, size_t max) {
if (n < max) {
recursion(str, n + 1, max);
for (size_t i = n + 1; i < max; i++) {
char tmp = str[i];
str[i] = str[n];
str[n] = tmp;
recursion(str, n + 1, max);
str[n] = str[i];
str[i] = tmp;
}
} else {
printf("%s\n", str);
}
}
int main(void) {
char str[42];
errno = 0;
if (scanf("%41s", str) != 1) {
if (errno != 0) {
perror("scanf()");
} else {
fprintf(stderr, "no input");
}
return 1;
}
recursion(str, 0, strlen(str));
}