how to compare strings with for in C [duplicate] - c

This question already has answers here:
How does c compare character variable against string?
(3 answers)
Closed 1 year ago.
#include <stdio.h>
#include<string.h>
int main(void) {
char* a = "asdf";
for (int i = 0; i < strlen(a); i++) {
if(a[i] == "s"){
printf("%c", a[i]);
}
}
return 0;
}
I think this print "s" but it doesn't print anything what's the problem?

In your example type of "s" is a char *, so your comparison is between a char and a char *. You have to use 's', this type is a char.
To understand the difference try : printf("|%c|\n", "s"); and printf("|%c|\n", 's');

Related

How do I store a string input per word into a 2d array? [duplicate]

This question already has answers here:
How can I correctly assign a new string value?
(4 answers)
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Suppose I accept an input that is a string "I am a beginner in programming". What I want to do is store that input per word into a 2d array. How do I translate that to code?
I am new to programming so I am not really that familiar with it.
I tried coding it this way and perhaps the error is in the inner loop part. I think it repeats storing the "I" after it breaks out from the inner loop and having variable col initialized again? But I can't really think of a way to correct it.
int main(void) {
const int SIZE1=20;
const int SIZE2=30;
char string[100];
char string2[SIZE1][SIZE2];
int row, col;
printf("Input a string: ");
gets(string);
for(row=0; row<SIZE1; row++)
{
for(col=0; col<SIZE2; col++)
{
if(string[col]==' ')
break;
else
string2[row][col]=string[col];
}
}
printf("%s\n", string2[0]);
printf("%s\n", string2[1]);
printf("%s\n", string2[2]);
printf("%s\n", string2[3]);
printf("%s\n", string2[4]);
printf("%s\n", string2[5]);
printf("%s\n", string2[6]);
return 0;
}
You can use strtok function which breaks string into a series of tokens using the delimiter (A Space Character in this case).
#include <stdio.h>
#include <string.h>
int main(void) {
char myText[] = "I am a beginner in programming";
char *p = strtok(myText, " "); // Get The First Token
char *myTextArr[6];
int i = 0;
while (p != NULL) {
myTextArr[i++] = p;
p = strtok(NULL, " "); // Walk through other tokens
}
for (i = 0; i < 6; ++i) {
printf("%s\n", myTextArr[i]);
}
return 0;
}
Output:
I
am
a
beginner
in
programming

Printing char multi dimensional arrays not working like I want to [duplicate]

This question already has answers here:
How should character arrays be used as strings?
(4 answers)
Closed 11 months ago.
#include <stdio.h>
int main()
{
FILE *fisier;
fisier = fopen("cnp.txt", "r");
int cnpuri = 0;
char cnp[10][13];
while(1){
fscanf(fisier, "%s", cnp[cnpuri]);
cnpuri++;
if(feof(fisier))
break;
}
int i;
for(i = 0; i < cnpuri; i++){
printf("%s \n", cnp[i]);
}
fclose(fisier);
return 0;
}
This is the text I'm reading:
1234567890123
1763578126765
4156156546489
5749684631654
5498654168763
And this is what it shows in terminal:
12345678901231763578126765415615654648957496846316545498654168763
1763578126765415615654648957496846316545498654168763
415615654648957496846316545498654168763
57496846316545498654168763
5498654168763
The problem is that the second size of the array
char cnp[10][13];
is not large enough to store the terminating zero character of entered strings.
Declare it at least like
char cnp[10][14];
Also change the while loop the following way
while( cnpuri < 10 && fscanf(fisier, "%s", cnp[cnpuri]) == 1)
{
cnpuri++;
}

How do I get reversed string? [duplicate]

