i have the following problems in C programming.
I have an array of strings stored as words[10][50]. I want to extract each of the string from the array and then pass it on to another function. I tried on the following:
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
int Check_Anagram(char*,char*);
void main()
{
char words[10][20];
int i;
int flag;
for(i=0;i<3;i++)
{
scanf("%s\n",words[i][20]);
}
for(i=1;i<10;i++)
{
flag = Check_Anagram(words[i][20],words[i-1][20]);
}
getch();
}
int Check_Anagram(char *a,char *b)
{
printf("%s %s\n",a,b);
return 1;
}
This creates an exception during compiling.
Now i think that when i use the "printf" statement then this nomenclature works fine i.i words[i] prints the string "i" from the double dimension words array. When i try to do the same thing with the check function then the error occurs.
Can soemone point me how to do this passing ?
P.S. Please ignore any error in efficiency of program and likewise. I need your help and this is just a test program at learning string passing to a function
Thanks
You're passing words[i][20]. You need to pass words[i] instead in both loops. Try this:
for(i = 1; i < 3; i++) /* i < 3 */
{
flag = Check_Anagram(words[i], words[i-1]);
}
Another problem is that you're reading 3 strings and trying to print 10. So when you pass words[3] it contains garbage: printf tries to print garbage which need not be 0-terminated.
In the first for loop, when i is 0, you're pointing to words[-1], that's your exception.
flag = Check_Anagram(words[i][20],words[i-1][20]);
You are passing the 21st letter of each word the Check_Anagram. Instead you should pass the words themselves:
flag = Check_Anagram(words[i],words[i-1]);
You have a similar problem where you use scanf. To read a line from the console to each word you would use:
for(i=0;i<10;i++)
{
scanf("%s\n",words[i]);
}
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
int Check_Anagram(char [],char []);
void main()
{
char words[10][20];
int i;
int flag;
for(i=0;i<3;i++)
{
scanf("%s\n",words[i]);
}
for(i=1;i<10;i++)
{
flag = Check_Anagram(words[i],words[i-1]);
}
getch();
}
int Check_Anagram(char a[],char b[])
{
printf("%s %s\n",a,b);
return 1;
}
I finally got it corrected thanks to the help of all users.
I have posted the corrected code for people who are struggling with passing of string extracted from an array of strings to another function. hope it helps.
Related
I am trying to access and manipulate a list of strings from a function in C but for some reason i get a seg fault as soon as the loop in the function does 1 pass.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void print_stuff(char*** what)
{
for(int i = 0; i < sizeof(*what); ++i)
{
*what[i] = malloc(5 * sizeof(*what));
*what[i] = "hello";
printf("%s\n", *what[i]);
}
}
int main(void)
{
char** hello;
hello = malloc(20 * sizeof(*hello));
print_stuff(&hello);
}
I was going to write a long, detailed answer, but it got too long considering the amount of problems I can think of about this code. So here is the short version. It looks like what you are trying to do can be done just like this.
#include <stdio.h>
int main(void)
{
for(int i = 0; i < 20; i++) {
printf("hello\n");
}
}
I fail to understand the purpose of the rest of the code. It uselessly allocates memory, does not even do that correctly. And it's just wrong. Maybe more context is needed for what you are trying to achieve.
I am trying to copy a string from an array of strings to another string variable using strcpy... What is the mistake here? Why is the output wrong?
#include <stdio.h>
#include <string.h>
void main() {
int i;
char cw[3][12];
for (i = 0; i < 3; i++)
scanf("%s", &cw[i]);
puts(cw[2]);
char ch[12] = "hftiuh";
puts(ch);
strcpy(ch, cw[2]);
puts(ch[12]);
}
input
hello
again
there
this gives output
there
hftiuh
expected output
there
hftiuh
there
thanks everyone...for just viewing my silly ques...and especially stark for telling that its wrong instead of telling how to correct, it helped...I found the answer, there was no compiler error just to clear most comments...i changed
puts(ch[12]);
to
puts(ch);
thanks everyone.
#include<stdio.h>
#include<string.h>
int main()
{
int i;
char cw[3][12];
for(i=0;i<3;i++)
scanf("%s",*(cw+i));
puts(cw[2]);
char ch[12]="hftiuh";
puts(ch);
strcpy(ch,cw[2]);
puts(ch);
return 0;
}
This is my code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int countNumber(char string[],int number_[]);
int countNumber(char string[]);
int main(){
char string[] = "tran_huynh_minh_phuc";
int num = countNumber(string)+1;
int *number = (int *) calloc(num, sizeof(int));
countNumber(string,number);
for(int i=0;i<num;i++){
printf("%d\n",number[i]);
}
fflush(stdin);
char a[3][14];
strncpy(a[2], string+5, 5);
printf("%s",a[2]);
}
int countNumber(char string[],int *number)
{ int count=0;
int num_i=1;
number[0]=-1;
for(int i=0; i<strlen(string); i++)
{
if(string[i]=='_')
{ number[num_i] = i;
num_i++;
}
}
return count;
}
int countNumber(char string[])
{ int count=0;
for(int i=0; i<strlen(string); i++)
{
if(string[i]=='_')
{
count++;
}
}
return count;
}
this is my problem console results:
I am doing the program to get the substring which will get "tran","huynh","minh" from "tran_huynh_minh", however it is appearing some unexpected letters at the end of my substring. In addition, I tried many ways to fix it but it did not work. Can you find my mistakes?
Thanks
Minh Phuc
I think the intent for the bottom of the main block is something like this (this adds the null after the 'huynh' which was copied. By adding the null, when it goes to print a2, it knows when to stop. It looks like the desire was to put the second word in the array at index 2 (presumably being done in a loop once it was working or the like):
strncpy(a[2], string + 5, 5);
a[2][5] = '\0';
printf("%s", a[2]);
If you run something equivalent to this, you should see the expected output. However, you likely also see a warning on the strncpy function and considering using strncpy_s (depending what you are using to compile. It looks like you are on windows). If you replace the function call with strncpy_s (assuming Visual Studio), you will get the desired result without adding the null at the end separately. Note it expects a size of the destination array as a safeguard (14 in this case).
See this link as well as this one.
I need to read a word from main function and convert the characters in UCASE if the first character is LCASE and vice versa using the user defined function.I tried ways for returning the array from function but still I am lacking some core ideas. Please debug this program and explain the way it works.
#include <stdio.h>
#include <string.h>
int* low (char str)
{
int i;
for (i=1; i<strlen(str);i++)
{
if(str[i]<91)
{
str[i]=str[i]+32;
}
else
{
}
}
return &str;
}
int* high (char str[50])
{
int i;
for (i=0; i<strlen(str);i++)
{
if(str[i]>91)
{
str[i]=str[i]-32;
}
else
{
}
}
return &str;
}
void main()
{
char str[50];
char* strl;
printf("Enter any string....\n");
scanf("%s",str);
if (str[0]<91)
{
*strl=low(str);
}
else
{
*strl=high(str);
}
printf("Converted string is %s.",*strl);
}
There is already a problem here:
So if you are saying this code is perfect and you want us to debug it and explain how (on earth) this works, then here you go.
In function int* low (char str), you have if(str[i]<91). Thats a problem right there. str is a char received as an argument, and hence str[i] is a straight compile-time error.
Another one to deal with is the return statement.
You have a statement:
return &str;
which would return the address of str, which by the way is a char, whereas function low is supposed to return a pointer to an int.
The same is applicable to high function as well.
Suggestion: Leave aside this bad code and get a beginner level C programming book first. Read it and the try some codes out of it.
A few inputs for improvement: (Which you may not comprehend)
change
void main()
to
int main(void)
Why? Refer this legendary post: What should main() return in C and C++?
Secondly, int both functions you are using strlen() in loop which will always return a fixed value. So, instead of
for (i=0; i<strlen(str);i++)
I'd suggest,
size_t strlength = strlen(str);
for (i=0; i < strlength; i++)
You can try the code and method as below:
#include <stdio.h>
#include <string.h>
char* caseConverter (char *str)
{
int i;
for (i=0; i<strlen(str);i++)
{
if(str[i]>=65 && str[i]<=90)
{
str[i]=str[i]+32; //To lower case
}
else if((str[i]>=97 && str[i]<=122))
{
str[i]=str[i]-32; //To upper case
}
else
printf("%c is not an alphabet \n",str[i]);
}
return str;
}
void main()
{
char inputStr[50]= "Stubborn";
char* opStr= caseConverter(inputStr);
printf("Converted string is %s",opStr);
}
I need help to understand why in this little program i cannot manipulate correctly pointers:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void change(char *s[][15]){
int i=0;
while(i<5){
if(s[i][0]=='B') s[i][0]='v';
i++;
}
}
/*My code is supposed to allocate dynamically 5 arrays of 15 chars each
(like tab[5][15])and then put a message on them and try to modify the messages.
In this particular case i'm trying to change the first letter of each string to 'V'.
I'm doing this little experience because of another program
in which i have difficulties accessing double arrays*/
int main(){
int i;
char **s;
s =malloc(5*sizeof(char*));
for(i=0;i<5;i++){
s[i]=malloc(15*sizeof(char));
sprintf(s[i],"Bonjour%d",i);
}
change(s);
for(i=0;i<5;i++){
printf("%s\n",s[i]);
}
return 0;
}
I was expecting :
Vonjour0
Vonjour1
Vonjour2
Vonjour3
Vonjour4
but I get :
Bonjour0
Bonjour1
Bonjour2
Bonjour3
Bonjour4
I'm testing this little code for another program and I don't get why the arrays don't change.
In my other program I can't access the double pointer or print the content.
so my question is : why in this program I can't modify the content of the arrays ?
Your change method needs to use "char** s" instead of char *s[][15]. This is because your method is expecting a pointer to a multi-dimensional array. This is immutable as a result, since your original data type for the string is a pointer to an array of strings (IE: An array of chars).
Hopefully that was clear.
It should be
char **change(char **s){
int i=0;
while(i<5){
if(s[i][0]=='B') s[i][0]='v';
i++;
}
return s;
}
You only need to change the function argument to char *s[].
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void change(char *s[]){
int i=0;
while(i<5){
if(s[i][0]=='B') s[i][0]='v';
i++;
}
}
int main(){
int i;
char **s;
s =malloc(5*sizeof(char*));
for(i=0;i<5;i++){
s[i]=malloc(15*sizeof(char));
sprintf(s[i],"Bonjour%d",i);
}
change(s);
for(i=0;i<5;i++){
printf("%s\n",s[i]);
}
return 0;
}
Program output:
vonjour0
vonjour1
vonjour2
vonjour3
vonjour4