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...
Related
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
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');
This question already has answers here:
How can I read an input string of unknown length?
(11 answers)
Closed 4 years ago.
I am fairly new to C programming language. And I am getting stuck at finding the size of string the user inputs.
A sample code is below:
char * input;
int main(){
printf("Enter the string : ");
scanf("%s\n", input);
int n = ( ) // this is where i put the size of input which i need at run time for my code to work
for (int i=0; i<n; i++){
// code here
// i create threads here
}
for (int i=0; i<n; i++){
// code here
// i join threads here
}
}
So, my problem is I can't find any answers at stack overflow which deals with the size of input above.
Any help will be appreciated.
strlen(char*)
In C, this is the function to find the length of a string
The following example shows the usage of strlen() function:
#include <stdio.h>
#include <string.h>
int main () {
char str[50];
int len;
strcpy(str, "LOL");
len = strlen(str);
printf("Length of |%s| is |%d|\n", str, len);
return(0);
}
And, if you want find lenght without strlen()
#include <stdio.h>
int main()
{
char s[1000], i;
printf("Enter a string: ");
scanf("%s", s);
for(i = 0; s[i] != '\0'; ++i);
printf("Length of string: %d", i);
return 0;
}
This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 6 years ago.
Hello.I want to make a simple English-Turkish dictionary. This is my homework.I have to code with C but I don't know C at all. Why doesn't my following code work?
#include<stdio.h>
int main()
{
int i;
char word_array[10][20] ={"araba","car","kalem","pencil","derin","deep","mavi","blue","el","hand" };
//char arama[5] = {'d','e','r','i','n'};
char search[10] = "araba ";
for(i = 0 ; i < 10; i=i+2){
if(word_array[i] == search){
printf("i found: %s\n",i);
}
else{
printf("The word isnt in the array. %s\n",word_array[i],search);
}
}
return 0;
}
Review the code below. It fixed few issues with your code.
#include<stdio.h>
#include <string.h>
int main()
{
int i;
char word_array[10][20] ={"araba","car","kalem","pencil","derin","deep","mavi","blue","el","hand" };
char search[10] = "araba";
for(i = 0 ; i < 10; i++){
if(strcmp(word_array[i],search) == 0){
printf("i found: %s\n",i);
break;
}
}
if(i>=10) {
printf("The word %s isnt in the array.\n",search);
}
return 0;
}
#include<stdio.h>
#include <string.h> // <-------- Include header containing strcmp()
int main()
{
int i;
char word_array[10][20] = { "araba", "car", "kalem", "pencil", "derin", "deep", "mavi", "blue", "el", "hand" };
//char arama[5] = {'d','e','r','i','n'};
char search[10] = "araba"; // <-------- Remove space to have the same string.
for (i = 0; i < 10; i = i + 1){ // <-------- Search each word in array as opposed to each second word.
if (strcmp(word_array[i], search) == 0){ // <-------- Use proper C-string comparison.
printf("%i found: %s\n", i, word_array[i]); // <-------- Correct specifier "i" to "%i" and add the actual found word to output for "%s"
}
else{
printf("The word isnt in the array. %s\n", word_array[i], search);
}
}
return 0;
}
Also see: How do I properly compare strings?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a reverse fn() for strstr
I wrote this main and function that get two string and check if the second string exist within the first one,
then return the most right index appear place.
If wasn't found return -1.
This is the code I wrote:
#include <stdio.h>
int strindex(char[], char[]);
int main()
{
char a[100];
char b[100];
int search;
printf("Enter two strings, To search the second one in the first one:\n");
printf("Enter the first string to search in:\n");
fgets(a,100,stdin);
printf("Enter the second string to search in the first:\n");
fgets(b,100,stdin);
printf("\n\n THE FIRST STRING IS:%s\n\n THE SEARCH STRING IS:%s",a, b);
printf("\n\n");
search = strindex(a, b);
if(search==-1)
printf("The second String didnt found in the first string\n");
else printf("The second string appear in the first string at most right at index:%d\n",search);
return 0;
}
int strindex(char s[], char t[])
{
int foundIndex = -1;
int tempFound, startNext;
int i,j;
i = j = 0;
while (s[i]!='\0')
{
if(s[i]==t[j])
{
startNext = i+1;
tempFound = i;
while(s[i]!='\0' && t[j]!='\0' && s[i]==t[j])
i++,j++;
if (t[j]=='\0') /**check if it null**/
{
printf("found match");
foundIndex = tempFound;
}
i = startNext;
j = 0;
}
else i++;
}
return foundIndex;
}
I think I have a problem in this line:
if (t[j]=='\0') /**check if it null**/
cause I tried put two string that the second one contain in the first but although t[j] is equal to null it doesn't perform the inside if statement.
I know there are many other ways of writing this program. But I know this one should work too and i am trying to make it work.
You're juggling with indexes, which is quite confusing. Use the indexes for one purpose only and don't set and reset at will. I would suggest to rewrite your function a bit and cleanup the used indexes
int strindex(char s[], char t[])
{
int foundIndex = -1;
int i,j;
for (i = 0; s[i]!='\0'; i++)
{
if(s[i]==t[0])
{
for(j = 1; s[i + j]!='\0' && t[j]!='\0' && s[i + j]==t[j]; j++)
;
if (t[j]=='\0') /**check if it null**/
{
printf("found match");
foundIndex = i;
}
}
}
return foundIndex;
}
You can replace the inner loop with strncmp of course.
And now to your question ;-). Your strindex works as intended. As #wildplasser already noted, fgets stores the final newline \n in the buffer. Since there's no newline in the string to be searched except at the end, you never get a match, if the string to search for is in the middle and not at the end.
When you either strip the newline from a[] and b[], you will see, that strindex works.
Another approach could be to give the strings on the command line instead of reading with fgets.
int main(int argc, char **argv)
{
int search;
printf("\n\n THE FIRST STRING IS:%s\n\n THE SEARCH STRING IS:%s", argv[1], argv[2]);
printf("\n\n");
search = strindex(argv[1], argv[2]);
if(search==-1)
printf("The second String didnt found in the first string\n");
else printf("The second string appear in the first string at most right at index:%d\n",search);
return 0;
}
OK friends. this is the solution that works:-):
thanks to Olaf Dietsche
#include <stdio.h>
int strindex(char[], char[]);
int main()
{
char a[100];
char b[100];
int search;
printf("Enter two strings, To search the second one in the first one:\n");
printf("Enter the first string to search in:\n");
fgets(a,100,stdin);
printf("Enter the second string to search in the first:\n");
fgets(b,100,stdin);
printf("\n\n THE FIRST STRING IS:%s\n\n THE SEARCH STRING IS:%s",a, b);
printf("\n\n");
search = strindex(a, b);
if(search==-1)
printf("The second String didnt found in the first string\n");
else printf("The second string appear in the first string at most right at index:%d\n",search);
return 0;
}
int strindex(char s[], char t[])
{
int foundIndex = -1;
int tempFound, startNext;
int i,j;
i = j = 0;
while (s[i]!='\n')
{
if(s[i]==t[j])
{
startNext = i+1;
tempFound = i;
while(s[i]!='\n' && t[j]!='\n' && s[i]==t[j])
i++,j++;
if (t[j]=='\n')
foundIndex = tempFound;
i = startNext;
j = 0;
}
else i++;
}
return foundIndex;
}