This question already has answers here:
How do you reverse a string in place in C or C++?
(21 answers)
Reverse string with pointers? [duplicate]
(4 answers)
Closed 2 years ago.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void str_prn(char **);
int main(void)
{
char temp[80];
char **str;
int max;
int i;
printf("Number of Strings : ");
scanf("%d", &max);
str = (char **)malloc((max+1)*sizeof(char*));
i=0;
while(1)
{
printf("Input the string : ");
scanf("%s",temp);
str[i] = (char*)malloc(strlen(temp)+1);
strcpy(str[i],temp);
i++;
if(i ==max)
{
printf("Complete!!\n\n");
break;
}
}
str_prn(str);
i=0;
while(str[i]!=0)
{
free(str[i]);
++i;
}
free(str);
system("PAUSE");
return 0;
}
void str_prn(char **sp)
{
int i, len=0;
char *reversedSp;
len = strlen(*sp);
while(*sp !=0)len++;
{
for(i=0;i<len;i++)
{
reversedSp = *sp
}
}
return;
}
I want to get the result of input strings reversed.
I mean if I get "abcdeqwerty" the result should be result should be "edcbaytrewq"
First I used strrev func but my professor said not to use strrev.
So I tried to deal with the matter by using for loop.
In the function str_prn, How can I revise it to work?
You could try tou count the length of your string, and then start from the end...

Accept string using scanf and display them using printf [duplicate]

This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 4 years ago.
I cannot retrieve string using printf . instead getting error : segmentation fault
int main(){
char * a[5];
int i;
printf("\n enter value ");
for (i=0;i<5;i++){
printf("%d Name :\n",i);
scanf("%s",&a[i]);
}
printf("%s",a[2]);
printf("%s",a[3]);
}
~
char * a[5]; is array of 5 char pointer that means you can store 5 char buffer of any length.
The problem is in the statement
scanf("%s",&a[i]); replace this with scanf("%s",a[i]);
case 1 :-
int main(){
char *a[5] = {"stackoverflow","meta","ask ubuntu","super","unix"};
/* here a[row] can have any number of char */
for (int i = 0 ;i < 5;i++) {
printf("%s\n",a[i]);
}
return 0;
}
Case 2 :-
int main(){
char *a[5];
for (int i=0;i<5;i++){
printf("%d Name :\n",i);
a[i] = malloc(MAX);/* define MAX how many char you want to store into that */
scanf("%s",a[i]); /* & not required as a[i] itself address */
}
for (int i = 0 ;i < 5;i++) {
printf("%s\n",a[i]);
}
/* don't forget to free once job is done */
for (int i = 0 ;i < 5;i++) {
free(a[i]);
}
return 0;
}
You shouldn't input to a char* .
And you shouldn't store any string by char*, including string literal, in fact, string literal convert to char* is deprecated many years ago.
Use char a[5][MaximumLength] instead. MaximumLength should be a literal.
The problem is you are using a pointer before it has been initialized. It does not yet point at valid memory. These leads to program crashes or other kinds of unexpected behavior, such as "segmentation faults".
So you should edit it to:
char* my_string = malloc(size+1);
The %c format specifier is used whenever we want to be specific that the variable that we are going to printf or scanf is of type char.
On the other hand the %s format specifier is used to specify to the printf or scanf functions that contents of the address that is specified as the next parameter are to considered as string.
You are taking char as input, not the whole string.
scanf("%s",&a[i]);, will be scanf("%c",&a[i]);
printf("%s",a[3]);, will be printf("%c",a[3]);
printf("%s",a[2]);, will be printf("%c",a[2]);
The following code worked fine:
#include<stdio.h>
#include<stdlib.h>
int main(){
char* a = malloc(size+1); // where size is max string length
int i;
printf("\n enter value ");
for (i=0;i<5;i++){
printf("%d Name :\n",i);
scanf("%c",&a[i]);
printf("%c\n",a[i]);
}
printf("%c\n",a[2]);
printf("%c\n",a[3]);
free(a);
}

Accessing a string using counter variable in C/C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]
In a C++ reference book, I found an example that accessed a string like following:
void main()
{
char *str = "Test";
int len, i;
clrscr();
len = strlen(str);
for(i=0 ; i<len ; i++)
{
printf("%c", i[str]);
}
getch();
}
Why does i[str] work? i is a variable, not an array.
It also works if the string is declared as str[] instead of *str.
Char pointers point to the memory location at the start of a string, and the array indexes (eg, str[i]) are basically adding i iterations to the start of the string.
So, str + i = str[i] = i[str] = i + str
Using this inside printf, like you are doing, all of these will evaluate the same:
printf("%c", str[i]);
printf("%c", i[str]);
printf("%c", *(str+i));
printf("%c", *(i+str));
See also: With arrays, why is it the case that a[5] == 5[a]?
It works because in C i[str] and str[i] are equivalent
i[str] and str[i] evaluate the same way (as *(str+i) )
When you declare str[], str is a pointer to the first element of the array

Resources